pg_hba.conf and PostgreSQL authentication methods
pg_hba.conf decides who may connect from where and how they must prove who they are. Rules are checked top to bottom and the first match wins.
Not checked by a trusted editor yet Last edited Outdated?
Every connection to PostgreSQL passes through host-based authentication, configured in the file pg_hba.conf. It answers one question: for this connection type, database, user and client address, which authentication method applies? If no line matches, the connection is refused.
The file lives in the data directory by default; Debian and Ubuntu keep it in /etc/postgresql/<version>/main/. Ask the server where its file is with SHOW hba_file;.
The format of a rule
Each non-comment line is one record with these fields: type, database, user, address (not for local), method and optional options.
# TYPE DATABASE USER ADDRESS METHOD
local all postgres peer
local all all scram-sha-256
host appdb appuser 10.0.0.0/24 scram-sha-256
hostssl all all 192.0.2.0/24 scram-sha-256
host replication replicator 10.0.0.12/32 scram-sha-256
host all all 0.0.0.0/0 reject
- TYPE:
local(Unix-domain socket),host(TCP, with or without SSL),hostssl(TCP with SSL only),hostnossl(TCP without SSL only), plushostgssenc/hostnogssencfor GSSAPI encryption. - DATABASE: a name,
all,sameuser, a comma-separated list, or the keywordreplication, which matches physical replication connections only.alldoes not match replication connections. - USER: a role name,
all, or+groupnamefor members of a role. - ADDRESS: a CIDR range such as
10.0.0.0/24, a single host as/32, a host name, orsamehost/samenet. - METHOD: how the client must authenticate (see below).
First match wins, no fall-through
Rules are read top to bottom. The first line that matches the connection type, client address, database and user is used, and only that line. If its authentication fails, PostgreSQL does not try later lines. That is why specific rules and reject rules go near the top and broad catch-all rules go at the bottom.
Passing pg_hba.conf is not the whole story: the role also needs the CONNECT privilege on the database, and then normal object privileges.
Authentication methods
scram-sha-256: challenge-response password authentication. It never sends the password over the wire and is the most secure of the built-in password methods. The defaultpassword_encryptionisscram-sha-256.md5: older challenge-response method. If the stored password is already a SCRAM hash, the server automatically uses SCRAM. MD5 password support is deprecated and planned for removal, so migrate to SCRAM.password: sends the password in clear text. Avoid it except over SSL.peer: for local socket connections only. The server asks the kernel for the client’s OS user name and requires it to match the database user (or a mapping inpg_ident.conf). This is whysudo -u postgres psqlworks without a password.ident: the TCP equivalent of peer, using an ident server on the client. Rarely a good idea today.cert: SSL client certificate authentication.ldap,radius,pam,gss,sspi: delegate to an external system.trust: anyone who matches the line may connect as any role it names, with no password. Use it only on single-user test machines.reject: always refuse. Useful to block a range before a broader allow rule.
Moving from md5 to scram-sha-256
- Make sure all client drivers support SCRAM.
- Set
password_encryption = 'scram-sha-256'(already the default in recent versions). - Have every user set their password again, for example with
\passwordin psql. Only a newly set password is stored as a SCRAM hash. - Change the method in
pg_hba.conffrommd5toscram-sha-256and reload.
Testing and applying changes
Changes to pg_hba.conf need a reload, not a restart. Before reloading, the pg_hba_file_rules view shows how the server parses the file on disk; rows with a non-empty error column point at broken lines.
SELECT line_number, type, database, user_name, address, auth_method, error
FROM pg_hba_file_rules;
SELECT pg_reload_conf();
From the shell you can also run sudo systemctl reload postgresql or pg_ctl reload. When a connection is refused, the server log usually has more detail than the message the client sees.
Official documentation
See also
- 0198 Install PostgreSQL and connect with psql on Debian, Ubuntu and RHEL Install the PostgreSQL server from your distribution or the official PGDG repositories, start it, and open your first psql session as the… Beginner · How-to · Linux Article Install & connect 15 Oct 2026
- 0199 psql meta-commands cheat sheet The backslash commands you use every day in psql: connecting, listing databases, tables and roles, describing objects, timing queries, expanded output and… Beginner · Reference · Any system Article Install & connect · Roles & administration 15 Oct 2026
- 0200 PostgreSQL roles, privileges and default privileges Create login and group roles, grant access to databases, schemas and tables, and use ALTER DEFAULT PRIVILEGES so tables created later get… Intermediate · How-to · Linux Article Roles & administration · Security & access 15 Oct 2026
- 0209 Fix: FATAL: password authentication failed for user PostgreSQL reached the server and found a matching pg_hba.conf rule, but the password check failed. Usually a wrong password, a role without… Intermediate · Fix · Any system Article Roles & administration · Security & access 15 Oct 2026
- 0210 Fix: FATAL: no pg_hba.conf entry for host The server received the connection but no pg_hba.conf rule matches this client address, user, database and SSL mode. Add or correct a… Intermediate · Fix · Linux Article Install & connect · Security & access 15 Oct 2026
- 0211 Fix: psql connection to server on socket /var/run/postgresql/.s.PGSQL.5432 failed psql tried the local Unix socket and found nothing there. The server is not running, listens on another port, or uses a… Beginner · Fix · Linux Article Install & connect 15 Oct 2026
Repos in the wild
- postgres/postgres: PostgreSQL source code mirror postgres/postgres The official read-only GitHub mirror of the PostgreSQL source repository. Browse the code and history here; patches go through the project's mailing lists, not pull requests. GitHub
History
Every saved edit is kept. The plot on the sleeve draws one line per edit. Open any edit to see exactly what changed.
- Commitpedia +4,406 chars