Der Server ist erreichbar, aber keine Zeile in pg_hba.conf passt zur Verbindung. Jede Zeile nennt Typ, Datenbank, Rolle, Adresse und Methode – die Meldung nennt genau diese Felder. Passt keine Zeile, ist Schluss, bevor überhaupt ein Passwort geprüft wird.
Symptom
psql -h 172.16.0.182 -U notes_app -d postgres -c 'SELECT 1;' psql: error: connection to server at "172.16.0.182", port 5432 failed: FATAL: no pg_hba.conf entry for host "172.16.0.123", user "notes_app", database "postgres", SSL encryption
Die Rolle darf in die Datenbank notes, nicht in postgres. Die Meldung zählt auf, was der Server gesehen hat: Client-Adresse, Rolle, Datenbank und ob die Verbindung verschlüsselt war.
Diagnose
Die geltenden Regeln aus pg_hba_file_rules lesen und mit den Feldern der Meldung vergleichen.
psql -c "SELECT line_number AS nr, database, user_name, address, auth_method
FROM pg_hba_file_rules WHERE type = 'host';"
nr | database | user_name | address | auth_method
-----+---------------+-------------+------------+---------------
125 | {all} | {all} | 127.0.0.1 | scram-sha-256
127 | {all} | {all} | ::1 | scram-sha-256
131 | {replication} | {all} | 127.0.0.1 | scram-sha-256
132 | {replication} | {all} | ::1 | scram-sha-256
133 | {notes} | {notes_app} | 172.16.0.0 | scram-sha-256
(5 rows)
Ursachen
Kombination fehltRolle, Datenbank oder Netz sind nicht freigegeben
Die Regel lässt genau eine Rolle in genau eine Datenbank, und nur aus dem Labornetz. Jede andere Kombination weist pg_hba.conf ab.
psql -h 172.16.0.182 -U postgres -d notes -c 'SELECT 1;' psql: error: connection to server at "172.16.0.182", port 5432 failed: FATAL: no pg_hba.conf entry for host "172.16.0.123", user "postgres", database "notes", ...
no encryptionhostssl verlangt TLS, der Client kommt ohne
hostssl statt host erzwingt TLS auf Serverseite. Ein Client mit sslmode=disable passt dann auf keine Zeile mehr – die Meldung endet auf „no encryption“.
sudo sed -i 's/^host notes tag03_report /hostssl notes tag03_report /' $H psql "$C sslmode=disable" -Atc 'SELECT 1' psql: error: connection to server at "172.16.0.182", port 5432 failed: FATAL: no pg_hba.conf entry for host "172.16.0.123", user "tag03_report", database "notes", no encryption
not reloadedNeue Zeile nicht geladen oder fehlerhaft
Eine Änderung an pg_hba.conf wirkt erst nach einem Reload. Enthält die Datei einen Fehler, lädt der Server sie gar nicht und behält die alte Fassung. pg_hba_file_rules liest die Datei frisch und zeigt Fehler vor dem Reload.
psql -c "SELECT line_number AS nr, error FROM pg_hba_file_rules WHERE error IS NOT NULL;" nr | error -----+----------------------------------------------- 135 | invalid authentication method "scram-sha-265" (1 row) ... LOG: /etc/postgresql/18/main/pg_hba.conf was not reloaded
Lösung
echo 'host notes notes_app 172.16.0.0/24 scram-sha-256' \ | sudo tee -a /etc/postgresql/18/main/pg_hba.conf psql -Atc 'SELECT count(*) FROM pg_hba_file_rules WHERE error IS NOT NULL' \ -c 'SELECT pg_reload_conf()' 0 t
Nicht tun: Eine Regel wie „all all 0.0.0.0/0“ eintragen. pg_hba.conf gehört so eng wie möglich: eigene Rolle und Datenbank pro Anwendung, nur deren Netz.
PostgreSQL zum Anfassen, Tag 3: Konfiguration und Zugang – pg_hba.conf lesen und ändern, SCRAM statt MD5 und TLS mit eigener CA.
Zu Tag 3 →
Häufige Fragen
Warum steht die Ablehnung zweimal da?
Mit dem Standard sslmode=prefer probiert psql es nach dem TLS-Versuch noch unverschlüsselt. Mit sslmode=require steht sie nur einmal da.
Was ist der Unterschied zu „pg_hba.conf rejects connection“?
Dann hat eine reject-Zeile gepasst. Die erste passende Zeile entscheidet – Zeilen darunter werden nie erreicht.
Reload oder Neustart?
Für pg_hba.conf reicht ein Reload (SELECT pg_reload_conf()). listen_addresses braucht dagegen einen Neustart.