Sub topic 5
How PostgreSQL Picks a Rule: First Match Wins
Assume you know basic SQL and psql, PostgreSQL roles and databases, Linux configuration files, and IP addresses in CIDR notation. This chapter focuses on how PostgreSQL searches pg_hba.conf, the client authentication configuration file.
The first matching record controls the connection
PostgreSQL reads pg_hba.conf from top to bottom. For each incoming connection, it checks the record fields:
- Connection type, such as
localorhost - Database name
- User or role name
- Client address, for network connections
- Authentication method and its options
The first record whose fields match the connection is selected. PostgreSQL does not continue looking for a better rule after that. If authentication fails under the selected rule, the connection fails.
# TYPE DATABASE USER ADDRESS METHOD
host all all 10.0.0.0/24 scram-sha-256
host appdb all 10.0.0.0/24 rejectA connection from 10.0.0.25 to appdb matches both records. PostgreSQL uses the first record, so it requests SCRAM authentication. The later reject record has no effect.
To reject appdb clients from that network, reverse the records:
# TYPE DATABASE USER ADDRESS METHOD
host appdb all 10.0.0.0/24 reject
host all all 10.0.0.0/24 scram-sha-256Now the specific exception appears before the general rule.
Specific rules must precede general rules
The keywords all and broad network ranges match many connections. Put narrow rules first and broad fallback rules later.
| Connection | Specific rule | General rule |
|---|---|---|
| `app_user` to `appdb` from `10.0.0.0/24` | `host appdb app_user 10.0.0.0/24 scram-sha-256` | `host all all 0.0.0.0/0 reject` |
| Result | The specific rule is selected | The fallback is not reached |
A rule matches only when all relevant fields match. For example, a rule for database appdb does not match a connection to postgres. A rule for 10.0.0.0/24 does not match a client at 10.0.1.5.
Connection type also affects matching
A local record applies to Unix-domain socket connections and has no address column. A host record applies to TCP connections, whether or not TLS is used. hostssl applies only when the client uses TLS, while hostnossl applies only without TLS.
# TYPE DATABASE USER ADDRESS METHOD
local all all peer
hostssl appdb app_user 10.0.0.0/24 scram-sha-256
host all all 0.0.0.0/0 rejectA TLS connection to appdb from 10.0.0.25 as app_user uses the hostssl record. A non-TLS connection from the same client skips hostssl and reaches the later host rejection.
A reliable way to design rule order
Use this order:
- Put explicit rejections or special exceptions first.
- Put specific database, role, and network rules next.
- Put broader rules after specific rules.
- End with an explicit rejection when possible.
After editing, reload the configuration and inspect the parsed records:
SELECT pg_reload_conf();
SELECT line_number, type, database, user_name, address, auth_method, error
FROM pg_hba_file_rules
ORDER BY line_number;The line_number column helps you review order. A non-null error indicates that PostgreSQL could not parse a record.
Exercise
A client connects over TCP from 192.168.1.20 to database appdb as report_user. Which rule wins?
Show answer
host appdb all 192.168.1.0/24 reject wins if it appears before any broader rule, because it matches the connection first.