Short answer
“Connection refused” means nothing listens on that address and port. Run pg_lsclusters: if the cluster is down, start it with sudo systemctl restart postgresql; if the port is not 5432, connect to the one shown. From another machine, PostgreSQL also needs listen_addresses = '*' and a pg_hba.conf line for your address.
Tested on Ubuntu 26.04 and 24.04, two separate runs. On 24.04? One section differs, each step marked “On Ubuntu 24.04” right under it: Cause 1: the cluster is not running. Everything that differed
The error
$ psql -h localhost -U app -d appdb -c 'select 1'psql: error: connection to server at "localhost" (::1), port 5432 failed: Connection refused Is the server running on that host and accepting TCP/IP connections?connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused Is the server running on that host and accepting TCP/IP connections?localhost has two addresses, IPv6 ::1 and IPv4 127.0.0.1, and psql tried both. “Connection refused” comes from the operating system: no program listened there. The question below it is psql’s guess. The three usual reasons follow, each made on the test server and fixed.
Cause 1: the cluster is not running
The first thing people check is misleading on Ubuntu:
$ systemctl status postgresql --no-pager | head -n 3● postgresql.service - PostgreSQL RDBMS Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; preset: enabled) Active: active (exited) since Thu 2026-11-19 17:43:32 UTC; 15s agoactive (exited) looks fine, but postgresql.service only groups the clusters and runs nothing itself. The database runs in a cluster, and pg_lsclusters shows each one:
$ pg_lsclustersVer Cluster Port Status Owner Data directory Log file18 main 5432 down postgres /var/lib/postgresql/18/main /var/log/postgresql/postgresql-18-main.logdown. Restarting postgresql restarts every cluster:
$ sudo systemctl restart postgresql$ pg_lsclustersVer Cluster Port Status Owner Data directory Log file18 main 5432 online postgres /var/lib/postgresql/18/main /var/log/postgresql/postgresql-18-main.log$ psql -h localhost -U app -d appdb -c 'select 1' ?column?---------- 1(1 row)If the cluster does not come up, its log (the last column of pg_lsclusters) says why. After a good start it ends like this:
$ sudo tail -n 3 /var/log/postgresql/postgresql-18-main.log2026-11-19 17:43:48.117 UTC [5345] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"2026-11-19 17:43:48.124 UTC [5351] LOG: database system was shut down at 2026-11-19 17:43:45 UTC2026-11-19 17:43:48.130 UTC [5345] LOG: database system is ready to accept connectionsOn Ubuntu 24.04
Ubuntu 24.04 ships PostgreSQL 16, so the cluster is 16 main and its log is postgresql-16-main.log:
$ sudo tail -n 3 /var/log/postgresql/postgresql-16-main.log2026-11-19 17:46:24.377 UTC [4866] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"2026-11-19 17:46:24.384 UTC [4869] LOG: database system was shut down at 2026-11-19 17:46:21 UTC2026-11-19 17:46:24.393 UTC [4866] LOG: database system is ready to accept connectionsCause 2: the cluster listens on another port
The same error, but this time the cluster runs. A second cluster, an upgrade or an edited postgresql.conf can put it on 5433:
$ psql -h localhost -U app -d appdb -c 'select 1'psql: error: connection to server at "localhost" (::1), port 5432 failed: Connection refused Is the server running on that host and accepting TCP/IP connections?connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused Is the server running on that host and accepting TCP/IP connections?$ pg_lsclustersVer Cluster Port Status Owner Data directory Log file18 main 5433 online postgres /var/lib/postgresql/18/main /var/log/postgresql/postgresql-18-main.log$ sudo ss -tlnp | grep postgresLISTEN 0 200 127.0.0.1:5433 0.0.0.0:* users:(("postgres",pid=5700,fd=7))LISTEN 0 200 [::1]:5433 [::]:* users:(("postgres",pid=5700,fd=6))Connect to the port it uses with -p, or set your application to it:
$ psql -h localhost -p 5433 -U app -d appdb -tAc 'select 1'1Or move the cluster back to 5432 (only if no other cluster uses it):
$ sudo sed -i 's/^port = 5433/port = 5432/' /etc/postgresql/*/main/postgresql.conf && sudo systemctl restart postgresqlCause 3: refused from another machine (listen_addresses)
From the server itself everything works, but from another machine the connection is refused. The test connects to a second address of the server, 192.0.2.10, which behaves like your server’s public or private address:
$ psql -h 192.0.2.10 -U app -d appdb -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?$ sudo ss -tlnp 'sport = :5432'State Recv-Q Send-Q Local Address:Port Peer Address:Port ProcessLISTEN 0 200 127.0.0.1:5432 0.0.0.0:* users:(("postgres",pid=6135,fd=7))LISTEN 0 200 [::1]:5432 [::]:* users:(("postgres",pid=6135,fd=6))$ sudo -u postgres psql -tAc "show listen_addresses"localhostPostgreSQL listens only on 127.0.0.1 and ::1, its default. ALTER SYSTEM changes the setting without editing a file whose path holds the version number. It takes effect after a restart, not a reload:
$ sudo -u postgres psql -c "ALTER SYSTEM SET listen_addresses = '*'"ALTER SYSTEM$ sudo systemctl restart postgresql$ sudo ss -tlnp 'sport = :5432'State Recv-Q Send-Q Local Address:Port Peer Address:Port ProcessLISTEN 0 200 0.0.0.0:5432 0.0.0.0:* users:(("postgres",pid=6664,fd=6))LISTEN 0 200 [::]:5432 [::]:* users:(("postgres",pid=6664,fd=7))Now it listens on every address. The connection gets through, and the next error comes from PostgreSQL itself, not the operating system:
$ psql -h 192.0.2.10 -U app -d appdb -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 "app", database "appdb", SSL encryptionconnection to server at "192.0.2.10", port 5432 failed: FATAL: no pg_hba.conf entry for host "192.0.2.10", user "app", database "appdb", no encryptionpg_hba.conf lists who may log in from where, and nothing allowed this address. Add one line: this database, this user, this network, with a password. A reload is enough for pg_hba.conf:
$ echo 'host appdb app 192.0.2.0/24 scram-sha-256' | sudo tee -a /etc/postgresql/*/main/pg_hba.confhost appdb app 192.0.2.0/24 scram-sha-256$ sudo systemctl reload postgresql$ psql -h 192.0.2.10 -U app -d appdb -c 'select 1' ?column?---------- 1(1 row)Use your own client’s address or network in that line, never 0.0.0.0/0. If it is still refused from your machine after these steps, a firewall between the two drops or refuses port 5432.
Troubleshooting
systemctl status postgresqlsays active, but connections are refused: that unit runs nothing; checkpg_lsclusters. Details.pg_lsclusterssays online, still refused: compare the port it shows with the one you use. Details.- Works on the server, refused from elsewhere:
listen_addressesislocalhost. Details. no pg_hba.conf entry for host …: the connection got through; add ahostline and reload. Details.listen_addresseschanged, nothing different: it needs a restart, not a reload. Details.
What these steps changed on the server
Both runs recorded the server before the first step and after the last (see the Tested on box below): listen_addresses in postgresql.auto.conf, one line in pg_hba.conf, and PostgreSQL listening on every address.
Ubuntu 26.04 vs 24.04
The same check ran on a fresh 26.04 server and on a fresh 24.04 server, each with its own run log. Every error and fix was the same on both. Only the version differed:
| What | Ubuntu 26.04 | Ubuntu 24.04 |
|---|---|---|
| System | Ubuntu 26.04.1 LTS7.0.0-43-generic | Ubuntu 24.04.5 LTS6.8.0-147-generic |
| postgresql | 18+290ubuntu1 | 16+257build1.1 |
| postgresql-common | 290ubuntu1 | 257build1.1 |
| postgresql-18 | 18.6-0ubuntu0.26.04.1 | not installed |
| postgresql-client-18 | 18.6-0ubuntu0.26.04.1 | not installed |
| postgresql-16 | not installed | 16.15-0ubuntu0.24.04.1 |
| postgresql-client-16 | not installed | 16.15-0ubuntu0.24.04.1 |
| PostgreSQL version, cluster and log file | PostgreSQL 18, 18 main, postgresql-18-main.log | PostgreSQL 16, 16 main, postgresql-16-main.log |
| Proof | Run log, 28 checks passed | Run log, 28 checks passed |
