pg_hba.conf and Authentication
Pedal's new reporting server cannot connect: pg_hba.conf rejects connection for host "10.0.2.14", user "asha", database "pedal", no encryption. The role exists, the password is right, the grants are right. Before PostgreSQL checks any of that, it asks a different question: is this kind of connection allowed in at all, and how must it prove who it is?
The rule
Top to bottom, first match wins; no match, no entry.
pg_hba.conf (host-based authentication) is a list of rules. For each new connection PostgreSQL reads the rules from the top and uses the first line whose type, database, user and address all match. That line's method decides how the client authenticates. If authentication fails, it does not try the next line. If no line matches, the connection is rejected.
# TYPE DATABASE USER ADDRESS METHOD
local all postgres peer
hostssl pedal pedal_app 10.0.1.0/24 scram-sha-256
hostssl pedal +analysts 10.0.2.0/24 scram-sha-256
host replication replicator 10.0.9.5/32 scram-sha-256
host all all 0.0.0.0/0 rejectThe columns:
- TYPE:
localfor Unix-socket connections on the same machine,hostfor TCP with or without encryption,hostsslfor TCP with TLS only. - DATABASE: a name,
all, orreplicationfor streaming-replication connections. - USER: a role,
all, or+groupfor every member of a group role. - ADDRESS: an IP range in CIDR form, such as
10.0.2.0/24. - METHOD: how to authenticate.
The methods a DBA uses most:
| Method | Meaning |
|---|---|
scram-sha-256 |
Password check that never sends the password itself; the modern default |
peer |
Local only: the OS user name must equal the role name |
cert |
The client presents a TLS certificate |
reject |
Refuse outright |
trust |
Let anyone in without checking. Never on a shared server |
md5 |
Older password method, deprecated; move to SCRAM |
Two more things to know. Edits take effect after a reload, and SELECT * FROM pg_hba_file_rules; shows how the server parsed the file, including errors. And pg_hba.conf only matters for connections that reach the server: listen_addresses in postgresql.conf (default localhost) decides which network interfaces it listens on at all, and changing it needs a restart.