Skip to content
Log In Sign Up info@muvcloud.com

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
FieldValues
TYPElocal, host, hostssl, hostnossl
DATABASEA name, all, replication, and more
USERA role, all, or +group
ADDRESSIP with mask; none for local
METHODHow 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_encryption is scram-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 host line.
  • 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:

  1. Copy the file: sudo cp pg_hba.conf pg_hba.conf.bak in its folder.
  2. Keep a psql session as postgres open while you edit.
  3. Check the file as written on disk with the pg_hba_file_rules view, shown below. Fix any row with an error.
  4. Reload, do not restart: open sessions stay connected, new ones use the new rules.
  5. 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 hostssl line 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 localhost to connect over TCP with a password, or run psql as the matching system user.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations