Sub topic 14
Mapping operating system users with pg_ident.conf
This chapter assumes that you know PostgreSQL roles, basic pg_hba.conf syntax, and the difference between local socket connections and TCP connections. You will use pg_ident.conf to connect operating system identities to PostgreSQL role names.
What pg_ident.conf does
Authentication proves an identity. Authorization decides what that identity may do. The pg_ident.conf file helps PostgreSQL translate an authenticated operating system username into a PostgreSQL role name.
The file is used with the peer and ident authentication methods:
peerchecks the operating system user for a local Unix-domain socket connection.identasks an ident service on the client host for a username over TCP.- A user map links the returned operating system username to an allowed PostgreSQL role.
A map does not grant database privileges. The PostgreSQL role still needs the required permissions.
Writing a user map
Each non-comment record in pg_ident.conf has three fields:
| Field | Meaning |
|---|---|
| Map name | Name referenced by `map=...` in `pg_hba.conf` |
| System username | Operating system username returned by peer or ident |
| PostgreSQL username | Role that the connection may use |
For example:
# MAPNAME SYSTEM-USERNAME PG-USERNAME
ops_map alice app_user
ops_map bob app_userThis map allows the operating system users alice and bob to authenticate as the PostgreSQL role app_user. It does not allow either user to authenticate as another role.
The file is usually in the PostgreSQL configuration directory. Use SHOW config_file; or SHOW hba_file; to discover the active configuration location. The pg_ident.conf file is commonly beside pg_hba.conf.
Referencing the map from pg_hba.conf
Add map=ops_map as an option on a peer or ident rule:
# TYPE DATABASE USER ADDRESS METHOD
local appdb app_user peer map=ops_map
host appdb app_user 192.0.2.0/24 ident map=ops_mapThe local rule checks the operating system user for a socket connection. The host rule uses the ident protocol for TCP connections from the specified network.
Rules still follow the first-match-wins rule. A previous matching rule can prevent PostgreSQL from reaching the mapped rule.
Reloading and checking the map
After editing the files, reload the server:
SELECT pg_reload_conf();
SELECT line_number, map_name, sys_name, pg_username, error
FROM pg_ident_file_mappings
ORDER BY line_number;The error column should be null for valid records. Check both files when troubleshooting. A valid map has no effect unless a matching pg_hba.conf rule uses the same map name.
Common failures include a misspelled map name, an operating system username that has no map entry, and a rule earlier in the file that uses another authentication method. An error such as Peer authentication failed for user "app_user" often means that the local operating system name did not map to app_user.
Exercise
The operating system user is alice, the requested PostgreSQL role is app_user, and the HBA rule uses peer map=ops_map. What pg_ident.conf record is needed?
Show answer
Add ops_map alice app_user. Then reload the configuration and confirm that the mapping view reports no error.