Skip to main content
RunBook Academy

PostgreSQLV · Authentication, Roles and TLSAuthentication

Authentication methods and what each one proves

Intermediate⏱ ~25 minpsql

What you'll learn

  • State what each common authentication method actually verifies
  • Choose a method appropriate to where the connection comes from
  • Explain why peer authentication works locally and cannot work remotely
  • Identify the external dependencies a method introduces

Prerequisites

Verified against PostgreSQL 18.x · PostgreSQL (comparison targets) 17.11, 16.15 · PostgreSQL (support calendar) 18, 17, 16, 15, 14 supported · pgBackRest 2.59.1 · PgBouncer 1.25.2 · Patroni 4.1.5 · Ubuntu (host baseline) 26.04 LTS · 2026-08-27

Not yet marked complete on this device.

The METHOD column decides what the server will accept as evidence of identity. Reading it as “how secure is this” is less useful than reading it as “what exactly is being proved, and by whom”.

What each one proves

MethodProvesDepends on
trustNothing. The claimed role is acceptedNetwork reachability alone
rejectNothing; refuses unconditionally
peerThe OS account owning the client socketUnix socket, local only
scram-sha-256Knowledge of a password, without sending itThe stored verifier
md5Knowledge of a password, weakly. Deprecated in 18The stored hash
certPossession of a private key matching a trusted certificateTLS, and a CA you trust
ldapThe directory accepted the credentialAn external directory being up
gss / sspiA Kerberos ticketA KDC being up
oauthA token a validator accepted. New in 18An identity provider being up

Two columns matter equally. The middle one is the security question. The right-hand one is the availability question, and it is the one that produces incidents: an authentication method with an external dependency means your database is unreachable whenever that dependency is.

trust, and where it is legitimate

trust accepts the connection as whatever role it claims to be, with no evidence at all. Any client that can reach the port can connect as any role, including superusers.

It has exactly two legitimate uses.

A disposable local cluster, which is why the official container image ships it for loopback and the Unix socket, and why every lab in this course relies on it.

Single-user recovery, where the server is not accepting network connections at all and you are repairing something.

peer, and why it is local-only

peer asks the kernel which operating-system user owns the other end of the Unix socket, and requires that name to match the PostgreSQL role being requested. The evidence comes from the kernel, cannot be forged by the client, and needs no password.

It is the right default for the local line, and it is why the Debian packaging ships exactly that:

Read-only / Safethe pg_hba.conf a Debian-packaged cluster starts with
$ grep -vE '^#|^$' /etc/postgresql/18/reporting/pg_hba.conf
local   all             postgres                                peer
local   all             all                                     peer
host    all             all             127.0.0.1/32            scram-sha-256
host    all             all             ::1/128                 scram-sha-256
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            scram-sha-256
host    replication     all             ::1/128                 scram-sha-256

peer cannot work over TCP, because there is no operating-system identity attached to a network connection. A host line with peer is a configuration error, and the server will tell you so.

The name-matching requirement is strict by default: OS user deploy can only connect as PostgreSQL role deploy. pg_ident.conf provides a mapping when that is too rigid:

# pg_ident.conf: MAPNAME  SYSTEM-USERNAME  PG-USERNAME
ops    deploy      app_migrator
ops    ansible     app_migrator
# pg_hba.conf
local   all   app_migrator   peer  map=ops

That says: the OS accounts deploy and ansible may connect as the PostgreSQL role app_migrator, and nobody else may.

The password methods

scram-sha-256 is the current standard and the default for password_encryption in every supported version. The next lesson covers how it works and why the distinction from md5 matters more than it looks.

password sends the password in clear over the connection. It is acceptable only inside TLS, and since scram-sha-256 works everywhere password does, there is essentially no remaining reason to choose it.

md5 is deprecated in PostgreSQL 18, which emits a warning when a password is stored with it and announces removal in a future release.

cert

cert requires a client certificate and treats a successful TLS client-certificate validation as the authentication. It implies hostssl, and the certificate’s Common Name must match the requested role unless a map says otherwise.

Its strength is that no password exists to leak, rotate or embed in a configuration file. Its cost is a certificate lifecycle: issuance, distribution, expiry monitoring and revocation, all of which become your problem. A client certificate that expires unnoticed is an outage, and it is exactly the failure the Secrets and PKI course is built around.

Choosing

A serviceable default for a self-managed estate:

# Local administration, authenticated by the operating system
local   all   postgres                     peer
local   all   all                          peer

# Applications over the network, encrypted and password-authenticated
hostssl all   +app_roles   10.0.0.0/8      scram-sha-256

# Replication, from known standbys only
hostssl replication  replicator  10.0.1.0/24  scram-sha-256

# Everything else
host    all   all          all             reject

The final explicit reject is optional — the absence of a matching rule already refuses — but it makes the intent visible and produces the clearer of the two error messages from the previous lesson.

Production discipline

  1. Read the method as “what is being proved”, not “how secure”. trust proves nothing; peer proves an OS account; scram-sha-256 proves knowledge of a password.
  2. Use peer rather than trust on the local line, including on hosts you consider private.
  3. Prefer hostssl over host for anything crossing a network, so a rule cannot match an unencrypted connection.
  4. Treat every external method as an availability dependency and keep a locally-authenticated break-glass role.
  5. Measure the authentication component in the connection log before blaming the database for connection latency.
  6. Never use password. scram-sha-256 works everywhere it does and does not transmit the secret.

Cross-course references

  • Secrets, PKI & Certificate Management — Part V (X.509) and Part IX (Certificate lifecycle) cover the client-certificate lifecycle that cert authentication makes your responsibility.
  • Linux for Production Sysadmins — Part XXVII (Auth) covers PAM, SSSD and directory integration, which is the same dependency discussed here from the host side.
  • Observability for Production Sysadmins — Part XI (Blackbox) covers probing an authentication path end to end rather than probing only the port.

Quiz

Knowledge check · 6 questions

  1. Q1. A host line in pg_hba.conf specifies the peer method. What happens?

  2. Q2. Why is trust on the local socket not automatically safe on a host that also runs application services?

  3. Q3. Which authentication methods introduce a dependency on a system outside the database? Select all that apply.

  4. Q4. cert authentication removes the need to manage credentials, because no password exists.

  5. Q5. An application opening a connection per request reports high latency after moving to LDAP authentication. Name the log field that would confirm the cause and the change that removes the problem.

  6. Q6. Diagnose the outage and state what should have been in place.

    At 09:40 a corporate directory upgrade begins. At 09:52 every PostgreSQL cluster in the estate stops accepting new connections, while existing sessions continue working normally. The database hosts are healthy, disks are fine, and pg_stat_activity on the existing sessions shows nothing unusual. The database team is paged and spends thirty minutes investigating the databases. All clusters use ldap in pg_hba.conf for every non-local role. There is no locally-authenticated administrative account other than one whose password nobody can locate.

Passing score: 75%. Answers are checked in this browser.