Sub topic 6
Choosing an authentication method
This chapter assumes you can read a pg_hba.conf record (type, database, user, address, method), you know that the first matching rule wins, and you are comfortable with PostgreSQL roles and basic psql usage. Here you will learn what each common authentication method does, what it trusts, and how to pick one for a given kind of connection.
What the method field decides
The last column of a pg_hba.conf record names the method. The method tells the server how to check that the client is who it claims to be. It says nothing about what the client may do afterward. Privileges come from GRANT and role attributes, and the CONNECT privilege on a database is checked separately.
Methods fall into three groups:
- Methods that skip the check or refuse outright:
trustandreject. - Methods that rely on something the operating system already knows:
peer, andidentfor TCP connections. - Methods that prove identity with a secret:
scram-sha-256,md5,password, andcert(a TLS client certificate).
PostgreSQL also supports gss, sspi, ldap, radius, pam, and bsd. They are useful in directory-managed environments but are outside this chapter.
The methods one by one
reject
reject closes the connection immediately for any matching client. It is best used as an explicit last rule, or to carve out an exception before a broader allow rule. Because rules are tried in file order, a reject for one address placed above a broad scram-sha-256 rule blocks that address while the rest of the network still works.
trust
trust accepts the connection with no password and no identity check. Any client that can reach a matching rule can claim any role name, including a superuser. The database name and role name in the connection string are the only things the server sees.
That makes trust dangerous on anything reachable by more than one person. Common mistakes are leaving host all all 0.0.0.0/0 trust in place after a quick test, or using trust on local for convenience when other users share the server. Use it only for throwaway development databases or tightly controlled single-user machines, and never for a network address range.
peer
peer applies only to local (Unix-domain socket) connections. The server asks the kernel for the operating system user name of the process on the other end of the socket. It then checks that the name matches the requested database role. The kernel supplies this information, so the client cannot fake it.
For example, if you are logged in to Linux as postgres and run psql, the server sees OS user postgres and role postgres, and lets you in without a password. If you are OS user alice and ask for role postgres, the match fails and the connection is refused, unless a user map in pg_ident.conf links the two names.
peer is a good default for local administration. No password is stored or typed, and nothing travels over a network. Its weakness is that anyone who can become that OS user can become that role, so protect the OS account as carefully as you would protect the database role.
peer does not work over TCP, even to 127.0.0.1. For TCP connections the equivalent method is ident, which asks an ident server on the client machine. That protocol is weak and rarely used, so prefer a password method for host rules.
password
password asks the client for the password and sends it in clear text. The server then compares it with the stored verifier. If the connection is not encrypted with TLS, anyone on the network path can read the password. There is almost no reason to choose this method today. Its only real use is with old clients that support nothing else, and even then it should be limited to hostssl rules.
md5
md5 uses a challenge-response exchange so the password itself is not sent. It is still weak. The stored hash is effectively the password equivalent: someone who steals the hash from pg_authid, or captures the exchange, can authenticate without knowing the original password. MD5 is also fast to brute-force. PostgreSQL 14 deprecated md5 password hashing, and PostgreSQL 18 reports a deprecation warning when an MD5 password is set. Keep md5 rules only as a temporary step while migrating roles to SCRAM, which is covered in a later chapter.
One useful detail: an md5 rule in pg_hba.conf will also accept a role whose stored password is a SCRAM verifier. The server switches to SCRAM for that role. The reverse does not work. A scram-sha-256 rule cannot authenticate a role that still has an MD5 hash.
scram-sha-256
scram-sha-256 is the recommended password method. The client and server run a salted, iterated challenge-response exchange. The password is never sent over the wire, and the stored verifier alone does not let an attacker log in as the user. The next chapters explain the details. In short, it is the right choice for any rule that must use a password.
cert
cert authenticates the client by its TLS certificate. The server checks that the certificate chains to a trusted CA and that the certificate's common name equals the requested role name (or maps to it through pg_ident.conf). It only works on hostssl connections.
cert is strong, and suits service-to-service connections where you already operate a certificate authority. The cost is operational: you must issue, distribute, rotate, and revoke certificates. A cert rule on its own does not require a password. You can combine it with clientcert=verify-full on a password rule to demand both.
Comparison
| Method | Applies to | Secret used | Password sent on the wire | Typical use |
|---|---|---|---|---|
| `trust` | any | none | not applicable | Disposable local development only |
| `reject` | any | none | not applicable | Explicit denial, final catch-all |
| `peer` | `local` | OS identity from the kernel | no | Local administration and local services |
| `password` | any | password | yes, in clear text | Avoid |
| `md5` | any | password | no, but weak hash | Temporary, during migration |
| `scram-sha-256` | any | password | no | Default for password logins |
| `cert` | `hostssl` | client certificate | no | Services with a CA and rotation process |
A decision flow
The question to ask is where the client connects from and what it can prove.
Worked example
Consider a server that has these needs:
- The
postgresOS user administers the database over the socket. - A web application at
10.0.0.12connects asapp_usertoappdb. - A reporting job connects from the same subnet and has a client certificate.
- Nothing else should connect.
# TYPE DATABASE USER ADDRESS METHOD
local all postgres peer
hostssl appdb app_user 10.0.0.12/32 scram-sha-256
hostssl appdb report_user 10.0.0.0/24 cert
host all all 0.0.0.0/0 rejectThe rules read as follows:
- Local superuser access uses
peer. No password exists to leak. - The application must use TLS and a SCRAM password, and only from one address.
- The reporting role needs a valid certificate whose common name is
report_user, from the subnet. - Everything else, including plain
hostconnections that no earlier rule matched, is rejected.
Note what happens to other attempts. A local connection by another role matches no rule, and the server refuses it with a "no pg_hba.conf entry" error. If you want a clearer record in the file, add an explicit local all all reject line. A non-TLS connection from 10.0.0.12 skips the hostssl rule and falls to the final reject.
Edge cases and common errors
- Method versus stored password mismatch. A
scram-sha-256rule fails for a role with an MD5 hash. The client seespassword authentication failed, and the server log names the reason, for example that the user's password is not a valid SCRAM secret. - A role with no password. With any password method, a role whose
rolpasswordis null can never log in, because there is nothing to compare against. SettingNOLOGINor a missing password is not a replacement for arejectrule, but the failure is safe. peerover TCP. Ahostrule withpeeris invalid. The server reports an error for that line, and the rule is skipped.pg_hba_file_rulesshows it in theerrorcolumn.certon a non-TLS rule. Usingcerton a plainhostline is also invalid. Usehostssl.- Overly broad
trustahead of a stricter rule. Because the first match wins, atrustline near the top silently disables everything below it for the addresses it covers. - Different methods per role. You can mix methods in one file, but each added method is another path to audit. Choose the fewest that meet your needs.
Selection criteria
Use these questions in order:
- Can this source be denied entirely? If so, use
reject. - Is it a local socket and does an OS identity already map to the role? Use
peer. - Does the client need to prove itself with a password? Use
scram-sha-256, and requirehostsslfor anything off the host. - Do you already run a CA and rotate certificates? Consider
certfor machine accounts. - Is there a legacy
md5rule? Treat it as a migration task, not a design choice. - Is there a
trustorpasswordrule? Replace it unless you can name a specific reason it must remain.
Exercise
A pg_hba.conf has these two lines in this order: host all all 10.0.0.0/24 md5 and hostssl all app_user 10.0.0.12/32 scram-sha-256. The role app_user has a SCRAM verifier, and connects from 10.0.0.12 over TLS. Which rule is used, and what is the consequence? How would you fix the file?
Show answer
The first rule matches, because host matches both TLS and non-TLS TCP connections and the address 10.0.0.12 is inside 10.0.0.0/24. The md5 rule is therefore used, and the second line never applies. The server accepts SCRAM verifiers under an md5 rule and uses SCRAM for this role, so login succeeds, but the rule does not enforce TLS and would also allow MD5 hashes for other roles. Fix it by moving the specific hostssl ... scram-sha-256 line above the broad one, and by changing the broad rule to hostssl ... scram-sha-256 or to reject once other roles are migrated.
Exercise
A developer adds host all all 127.0.0.1/32 peer so that local tools can connect without a password. What happens, and what should they use instead?
Show answer
peer is valid only for local (Unix socket) connections. On a host line the entry is invalid, so the server skips it and reports the problem in the error column of pg_hba_file_rules. Connections over TCP to 127.0.0.1 then fall through to later rules. The developer should connect over the Unix socket, which peer supports, or use a host ... 127.0.0.1/32 scram-sha-256 rule and give the role a password.