Fix no pg_hba.conf entry for host "::1" (localhost is IPv6)
no pg_hba.conf entry for host "::1": localhost went over IPv6 but pg_hba.conf only allows 127.0.0.1/32. Add a ::1/128 line or use -h 127.0.0.1.
psql: error: connection to server at "::1", port 5432 failed: FATAL: pg_hba.conf rejects connection for host "::1", user "alice", database "app", no encryptionpsql: error: connection to server at "2001:db8::2", port 5432 failed: FATAL: no pg_hba.conf entry for host "2001:db8::10", user "alice", database "app", no encryptionhost "::1" means the client arrived over the IPv6 loopback, not IPv4 127.0.0.1. The rest of the message tells two cases apart. pg_hba.conf rejects connection (the first line) means a reject line matched on connection type, database, user and address; no pg_hba.conf entry means no line matched on all four. One possible cause of no match is a file with a 127.0.0.1/32 line and no ::1/128 line — but a database, user or connection type (host/hostssl) mismatch gives the same message, so check the file. If the IPv4-only file is the cause, add a matching ::1/128 line, or pin the client to -h 127.0.0.1. Both messages come from the same spot in the PostgreSQL source (auth.c), and the quoted value is always the numeric client address, never a host name.
To see which line a connection hits, paste your file into the pg_hba.conf checker.
[1] Check the client address in the server log. With log_connections = on, every attempt logs connection received: host=… (how to turn it on: Which pg_hba.conf line matched). This file allows only the IPv4 loopback:
# IPv4 loopback only · IPv6 rangeshost all all 127.0.0.1/32 trusthost all all 2001:db8::10/128 md5host all all 2001:db8::/64 passwordhost all all ::0/0 rejectlocal all all rejectSame machine, same user alice, two connections. Only the address family differs:
LOG: connection received: host=127.0.0.1 port=50858LOG: connection authenticated: user="alice" method=trust (/var/lib/postgresql/data/pg_hba.conf:2)LOG: connection received: host=::1 port=46254FATAL: pg_hba.conf rejects connection for host "::1", user "alice", database "app", no encryptionThe ::1 connection skipped line 2 (127.0.0.1/32) and fell to line 5, ::0/0 reject. The docs state it plainly: an entry in IPv4 format matches only IPv4 connections, and an IPv6 entry matches only IPv6 connections.
[2] Find out why localhost becomes ::1. When a host name resolves to several addresses, libpq tries them in the order the OS returns. Run getent ahosts localhost on the client: if ::1 is listed before 127.0.0.1, that machine's localhost connections go to ::1. Per the libpq docs, if a connection is established but authentication fails, the remaining addresses are not tried. A rejection on ::1 never falls back to 127.0.0.1.
[3] Pair the IPv4 line with a ::1/128 line. This is the usual fix:
# TYPE DATABASE USER ADDRESS METHOD…host all all 127.0.0.1/32 scram-sha-256host all all ::1/128 scram-sha-256…LOG: connection received: host=127.0.0.1 port=39038LOG: connection authenticated: identity="alice" method=scram-sha-256 (/var/lib/postgresql/data/pg_hba.conf:4)LOG: connection received: host=::1 port=41104LOG: connection authenticated: identity="alice" method=scram-sha-256 (/var/lib/postgresql/data/pg_hba.conf:5)Each connection matched its own line, 4 and 5. Then run SELECT pg_reload_conf(); and confirm in the server log that the file was taken. One bad line makes the server ignore the whole file: pg_hba.conf changes not applied after reload.
[4] Use samehost if you want one line for both. It matches any of the server's own IP addresses, so both loopbacks hit the same line:
…host all dave samehost scram-sha-256…LOG: connection received: host=127.0.0.1 port=51678LOG: connection authenticated: identity="dave" method=scram-sha-256 (/var/lib/postgresql/data/pg_hba.conf:4)LOG: connection received: host=::1 port=45260LOG: connection authenticated: identity="dave" method=scram-sha-256 (/var/lib/postgresql/data/pg_hba.conf:4)[5] If you cannot edit the server file, pin the address on the client. psql -h 127.0.0.1 … or a connection string with host=127.0.0.1 (or hostaddr=127.0.0.1) skips name resolution and always uses IPv4. In application settings, that means replacing localhost with 127.0.0.1.
[6] Don't try to cover IPv4 clients with an IPv4-mapped ::ffff: entry. Line 8 below did not match the IPv4 client 192.0.2.10:
…host all carol ::ffff:192.0.2.10/128 md5host all carol 0.0.0.0/0 password…LOG: connection received: host=192.0.2.10 port=55660LOG: connection authenticated: identity="carol" method=password (/var/lib/postgresql/data/pg_hba.conf:9)The connection landed on line 9 (0.0.0.0/0), not line 8 (md5). The docs say an IPv6-format entry matches only IPv6 connections even when the address is in the IPv4-in-IPv6 range. Write IPv4 clients in IPv4 form.
A missing IPv6 address gives the same message. The second line at the top came from 2001:db8::10 against a file whose only host lines were 127.0.0.1/32 and ::1/128. Add that address or its range (2001:db8::/64) in IPv6 form.
If the connection never gets that far (Connection refused), the problem is before authentication: PostgreSQL 5432 Connection refused. If a line matches but the password is refused, see psql password authentication failed.
Official sources: pg_hba.conf · libpq multiple hosts and addresses · log_connections · auth.c (REL_17_STABLE)
Check it with the pg_hba.conf rule checker tool ↗ — calculated in your browser.