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

Server
$ 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?
Output hidden (4 lines)

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:

Server
$ 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 ago
Output hidden (3 lines)

active (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:

Server
$ 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.log
Output hidden (2 lines)

down. Restarting postgresql restarts every cluster:

Server
$ sudo systemctl restart postgresql
Server
$ 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
Output hidden (2 lines)
Server
$ psql -h localhost -U app -d appdb -c 'select 1' ?column?----------        1(1 row)
Output hidden (4 lines)

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:

Server
$ 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 connections
Output hidden (3 lines)
On Ubuntu 24.04

Ubuntu 24.04 ships PostgreSQL 16, so the cluster is 16 main and its log is postgresql-16-main.log:

Server
$ 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 connections
Output hidden (3 lines)

Cause 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:

Server
$ 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?
Output hidden (4 lines)
Server
$ 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
Output hidden (2 lines)
Server
$ 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))
Output hidden (2 lines)

Connect to the port it uses with -p, or set your application to it:

Server
$ psql -h localhost -p 5433 -U app -d appdb -tAc 'select 1'1
Output hidden (1 line)

Or move the cluster back to 5432 (only if no other cluster uses it):

Server
$ sudo sed -i 's/^port = 5433/port = 5432/' /etc/postgresql/*/main/postgresql.conf && sudo systemctl restart postgresql

Cause 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:

Server
$ 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?
Output hidden (2 lines)
Server
$ 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))
Output hidden (3 lines)
Server
$ sudo -u postgres psql -tAc "show listen_addresses"localhost
Output hidden (1 line)

PostgreSQL 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:

Server
$ sudo -u postgres psql -c "ALTER SYSTEM SET listen_addresses = '*'"ALTER SYSTEM
Output hidden (1 line)
Server
$ sudo systemctl restart postgresql
Server
$ 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))
Output hidden (3 lines)

Now it listens on every address. The connection gets through, and the next error comes from PostgreSQL itself, not the operating system:

Server
$ 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 encryption
Output hidden (2 lines)

pg_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:

Server
$ 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
Output hidden (1 line)
Server
$ sudo systemctl reload postgresql
Server
$ psql -h 192.0.2.10 -U app -d appdb -c 'select 1' ?column?----------        1(1 row)
Output hidden (4 lines)

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 postgresql says active, but connections are refused: that unit runs nothing; check pg_lsclusters. Details.
  • pg_lsclusters says online, still refused: compare the port it shows with the one you use. Details.
  • Works on the server, refused from elsewhere: listen_addresses is localhost. Details.
  • no pg_hba.conf entry for host …: the connection got through; add a host line and reload. Details.
  • listen_addresses changed, 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:

WhatUbuntu 26.04Ubuntu 24.04
SystemUbuntu 26.04.1 LTS7.0.0-43-genericUbuntu 24.04.5 LTS6.8.0-147-generic
postgresql18+290ubuntu116+257build1.1
postgresql-common290ubuntu1257build1.1
postgresql-1818.6-0ubuntu0.26.04.1not installed
postgresql-client-1818.6-0ubuntu0.26.04.1not installed
postgresql-16not installed16.15-0ubuntu0.24.04.1
postgresql-client-16not installed16.15-0ubuntu0.24.04.1
PostgreSQL version, cluster and log filePostgreSQL 18, 18 main, postgresql-18-main.logPostgreSQL 16, 16 main, postgresql-16-main.log
ProofRun log, 28 checks passedRun log, 28 checks passed