PostgreSQL authentication looks like two things — a role catalog and a pg_hba.conf file — but operationally it is one workflow: define who the user is, then declare from where and how they may connect. This reference covers the role model, the pg_hba.conf rule order, the scram-sha-256 pattern, and the audit queries that surface accounts that should not exist.
How to verify
To list every role, what it can do, and how its members are nested:
-- All roles, with their attributes
\du
-- Specific role attributes
SELECT rolname, rolsuper, rolcreatedb, rolcreaterole, rolcanlogin, rolreplication
FROM pg_roles WHERE rolcanlogin = true;
-- Role memberships
SELECT r.rolname AS role, m.rolname AS member
FROM pg_auth_members a
JOIN pg_roles r ON r.oid = a.roleid
JOIN pg_roles m ON m.oid = a.member
ORDER BY r.rolname;
And the active hba rules:
sudo cat /etc/postgresql/16/main/pg_hba.conf | grep -v '^#' | grep -v '^$'
# Or query from inside the database (PG 10+)
sudo -u postgres psql -c "SELECT line_number, type, database, user_name, address, auth_method FROM pg_hba_file_rules;"
What’s happening
A PostgreSQL role is the unit of identity. A role can LOGIN (act as a user) or not (act as a group), can be a SUPERUSER or not, and can own objects. Granting one role to another creates a membership; the child can SET ROLE parent to act with the parent’s privileges. The distinction between user and group dissolved in 8.1 — there is only role.
pg_hba.conf (Host-Based Authentication) is the firewall layer between a client and any role-based authentication. Each line declares: connection type (local for Unix socket, host for TCP, hostssl for TLS-required TCP, hostnossl for plain TCP), target database (all or a list), target role (all or a list), client address (CIDR or hostname), and auth method (peer, trust, md5, scram-sha-256, cert, ldap, others). Rules are evaluated top-to-bottom; the first match wins.
scram-sha-256 is the modern default for password auth (introduced in PG 10, default in 14+). It does not send the password in cleartext or in a reversible hash. md5 is the legacy method — supported but you should migrate.
The procedure
-
Set the password encryption method to scram-sha-256. In
postgresql.conf:password_encryption = scram-sha-256Reload. Then any new password (or
\passwordchange) is stored as scram. Existing md5 passwords keep working until users change them. -
Create application roles with the principle of least privilege.
-- A group role for the schema's owner privileges CREATE ROLE orders_owner NOLOGIN; CREATE SCHEMA orders AUTHORIZATION orders_owner; -- A group role for read/write access CREATE ROLE orders_app NOLOGIN; GRANT USAGE ON SCHEMA orders TO orders_app; GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA orders TO orders_app; GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA orders TO orders_app; -- Default privileges for future objects ALTER DEFAULT PRIVILEGES FOR ROLE orders_owner IN SCHEMA orders GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO orders_app; -- The login role for the application CREATE ROLE orderapp WITH LOGIN PASSWORD 'strong-password' IN ROLE orders_app; -
Configure pg_hba.conf with explicit rules ordered most-specific to least-specific. A working file:
# TYPE DATABASE USER ADDRESS METHOD # Local admin via socket — peer auth (no password) local all postgres peer # Replication user from each replica host hostssl replication repl 10.0.0.6/32 scram-sha-256 hostssl replication repl 10.0.0.7/32 scram-sha-256 # Application from app subnet, TLS required hostssl orders orderapp 10.0.1.0/24 scram-sha-256 # Read-only analyst from BI subnet hostssl orders analyst 10.0.2.0/24 scram-sha-256 # Reject everything else explicitly host all all 0.0.0.0/0 reject host all all ::/0 reject -
Reload after editing.
sudo systemctl reload postgresql@16-mainConfirm the rules loaded without error:
SELECT * FROM pg_hba_file_rules WHERE error IS NOT NULL; -
Require TLS for all non-local connections. In
postgresql.conf:ssl = on ssl_cert_file = '/etc/ssl/certs/postgres.crt' ssl_key_file = '/etc/ssl/private/postgres.key' ssl_min_protocol_version = 'TLSv1.2'And use
hostssl(nothost) in pg_hba.conf for any non-local rule. -
Test the rule from the application network.
psql "host=db.internal port=5432 dbname=orders user=orderapp sslmode=verify-full sslrootcert=/etc/ssl/certs/ca.crt"sslmode=verify-fullrequires a valid cert AND a hostname match — the strictest mode. -
Rotate passwords with the dual-password pattern. Postgres has no native dual-password but the wire protocol allows multiple passwords on the same role via SCRAM mechanism — in practice, schedule rotation with rolling deploys: change the password in vault, deploy the app with the new password (it reconnects on next pool refresh), then verify no connections are using the old one with
SELECT * FROM pg_stat_activity. -
Periodic audit.
-- Superuser accounts — should be tightly held SELECT rolname FROM pg_roles WHERE rolsuper = true; -- Login accounts older than 90 days without activity SELECT rolname FROM pg_roles WHERE rolcanlogin = true AND NOT EXISTS (SELECT 1 FROM pg_stat_activity WHERE usename = rolname); -- Hba rules that are not hostssl (potential cleartext exposure) SELECT * FROM pg_hba_file_rules WHERE type IN ('host','hostnossl');
Common pitfalls
- A
trustline in pg_hba.conf disables authentication entirely. Never usetrustoutside of single-user mode on a freshly-built host. - The order of pg_hba.conf rules matters absolutely — a permissive line above a restrictive one means the restrictive line is dead. Verify with
SELECT * FROM pg_hba_file_rules(it shows the effective order). - Granting role membership does not automatically activate it on connection — the user must
SET ROLE. UseALTER ROLE orderapp SET role = orders_app;to set a default. pg_hba.confchanges take effect on reload, but aSIGHUP(which reload sends) does not interrupt existing connections — old credentials keep working until reconnect.peerauth requires the OS user name to match the DB role name.psql -U postgresfrom your own shell fails with “peer authentication failed” — usesudo -u postgres psql.
In the engagements we run, role definitions and pg_hba rules live in version control, are applied with Ansible’s postgresql_user and postgresql_pg_hba modules, and are audited monthly for drift. We carry this under Managed Operations so the role catalog is a documented system, not a folklore.