Skip to the workspace
Pebellslearn by building
Security  ›  Capstone Project

pg_hba.conf and Authentication

pg_hba.conf 15 minpg_hba.conf is read top to bottom and the first line matching a connection's type, database, user and address decides its authentication method; no match means rejection.

postgresql dba / pg_hba.conf and Authentication

Explore each scene at your own pace.

01 · The rule

The rule

pg_hba.conf and Authentication

Pedal's new reporting server cannot connect: pg_hba.conf rejects connection for host "10.0.2.14", user "asha", database "pedal", no encryption. The role exists, the password is right, the grants are right. Before PostgreSQL checks any of that, it asks a different question: is this kind of connection allowed in at all, and how must it prove who it is?

The rule

Top to bottom, first match wins; no match, no entry.

pg_hba.conf (host-based authentication) is a list of rules. For each new connection PostgreSQL reads the rules from the top and uses the first line whose type, database, user and address all match. That line's method decides how the client authenticates. If authentication fails, it does not try the next line. If no line matches, the connection is rejected.

# TYPE    DATABASE     USER         ADDRESS         METHOD
local     all          postgres                     peer
hostssl   pedal        pedal_app    10.0.1.0/24     scram-sha-256
hostssl   pedal        +analysts    10.0.2.0/24     scram-sha-256
host      replication  replicator   10.0.9.5/32     scram-sha-256
host      all          all          0.0.0.0/0       reject

The columns:

  • TYPE: local for Unix-socket connections on the same machine, host for TCP with or without encryption, hostssl for TCP with TLS only.
  • DATABASE: a name, all, or replication for streaming-replication connections.
  • USER: a role, all, or +group for every member of a group role.
  • ADDRESS: an IP range in CIDR form, such as 10.0.2.0/24.
  • METHOD: how to authenticate.

The methods a DBA uses most:

Method Meaning
scram-sha-256 Password check that never sends the password itself; the modern default
peer Local only: the OS user name must equal the role name
cert The client presents a TLS certificate
reject Refuse outright
trust Let anyone in without checking. Never on a shared server
md5 Older password method, deprecated; move to SCRAM

Two more things to know. Edits take effect after a reload, and SELECT * FROM pg_hba_file_rules; shows how the server parsed the file, including errors. And pg_hba.conf only matters for connections that reach the server: listen_addresses in postgresql.conf (default localhost) decides which network interfaces it listens on at all, and changing it needs a restart.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.