pg_hba.conf decides who may connect to a PostgreSQL server, from where, to which database, and how they prove who they are. It reads like a firewall rule list, but three of its rules surprise almost everyone, and most of the mistakes below follow from them. The behaviour described here is PostgreSQL 16's, from src/backend/libpq/hba.c and the pg_hba.conf chapter of the manual, and each point was checked against a real PostgreSQL 16.15 server (the parity run). This guide is not affiliated with the PostgreSQL Global Development Group.
For each connection PostgreSQL reads the rules from the top, with an included file's lines standing where its include line is, and stops at the first line whose connection type, client address, database and user all match. It then uses that line's method. It does not look for the most specific line, and if the password is wrong it does not try the next line. A connection no line matches is refused.
If any line fails to parse, or an included file cannot be opened, PostgreSQL rejects the file as a whole. At start-up the server will not start. On a reload (pg_ctl reload, SELECT pg_reload_conf(), systemctl reload) it logs the error and carries on with the previous rules, so your change has no effect and nothing on the screen says so. SELECT * FROM pg_hba_file_rules WHERE error IS NOT NULL; reads the file on disk and shows the errors before you reload.
all, sameuser, samerole, replication, samehost and samenet are keywords only without quotes; "all" is a database or role named all. The database all does not match physical replication connections (pg_basebackup, streaming standbys): those match only lines that say replication. A +group matches members of the role, and a superuser is not counted as a member unless it was granted the role.
This file is a composite of patterns that are common in real deployments:
# pg_hba.conf on an app database, edited over the years
local all postgres peer
local all all trust
host all all 127.0.0.1/32 trust
host all all 0.0.0.0/0 md5
host all postgres 0.0.0.0/0 reject
host reporting /analyst 10.20.0.0/16 password
hostssl all all 192.168.10.5/24 scram-sha-256
host all backup 010.0.0.7/32 scram-sha-256
The CLI's output for it, unedited:
pg_hba.conf Check: 1 high, 7 medium, 6 low
Read, in order: pg_hba.conf
Rules loaded: 8
[high] cleartext-password-no-tls pg_hba.conf:7
host reporting /analyst 10.20.0.0/16 password
password from 10.20.0.0/16 on a host line: the client sends the password in clear text, and this line also accepts connections without TLS, so anyone on the network path can read it.
fix: Change the line to hostssl, and use scram-sha-256 instead of password.
[medium] trust-local pg_hba.conf:3
local all all trust
trust on the local socket for users all, databases all: anyone with a shell on this machine can connect as any of those roles, including the postgres superuser, with no password.
fix: Use peer (the OS user must match the role) or scram-sha-256.
[medium] trust-loopback pg_hba.conf:4
host all all 127.0.0.1/32 trust
trust from 127.0.0.1/32: any process on the server (another user, a container sharing the network, a web app with an SSRF bug) can connect as all with no password.
fix: Use scram-sha-256 for TCP, or connect over the local socket with peer.
[medium] no-tls-required pg_hba.conf:5
host all all 0.0.0.0/0 md5
host md5 from 0.0.0.0/0: this line also accepts connections without TLS, so queries and results cross the network unencrypted, and an attacker in the path can take over the session after login.
fix: Use hostssl for remote lines, and add a hostnossl ... reject line above them so non-TLS attempts are refused.
[medium] shadowed-rule pg_hba.conf:6
host all postgres 0.0.0.0/0 reject
This rule can never match: line 5 (host all all 0.0.0.0/0 md5) comes first and matches every connection this one would. PostgreSQL uses the first matching line, not the most specific one. The reject never fires, so the connections it was meant to block are let in by the earlier line.
fix: Move this line above the broader one, or delete it.
[medium] unanchored-regex pg_hba.conf:7
host reporting /analyst 10.20.0.0/16 password
user regex /analyst is searched for anywhere in the name: with no ^ it also matches names that merely contain analyst; with no $ it also matches longer names that start with it.
fix: Anchor it: /^analyst$.
[medium] no-tls-required pg_hba.conf:9
host all backup 010.0.0.7/32 scram-sha-256
host scram-sha-256 from 8.0.0.7/32: this line also accepts connections without TLS, so queries and results cross the network unencrypted, and an attacker in the path can take over the session after login.
fix: Use hostssl for remote lines, and add a hostnossl ... reject line above them so non-TLS attempts are refused.
[medium] ipv4-shorthand pg_hba.conf:9
host all backup 010.0.0.7/32 scram-sha-256
The address is read with inet_aton rules: it means 8.0.0.7 (a leading 0 is octal, a 0x part is hex, and missing parts are filled from the last one).
fix: Write all four parts in plain decimal.
[low] md5-method pg_hba.conf:5
host all all 0.0.0.0/0 md5
md5 from 0.0.0.0/0: MD5 password hashes are weak and deprecated (PostgreSQL 18 warns on them). An md5 line already uses SCRAM for roles whose stored password is a SCRAM verifier, so the risk is roles still holding MD5 hashes.
fix: Set password_encryption = scram-sha-256, re-set each role's password, then change the line to scram-sha-256.
[low] any-address pg_hba.conf:5
host all all 0.0.0.0/0 md5
md5 from 0.0.0.0/0: anyone on the internet who can reach the port can try passwords against all.
fix: Narrow the address to the networks that need access, and keep the port off the public internet (firewall, security group, listen_addresses).
[low] shadowed-rule pg_hba.conf:7
host reporting /analyst 10.20.0.0/16 password
This rule can never match: line 5 (host all all 0.0.0.0/0 md5) comes first and matches every connection this one would. PostgreSQL uses the first matching line, not the most specific one.
fix: Move this line above the broader one, or delete it.
[low] host-bits-set pg_hba.conf:8
hostssl all all 192.168.10.5/24 scram-sha-256
192.168.10.5/24 has host bits set, so it matches the whole 192.168.10.0/24, not just 192.168.10.5.
fix: Write 192.168.10.0/24 if that is intended, or 192.168.10.5/32 for one host.
[low] shadowed-rule pg_hba.conf:8
hostssl all all 192.168.10.5/24 scram-sha-256
This rule can never match: line 5 (host all all 0.0.0.0/0 md5) comes first and matches every connection this one would. PostgreSQL uses the first matching line, not the most specific one.
fix: Move this line above the broader one, or delete it.
[low] shadowed-rule pg_hba.conf:9
host all backup 010.0.0.7/32 scram-sha-256
This rule can never match: line 5 (host all all 0.0.0.0/0 md5) comes first and matches every connection this one would. PostgreSQL uses the first matching line, not the most specific one.
fix: Move this line above the broader one, or delete it.
Which line decides:
local socket, user postgres, database postgres: pg_hba.conf:2 peer
TCP from 127.0.0.1, user postgres: pg_hba.conf:4 trust
TCP from the internet (203.0.113.10), no TLS, user postgres: pg_hba.conf:5 md5
TCP from the internet (203.0.113.10), TLS, user postgres: pg_hba.conf:5 md5
replication from the internet (203.0.113.10), TLS, user postgres: no line matches, refused
parse-error (error)10.1 or 192.168.1.0 without /bits makes the next field the netmask, so the method is read as a mask (invalid IP mask "md5"); scram instead of scram-sha-256; peer on a host line; cert or clientcert on a line that is not hostssl; a host name with /24; an option without =; a comma inside an option value (ldapsuffix=,dc=example) that splits it into two options unless the value is quoted. Because of rule 2, one of these refuses the whole file.include-missing (error)include, include_dir or @file names something that is not among the files given. A relative path is taken from the directory of the file that names it. If it is missing on the server too, the whole file is refused. include_if_exists is the form for an optional file.no-entries (error)hostssl-without-ssl (medium, only when you say the server runs with ssl = off)hostssl line on a server without SSL is accepted with a warning (the view shows an error) but never matches, so TLS clients fall through to the rules below.trust-remote (high), trust-loopback (medium), trust-local (medium)trust lets the connection in as the role it names, with no password. From a network range that is anyone who can reach the port. On 127.0.0.1 or samehost it is every process on the server, including other users, containers sharing the network namespace and a web app tricked into making requests (SSRF). On the local socket it is anyone with a shell. initdb writes trust lines unless given -A, and container images often keep them.ident-over-tcp (high, medium on loopback)ident asks the client machine's ident service (port 113) who owns the connection and believes the answer. Whoever controls the client machine chooses the answer. (On the local socket ident silently becomes peer, which is fine.)cleartext-password-no-tls (high)password, ldap, pam, radius and bsd have the client send the password itself. On a host line from a remote range the connection may be unencrypted, so the password crosses the network in clear. Make the line hostssl.password-method (low)password where the transport is protected (a hostssl line, loopback, the socket): the server still receives the password rather than a SCRAM proof. Prefer scram-sha-256.md5-method (low)md5 line already authenticates with SCRAM for roles whose stored password is a SCRAM verifier, so migrate the stored passwords (password_encryption = scram-sha-256, then set each password again) and then change the line.no-tls-required (medium)host, hostnossl or hostnogssenc line with scram-sha-256, md5 or gss: the login itself does not reveal the password, but the session that follows can be unencrypted. A common layout is hostnossl all all 0.0.0.0/0 reject first, then hostssl lines.any-address (low)all, 0.0.0.0/0 or ::/0 with a password method: anyone who can reach the port can try passwords. Narrow the range and keep the port off the public internet.secret-in-file (medium)ldapbindpasswd= or radiussecrets= keeps a secret in pg_hba.conf, and pg_hba_file_rules shows it to anyone allowed to read that view.unanchored-regex (medium)/ is a regular expression, and it is searched for anywhere in the name: /admin matches notadmin and admin_readonly. Write /^admin$.shadowed-rule (low; medium for a reject)reject written to block one user or network under a line that lets everyone in: the block never happens. The check is conservative: it only reports a line when the earlier one certainly covers its type (a host line covers hostssl and hostnossl), address range, databases and users.quoted-keyword (low)"all", "replication", "samehost" and the other keywords in quotes mean a name, not the keyword (rule 3). A quoted address keyword is read as a host name.ipv4-shorthand (medium)010.0.0.1 is 8.0.0.1), 0x is hex, and missing parts are filled from the last one (10.1 is 10.0.0.1). Write four decimal parts.address-is-hostname (medium when it looks like an IP, else low)10.0.0.300 becomes a host name that almost certainly never matches. Real host names make PostgreSQL do a reverse lookup of each client and check that it resolves back, so slow DNS delays logins and whoever controls that DNS decides who matches.host-bits-set (low)192.168.10.5/24 matches all of 192.168.10.0/24, not one host. Write /32 for a single address.include-dir-file-ignored (low)include_dir reads only names that end in .conf, do not start with a dot and are at least six characters long, in byte order; 20-ops.conf.bak is skipped.The parser is an independent implementation in JavaScript, not PostgreSQL code. 600 generated files of odd but plausible lines (quotes, commas, continuations, includes, bad masks, shorthand addresses, every method and many options) were written to a PostgreSQL 16.15 server's data directory and its pg_hba_file_rules view read back; the checker agrees with every row: the type, databases, users, address, netmask, method, options and error text (for regular expressions PostgreSQL's own engine words its errors differently, so there only the failure is compared). Then 160 files of valid rules were loaded one at a time and 36 real connections made against each, over the socket and TCP with and without TLS, as three roles to three databases and as physical replication; for every one of the 5,760 connections the checker names the same line PostgreSQL logged as the one that authenticated, or the same refusal. The cases, the runner and the recorded output are in the repository.
Limits: regular expressions are evaluated with JavaScript's engine, which agrees with PostgreSQL's for common syntax; host names, samehost and samenet from outside addresses, and group membership you have not given are reported as undecided rather than guessed; pg_ident.conf maps are not read.