Sub topic 1
Authentication and authorization: what pg_hba.conf decides
Prerequisites
This chapter assumes you can run psql, that you know what a PostgreSQL role is, and that you can edit a file on a Linux server. You should also recognize IP addresses and CIDR notation such as 10.0.0.0/24.
Authentication and authorization
Authentication checks who is connecting. Authorization checks what that role may do once connected. PostgreSQL decides both separately.
pg_hba.conf (host-based authentication file) controls authentication only. It decides whether a connection is accepted at all, from where, to which database, for which role, and what proof of identity is required. Privileges such as GRANT SELECT and table ownership control authorization, and they live inside the database.
Finding and reloading pg_hba.conf
Ask the server where its file lives:
SHOW hba_file;After editing the file, tell the server to re-read it without a restart:
SELECT pg_reload_conf();Anatomy of a pg_hba.conf record
Each non-comment line has up to five fields:
| Field | Meaning |
|---|---|
| TYPE | Connection type, such as `local` or `host` |
| DATABASE | Database name, `all`, or a comma-separated list |
| USER | Role name, `all`, or a list |
| ADDRESS | Client address in CIDR form. Used only by `host` types |
| METHOD | How to authenticate, such as `scram-sha-256` or `reject` |
Connection types
localmatches Unix domain socket connections on the server host.hostmatches TCP connections, with or without TLS.hostsslmatches only TCP connections that use TLS.hostnosslmatches only TCP connections that do not use TLS.
Use hostssl when a rule must never accept an unencrypted password.
How PostgreSQL picks a rule: first match wins
The server reads the file top to bottom and applies the first record that matches the connection's type, database, user, and address. It stops there. If no record matches, the connection is rejected.
Consider this file:
# TYPE DATABASE USER ADDRESS METHOD
local all postgres peer
host all all 10.0.0.0/24 scram-sha-256
host all all 0.0.0.0/0 rejectA connection from 10.0.0.5 as app_user matches the second line. A connection from 203.0.113.9 matches the third line and is refused.
flowchart TD
A["Client connects"] --> B{"Record 1 matches?"}
B -- Yes --> C["Apply its method"]
B -- No --> D{"Record 2 matches?"}
D -- Yes --> C
D -- No --> E["Next record"]
E --> F{"Any record left?"}
F -- No --> G["Connection rejected"]caption: Rule matching in pg_hba.conf, with the first matching record deciding the outcome. alt: A flowchart in which each record is tested in order. A matching record applies its method. If no record matches, the connection is rejected.
Note that a broad rule placed early hides every stricter rule below it.
Exercise
A server has these two lines in order: host all all 10.0.0.0/8 trust and then host appdb app_user 10.1.2.0/24 reject. What happens when app_user connects to appdb from 10.1.2.7?
Show answer
The first line matches, because 10.1.2.7 lies inside 10.0.0.0/8, and it uses trust. The second line is never reached, so the connection succeeds without a password. Swap the lines to make the reject rule take effect.