Sub topic 6
Choosing an Authentication Method
This chapter assumes you know basic SQL and psql, understand PostgreSQL roles and databases, and can read a pg_hba.conf record. It also assumes you know that an IP address identifies a host and that CIDR notation describes an address range.
Authentication answers, “Who is this client?” Authorization answers, “What may that authenticated role do?” PostgreSQL uses pg_hba.conf to choose how a connection must prove its identity. The selected method does not grant table or database privileges.
Common methods and their uses
| Method | Typical use | Main risk or requirement |
|---|---|---|
| `trust` | Temporary local development | Anyone matching the rule can connect as the requested role |
| `reject` | Denying a known connection pattern | Must appear before broader rules |
| `peer` | Local Unix-socket connections | Operating-system user must map to the database role |
| `password` | Compatibility with clients that cannot use SCRAM | Password needs TLS because it is sent to the server |
| `scram-sha-256` | Modern password authentication | Client and stored role verifier must support SCRAM |
| `cert` | TLS client-certificate identity | Certificate and server-side identity mapping require management |
Local connections
A local record applies to Unix-domain sockets. It has no address column. peer is often a good choice for administrative access because PostgreSQL checks the operating-system user instead of asking for a database password.
local all postgres peer
local all all scram-sha-256The first rule allows the operating-system user postgres to connect as the database role postgres. The second rule requires a password for other local connections. If the operating-system name and database role differ, use a user map or choose another method.
Do not use trust on a shared server. It skips identity proof entirely.
Remote connections
A host record applies to TCP connections, whether or not they use TLS. Prefer scram-sha-256 for remote password authentication:
host appdb app_user 10.0.0.0/24 scram-sha-256The address range limits which clients can attempt this connection. It does not replace authentication. A matching client still needs the correct password and role permissions.
reject is useful for explicit denials:
host all all 0.0.0.0/0 rejectPlace this after permitted rules. Because PostgreSQL uses the first matching record, placing it first would deny every IPv4 connection.
TLS-specific choices
hostssl matches only connections that use TLS. It is appropriate when credentials must be protected in transit:
hostssl appdb app_user 10.0.0.0/24 scram-sha-256
hostnossl all all 10.0.0.0/0 rejectThis combination requires TLS for the application database and rejects non-TLS connections. A hostssl rule is usable only when the server has TLS enabled. A hostnossl rule can make an accidental unencrypted connection fail clearly.
With cert, the client proves its identity using a certificate. PostgreSQL can compare the certificate identity directly with the database role or use pg_ident.conf for a mapping. Certificates avoid password prompts, but issuing, rotating, and revoking them adds operational work.
Decision process
Choose a method by asking:
- Is the connection local or remote?
- Can the client use TLS?
- Does the client support SCRAM?
- Is identity based on an operating-system account, a password, or a certificate?
- What address range and database should be allowed?
Test the most specific permitted rule before adding a broad fallback. A fallback should normally be reject, not trust.
Exercise
Which rule should come first if app_user must connect to appdb only over TLS, while all other remote connections must be denied?
Show answer
Put hostssl appdb app_user <approved-CIDR> scram-sha-256 first, followed by a broad host all all 0.0.0.0/0 reject rule. PostgreSQL uses the first matching rule.