What the File Decides
Every connection attempt carries four facts: the connection type, the database, the role name, and the client address. PostgreSQL compares them with the lines of pg_hba.conf (host-based authentication) and applies the method of the first line that matches. If no line matches, the connection is refused.
The file decides only how a client logs in. What a role may do afterward is set with GRANT, and whether the server listens on the network at all is set by listen_addresses, covered in PostgreSQL remote connections.
Where the File Is
Ask the server:
sudo -u postgres psql -c 'SHOW hba_file;'
- Debian and Ubuntu:
/etc/postgresql/VERSION/main/pg_hba.conf, where VERSION is the major version. - AlmaLinux and Rocky Linux, system packages:
/var/lib/pgsql/data/pg_hba.conf. - AlmaLinux and Rocky Linux, PostgreSQL project packages:
/var/lib/pgsql/VERSION/data/pg_hba.conf.
The Fields of a Line
# TYPE DATABASE USER ADDRESS METHOD
local all postgres peer
host appdb app_user 203.0.113.10/32 scram-sha-256
| Field | Values |
|---|---|
| TYPE | local, host, hostssl, hostnossl |
| DATABASE | A name, all, replication, and more |
| USER | A role, all, or +group |
| ADDRESS | IP with mask; none for local |
| METHOD | How the client proves identity |
local lines match Unix socket connections. host matches TCP with or without TLS, hostssl only with TLS, and hostnossl only without.
The First Matching Line Wins
There is no fallback: if the first matching line rejects the client, or its password check fails, PostgreSQL does not try later lines. Put narrow lines above broad ones:
host all all 10.0.0.99/32 reject
host appdb app_user 10.0.0.0/24 scram-sha-256
Authentication Methods
- peer: for local sockets. The operating system user name must match the role name. Debian and Ubuntu use it for postgres.
- scram-sha-256: a password challenge in which the password itself is never sent. Use it for network connections.
- md5: the older password method, deprecated since PostgreSQL 18 and due to be removed in a later major release. Move roles to SCRAM: make sure
password_encryptionisscram-sha-256(the default on current versions) and set each password again. - trust: anyone who matches the line logs in without a password. Never use it on a
hostline. - reject: refuses the connection outright.
Examples
# Local socket: system users log in as the role of the same name
local all all peer
# One application server on the private network
host appdb app_user 10.0.0.20/32 scram-sha-256
# A private subnet, reporting role
host appdb report 10.0.0.0/24 scram-sha-256
# One remote client, TLS only
hostssl appdb app_user 203.0.113.10/32 scram-sha-256
Use /32 for one IPv4 address. Avoid 0.0.0.0/0; limit the port in the firewall as well, as shown in port 5432.
Apply Changes Safely
A mistake here can lock out every client, including your applications. Keep the peer line for postgres near the top, and work in this order:
- Copy the file:
sudo cp pg_hba.conf pg_hba.conf.bakin its folder. - Keep a psql session as postgres open while you edit.
- Check the file as written on disk with the
pg_hba_file_rulesview, shown below. Fix any row with an error. - Reload, do not restart: open sessions stay connected, new ones use the new rules.
- Test a new connection from the client in a second terminal.
The view and pg_reload_conf() are run inside psql; other useful meta-commands are in the psql commands cheat sheet, and roles are created as shown in PostgreSQL create user.
SELECT line_number, type, database, user_name, address, auth_method, error
FROM pg_hba_file_rules;
SELECT pg_reload_conf();
The Two Usual Errors
- "no pg_hba.conf entry for host": no line matched the client address, role, database, and TLS state named in the message. Add a line or correct its mask, and check that a
hostsslline is not expecting TLS from a client without it. - "Peer authentication failed for user": you connected over the local socket as a system user whose name differs from the role. Add
-h localhostto connect over TCP with a password, or run psql as the matching system user.