GrepBoard/Manuals
605 manuals ·

Which pg_hba.conf line matched? Find it with log_connections

·Tested on PostgreSQL 17.11 (Docker postgres:17) · 2026-10·Test scope ↓·7 steps

With log_connections on, PostgreSQL 17 logs the pg_hba.conf line number even for successful logins. Failures show line N in DETAIL; reject lines show no number.

output
LOG:  connection authenticated: identity="alice" method=scram-sha-256 (/var/lib/postgresql/data/pg_hba.conf:4)

The :4 at the end is the pg_hba.conf line this connection matched. With log_connections on, PostgreSQL 17.11 logged this line for successful connections, and failed authentications carried Connection matched file … line N in DETAIL. Only connections that hit a reject line get no number, and for those you walk the file from the top.

To see the matching line without touching a server, paste the file into the pg_hba.conf checker.

[1] Turn on log_connections. It defaults to off and is fixed at session start, so after a reload it applies to new connections only. No restart needed.

console
postgres=# ALTER SYSTEM SET log_connections = on;postgres=# SELECT pg_reload_conf();

On a busy server this adds several log lines per connection. When you are done, revert with ALTER SYSTEM RESET log_connections; and pg_reload_conf().

[2] Read the connection authenticated line of a successful login. It comes in two shapes:

output
LOG:  connection authenticated: identity="alice" method=scram-sha-256 (/var/lib/postgresql/data/pg_hba.conf:4)LOG:  connection authenticated: user="postgres" method=trust (/var/lib/postgresql/data/pg_hba.conf:7)

Methods that verify an identity (passwords, peer) log identity="…"; trust logs user="…". Both end in (file:line), so one grep 'connection authenticated' catches them. The log_connections docs don't describe this line; the format lives in the source (auth.c).

[3] For failed logins, search for Connection matched. DETAIL: can span two lines, so grepping for DETAIL: Connection matched misses some. Here is a wrong password:

output
FATAL:  password authentication failed for user "alice"DETAIL:  Password does not match for user "alice".	Connection matched file "/var/lib/postgresql/data/pg_hba.conf" line 2: "host    all    all    127.0.0.1/32            password   "

The second line starts with a tab and carries no log_line_prefix (here, the PID). Filtering by PID, as in grep '^2330|', drops it. Use grep -n 'Connection matched' and read the line above. The quoted text is the raw line from the file, trailing spaces included. What the other DETAIL sentences mean is tabulated in psql password authentication failed.

[4] Remember that a failed authentication stops at that line. The server does not try the next one. Peer failures are confusing because an authenticated line comes first:

output
LOG:  connection authenticated: identity="postgres" method=peer (/var/lib/postgresql/data/pg_hba.conf:3)LOG:  provided user name (alice) and authenticated user name (postgres) do not matchFATAL:  Peer authentication failed for user "alice"DETAIL:  Connection matched file "/var/lib/postgresql/data/pg_hba.conf" line 3: "local   all             all                                     peer"

authenticated only means the OS user postgres was identified; it did not match the requested role alice, so the login failed. Judge the outcome by the FATAL line. Fixes for this case are in PostgreSQL Peer authentication failed.

A local line written as ident shows up as peer in the log, because, per the docs, ident on a local socket is handled as peer:

run/set-05.conf
…local         all   dave                      ident…
output
LOG:  connection authenticated: identity="postgres" method=peer (/var/lib/postgresql/data/pg_hba.conf:8)

[5] If the connection hit reject, the log has no line number. Compare against the file from the top:

output
FATAL:  pg_hba.conf rejects connection for host "192.0.2.11", user "carol", database "app", no encryption
run/set-02.conf
# order · reject · +group · sameuser · samerolehost    app       +admins     192.0.2.0/24     scram-sha-256host    app       all         192.0.2.11/32    reject…

From the same 192.0.2.11, alice (a member of admins) matched line 2 and got in, while carol did not match line 2 and stopped at line 3, reject. Per the docs, a reject line is still the first match, so nothing below it is considered. To compare, run SELECT rule_number, file_name, line_number, type, database, user_name, address, auth_method FROM pg_hba_file_rules ORDER BY rule_number; to see how each line splits into fields (rule_number is the evaluation order; file_name tells you which file when there are includes), and find the first line where connection type, database, user, and address all match. Note that this view parses the file as it is on disk now — it does not show the rules the server is actually using. After an edit and before a reload, or after a failed reload, it can differ from what was in force when the connection was rejected, so first make sure the file has not changed since (pg_hba.conf changes not applied). When the address family (127.0.0.1 vs ::1) sends you to an unexpected line, see no pg_hba.conf entry for host "::1".

[6] If the number doesn't seem to match the file, look for line continuations (\). A record spread over several lines was logged with its first line number:

run/set-06.conf
…host    all \        alice \        192.0.2.10/32    md5…
output
LOG:  connection authenticated: identity="alice" method=md5 (/var/lib/postgresql/data/pg_hba.conf:4)

The record spans lines 4 to 6; the log says :4.

[7] Treat method= as the method name written on the line, not necessarily what was exchanged. alice in [6] has a password stored under the default password_encryption = scram-sha-256, and the docs say an md5 line uses SCRAM automatically when the stored password is SCRAM. The log still said method=md5. Check how a password is stored with SELECT rolname, left(rolpassword, 14) FROM pg_authid; (SCRAM-SHA-256$ or md5…).

Official sources: pg_hba.conf · log_connections · pg_settings: when changes apply · pg_hba_file_rules · Password Authentication · auth.c (REL_17_STABLE)

Check it with the pg_hba.conf rule checker tool ↗ — calculated in your browser.

#postgresql#pg_hba#log_connections#authentication#logging