Sub topic 14
Mapping operating system users with pg_ident.conf
This chapter assumes you can read a pg_hba.conf record, you know first-match-wins ordering, and you know what peer and cert authentication do. You should also be able to reload the server and query pg_hba_file_rules. Earlier chapters covered all of these.
Why a second file exists
With peer authentication, PostgreSQL asks the kernel which operating system user is on the other end of a local socket. By default it then requires that name to equal the requested database role. The OS user alice can connect as role alice, and nothing else.
That default breaks down in common situations:
- Several people share one service role, such as
app_user, but each has their own login on the server. - OS account names and role names follow different conventions.
- A TLS client certificate carries a name like
[email protected], and the role is justalice.
A user name map solves this. It is a named list of allowed pairs, each pairing an external identity with a database role. The pairs live in pg_ident.conf, and a pg_hba.conf record selects a map with the map= option.
A map decides only whether an external identity may be treated as a given role. It does not grant privileges. Privileges still come from role membership and GRANT. A map also does not apply to password methods such as scram-sha-256, because those have no external identity to translate.
Where maps apply
The map= option works with methods where the server learns an identity from outside the password exchange:
| Method | Where the external name comes from |
|---|---|
| `peer` | OS user of the local socket client |
| `ident` | Reply from an ident server for TCP clients |
| `cert` | Common Name (or full DN) of the client certificate |
| `gss`, `sspi` | Kerberos or Windows principal |
| `ldap`, `radius` | Only with certain options. Check the docs for your version. |
This chapter focuses on peer and cert, the two you are most likely to use.
Finding the file
pg_ident.conf normally sits next to pg_hba.conf in the data directory. Ask the server instead of guessing:
SHOW ident_file;
SHOW hba_file;Debian and Ubuntu packages keep both under /etc/postgresql/<version>/<cluster>/. Red Hat family packages keep them in the data directory, usually /var/lib/pgsql/<version>/data/.
Record format
Each non-comment line has three fields separated by whitespace:
# MAPNAME SYSTEM-USERNAME DATABASE-USERNAME
appmap alice app_user
appmap bob app_user
appmap postgres postgres- MAPNAME is an arbitrary label. Many lines can share one name, and together they form one map.
- SYSTEM-USERNAME is the external identity: the OS user for
peer, or the certificate name forcert. - DATABASE-USERNAME is the role the identity may act as.
Lines starting with # are comments. Double quotes let a field contain spaces. A field that begins with @ is treated as a file inclusion in the same way as in pg_hba.conf, so avoid that character as the first character of a name.
The map above lets alice and bob both connect as app_user, while postgres may connect only as postgres. A line is needed for every allowed pair.
Connecting a map to a pg_hba.conf record
Add map= to the options column:
# TYPE DATABASE USER ADDRESS METHOD OPTIONS
local appdb app_user peer map=appmap
local all postgres peer
local all all scram-sha-256The USER column of the pg_hba.conf record still matches the role the client asked for, for example psql -U app_user. The map is checked after that rule matches. It answers one question: may this external identity be that role?
sudo -u alice psql -U app_user appdbFor this command the server does the following:
- It finds that the connection is local and matches the first record, since the database is
appdband the requested role isapp_user. - It reads the OS user from the socket, which is
alice. - It searches
appmapfor the pairaliceandapp_user. It finds one, so the connection is allowed.
If carol ran the same command, no pair would match. The connection would fail with an error such as Peer authentication failed for user "app_user".
An important side effect
Without map=, the identity must equal the role name. With map=, that implicit rule is gone. Only the pairs listed in the map count.
If you add map=appmap to a record and the map contains no line for postgres / postgres, then the OS user postgres can no longer connect as role postgres through that record. Add a line for every pair you still want, including the ones that used to work automatically.
Regular expressions and substitution
If the system username begins with /, the rest is a regular expression. It matches if any part of the name matches, so anchor it with ^ and $. The database username may then contain \1, which is replaced by the first parenthesized group of the match.
A typical use is client certificates whose Common Name is an email address:
# MAPNAME SYSTEM-USERNAME DATABASE-USERNAME
certmap /^(.*)@example\.com$ \1A certificate with CN [email protected] maps to role alice. A certificate with CN [email protected] does not match, because of the anchors. Without ^ and $, a name such as [email protected] would match the unanchored pattern, so always anchor.
The matching record in pg_hba.conf could look like this:
hostssl appdb all 10.0.0.0/24 cert map=certmapHere all in the USER column means the client may request any role. The map then ensures the requested role equals the certificate's local part. Role bob requested with a certificate for [email protected] is refused.
Applying and checking changes
pg_ident.conf is read at reload time, like pg_hba.conf:
SELECT pg_reload_conf();From PostgreSQL 15, the pg_ident_file_mappings view shows how the file was parsed, in the same way that pg_hba_file_rules does for the HBA file:
SELECT map_number, line_number, map_name, sys_name, pg_username, error
FROM pg_ident_file_mappings
ORDER BY line_number;A non-null error marks a line the server could not parse, such as a bad regular expression. Check this view before you reload, because a file with errors can lock out users who depend on the map. The view reads the file on disk, so you can inspect your edit before the server uses it.
Then confirm the result from a real session:
SELECT session_user, current_user;Both should show the database role, not the OS name. Run the check through the same route your users take.
Common errors
- Peer authentication failed for user "app_user". The OS user is not paired with that role in the map. Check
sys_nameandpg_usernameinpg_ident_file_mappings, and check that themap=name in the HBA record is spelled the same asMAPNAME. - Ident authentication failed. The same cause applies to
ident, or the ident server is unreachable or untrusted. Preferpeerfor local use andcertorscram-sha-256over TCP. - A regular expression never matches. Check that the field starts with
/, and remember that the pattern is compared with the whole system name when you anchor it. Forcert, the name compared is the CN unless the record setsclientname=DN. A DN has a different shape, so write the pattern for it. - An old working login stopped. You added
map=to a record and left out the implicit pair. Add the line. - Map edits have no effect. You did not reload, or you edited a different file than
SHOW ident_filereports. - A map line seems ignored. The
map=option is on a different record than the one that matches the connection. Remember that first match wins inpg_hba.conf. Look atpg_hba_file_rulesto see which rule comes first.
Design guidance
Use a map when the mapping is a real policy, such as several admins sharing a service role, or certificate names that differ from role names. Do not use one to hide a naming mismatch you could fix at the source. For many users, name roles to match OS or certificate names and skip the map.
Keep in mind that a map allows an identity to become a role. The fewer pairs it lists, and the fewer privileges the target roles hold, the smaller the damage when an OS account is compromised. Map people to roles with limited rights, not to a superuser, and use SET ROLE for escalation.
Exercise
The server has this record and map, and OS user postgres now gets "Peer authentication failed for user postgres" when it runs psql -U postgres appdb. Why, and how do you fix it?
pghba.conf: local appdb all peer map=appmap
pgident.conf: appmap alice app_user
Show answer
Adding map=appmap turns off the default rule that an OS user may connect as the role with the same name. The map has no pair for postgres / postgres, so the record refuses it. Add the line appmap postgres postgres to pg_ident.conf. Then check it with SELECT * FROM pg_ident_file_mappings; and run SELECT pg_reload_conf();.
Exercise
Write a map line that lets a client certificate with CN [email protected] connect as role dana, but does not match [email protected]. Name the map certmap.
Show answer
certmap /^(.*)@example\.com$ \1. The ^ and $ anchors force the whole CN to end with @example.com, and \1 substitutes the captured local part as the role name. Without the anchors, the longer hostile name would match.