Fix PostgreSQL "Connection refused" on 5432 (listen_addresses)
psql Connection refused: nothing listens on that address and port. Check the server, port and listen_addresses, restart, then open pg_hba.conf.
psql: error: connection to server at "127.0.0.1", port 5432 failed: Connection refused Is the server running on that host and accepting TCP/IP connections?No process accepts connections on that address and port, so the operating system refused at once. PostgreSQL authentication was never reached, so passwords and pg_hba.conf don't matter yet. Check that the server is up, that the port is right, and that listen_addresses covers the address you connect to. The PostgreSQL default, localhost, accepts only loopback connections from the same machine.
[1] Check that the server is running. With the server down, TCP and socket connections fail with different messages:
$ psql -h 127.0.0.1 -U postgres -c 'SELECT 1'psql: error: connection to server at "127.0.0.1", port 5432 failed: Connection refused Is the server running on that host and accepting TCP/IP connections?$ psql -U postgres -c 'SELECT 1'psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory Is the server running locally and accepting connections on that socket?If the socket also says No such file or directory on the server machine, the server is down or uses another socket directory — check the service status and server log first. If the socket works and only TCP is refused, read on.
[2] Check the port. Connecting to the wrong port gives the same message, with the port you tried printed in it.
$ psql -h 127.0.0.1 -U postgres -p 5433 -c 'SELECT 1'psql: error: connection to server at "127.0.0.1", port 5433 failed: Connection refused Is the server running on that host and accepting TCP/IP connections?$ psql -h 127.0.0.1 -U postgres -Atc 'SHOW port'5432Match the client's port to the server's SHOW port.
[3] See which addresses the server listens on, from the server machine.
$ ss -ltnState Recv-Q Send-Q Local Address:Port Peer Address:PortLISTEN 0 200 127.0.0.1:5432 0.0.0.0:* LISTEN 0 200 [::1]:5432 [::]:* $ psql -h 127.0.0.1 -U postgres -Atc 'SHOW listen_addresses'localhost$ psql -h 192.0.2.10 -U postgres -c 'SELECT 1'psql: error: connection to server at "192.0.2.10", port 5432 failed: Connection refused Is the server running on that host and accepting TCP/IP connections?With only 127.0.0.1:5432 and [::1]:5432, connections to any other address are refused — even from the same machine to its own IP.
[4] Change listen_addresses. Use 'localhost,192.0.2.10' for specific addresses or '*' for all. ALTER SYSTEM writes to postgresql.auto.conf, which overrides postgresql.conf.
$ psql -h 127.0.0.1 -U postgres -c "ALTER SYSTEM SET listen_addresses = '*'"ALTER SYSTEM$ psql -h 127.0.0.1 -U postgres -Atc 'SELECT pg_reload_conf()'t$ psql -h 127.0.0.1 -U postgres -c "SELECT name, setting, pending_restart FROM pg_settings WHERE name = 'listen_addresses'" name | setting | pending_restart ------------------+-----------+----------------- listen_addresses | localhost | t(1 row)A reload does not change it: pending_restart is t and the value is still localhost. The docs say this parameter can only be set at server start.
[5] Restart the server and check the listening addresses again. A restart drops every connection, so pick a quiet moment. Package installs usually restart through the service manager (systemctl restart postgresql or similar); below, the server was started directly with pg_ctl.
$ su postgres -c 'pg_ctl -D /var/lib/postgresql/17/main -l /tmp/pg.log -w restart'waiting for server to shut down.... doneserver stoppedwaiting for server to start.... doneserver started$ ss -ltnState Recv-Q Send-Q Local Address:Port Peer Address:PortLISTEN 0 200 0.0.0.0:5432 0.0.0.0:* LISTEN 0 200 [::]:5432 [::]:* $ psql -h 192.0.2.10 -U postgres -c 'SELECT 1'psql: error: connection to server at "192.0.2.10", port 5432 failed: FATAL: no pg_hba.conf entry for host "192.0.2.10", user "postgres", database "postgres", no encryptionThe error is now no pg_hba.conf entry, not Connection refused. The connection arrived and the authentication rules have no line for that address — one step further.
[6] Add that address (or range) to pg_hba.conf and reload. This file takes effect on reload, no restart. A scram-sha-256 rule is password authentication, so the role you connect as must already have a password. Every TCP connection in this reproduction ran with the postgres role's test password (set at initdb) supplied through the PGPASSWORD environment variable. When typing it yourself, enter it at the password prompt (psql -W) or use a ~/.pgpass file with mode 600.
$ tail -1 /var/lib/postgresql/17/main/pg_hba.confhost all all 192.0.2.10/32 scram-sha-256$ psql -h 127.0.0.1 -U postgres -Atc 'SELECT pg_reload_conf()'t$ psql -h 192.0.2.10 -U postgres -c 'SELECT inet_server_addr(), inet_client_addr()' inet_server_addr | inet_client_addr ------------------+------------------ 192.0.2.10 | 192.0.2.10(1 row)In practice, list your clients' range (for example 203.0.113.0/24). Avoid 0.0.0.0/0, which invites password guessing from anywhere. Peer authentication failed on socket connections is a different problem — see Fix PostgreSQL Peer authentication failed.
[7] If the value is not what you set, find where it comes from. Editing postgresql.conf loses to an ALTER SYSTEM value left in postgresql.auto.conf.
$ grep -n listen_addresses /var/lib/postgresql/17/main/postgresql.conf /var/lib/postgresql/17/main/postgresql.auto.conf/var/lib/postgresql/17/main/postgresql.conf:60:listen_addresses = 'localhost'/var/lib/postgresql/17/main/postgresql.auto.conf:3:listen_addresses = '*'$ psql -h 127.0.0.1 -U postgres -c "SELECT setting, source, sourcefile FROM pg_settings WHERE name = 'listen_addresses'" setting | source | sourcefile ---------+--------------------+-------------------------------------------------- * | configuration file | /var/lib/postgresql/17/main/postgresql.auto.conf(1 row)Official sources: listen_addresses · port · ALTER SYSTEM · pg_settings · pg_hba.conf · docker-library/postgres Dockerfile
Check it with the pg_hba.conf rule checker tool ↗ — calculated in your browser.