How to Fix PostgreSQL 'Could Not Connect to Server'
'Could not connect to server: Connection refused' means your connection never reached the password. On a real Ubuntu 24.04 server with PostgreSQL 16.15 we find each place it stops: a server that isn't running (while systemctl calls it active), PostgreSQL listening on localhost only, a reload that changes nothing, a pg_hba.conf line that's missing, and a firewall that turns the refusal into a timeout.
Refused before the password
Your app says it could not connect to the PostgreSQL server. You restarted the app. Same error. An ice rink's booking site should list today's sessions. Instead: connection refused. The good news: the password is fine. The server never got far enough to ask for it.

Where a connection can stop
So where can a connection stop? Your app finds the server's address, then knocks on port five four three two, PostgreSQL's usual port. On the way, a firewall can drop the knock, and the app just waits, sometimes for minutes. If nothing listens on that port, the server's machine says no at once: refused. If PostgreSQL does listen, its rules file, pg_hba.conf, decides whether this address may log in, and only then comes the password.

First, take the app out of it. From the booking server, psql, PostgreSQL's own client: dash h for the server, dash U for the user, then the database. It gets the same refusal, and pg_isready, a quick check that ships with it, gets no response. An older psql words it the way you may have searched it: could not connect to server. Same failure, older wording.
psql -h rink-db -U rink rinkpsql: error: connection to server at "rink-db" (192.168.71.10), port 5432 failed: Connection refused Is the server running on that host and accepting TCP/IP connections?
pg_isready -h rink-dbrink-db:5432 - no response
psql -h rink-db -U rink rinkpsql: error: could not connect to server: Connection refused Is the server running on host "rink-db" (192.168.71.10) and accepting TCP/IP connections on port 5432?
Is PostgreSQL even running?
The usual fix first: is it even running? On the database server itself, psql doesn't use the network. Run as postgres, the database's own administrator account, it uses a socket, a file PostgreSQL creates while it runs. No such file: the server isn't running. So, a restart. It prints nothing, and psql fails exactly the same way.
sudo -u postgres psqlpsql: 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?
sudo systemctl restart postgresql
Guess first: what does systemctl status say about postgresql? Active. Active, exited. On Ubuntu, that unit is only a wrapper that starts the clusters, a cluster being one running PostgreSQL with its own data and port. pg_lsclusters lists the real ones: sixteen main, port five four three two, down.
systemctl status postgresql● postgresql.service - PostgreSQL RDBMS Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; preset: enabled) Active: active (exited) since Tue 2026-11-17 19:42:30 UTC; 23ms ago Process: 232 ExecStart=/bin/true (code=exited, status=0/SUCCESS) Main PID: 232 (code=exited, status=0/SUCCESS) CPU: 527us Nov 17 19:42:30 rink-db systemd[1]: Starting postgresql.service - PostgreSQ…S... Nov 17 19:42:30 rink-db systemd[1]: Finished postgresql.service - PostgreSQ…BMS. Hint: Some lines were ellipsized, use -l to show in full.
pg_lsclustersVer Cluster Port Status Owner Data directory Log file 16 main 5432 down postgres /var/lib/postgresql/16/main /var/log/postgresql/postgresql-16-main.log
So start the real one: postgresql at sixteen dash main. Now it fails out loud. The reason is in PostgreSQL's log: invalid value for shared_buffers, five twelve, lower case m b. Units are case sensitive. Last night someone raised the memory with a lower case unit, and that night's restart failed just as quietly.
sudo systemctl start postgresql@16-mainJob for postgresql@16-main.service failed because the service did not take the steps required by its unit configuration. See "systemctl status postgresql@16-main.service" and "journalctl -xeu postgresql@16-main.service" for details.
sudo tail -n 5 /var/log/postgresql/postgresql-16-main.log2026-11-17 19:42:30.818 GMT [248] LOG: invalid value for parameter "shared_buffers": "512mb" 2026-11-17 19:42:30.818 GMT [248] HINT: Valid units for this parameter are "B", "kB", "MB", "GB", and "TB". 2026-11-17 19:42:30.818 UTC [248] FATAL: configuration file "/etc/postgresql/16/main/postgresql.conf" contains errors pg_ctl: could not start server Examine the log output.
grep -n '^shared_buffers' /etc/postgresql/16/main/postgresql.conf130:shared_buffers = 512mb # min 128kB
The fix is two capital letters: five twelve, capital M, capital B. And the comment on the next line says: change requires restart.

Start it again. This time, nothing printed means it worked: pg_lsclusters says online.
sudo systemctl start postgresql@16-main
pg_lsclustersVer Cluster Port Status Owner Data directory Log file 16 main 5432 online postgres /var/lib/postgresql/16/main /var/log/postgresql/postgresql-16-main.log
Running again
PostgreSQL is running again, so the booking site should be back. Guess first: does the booking server connect now? No. Still refused.
psql -h rink-db -U rink rinkpsql: error: connection to server at "rink-db" (192.168.71.10), port 5432 failed: Connection refused Is the server running on that host and accepting TCP/IP connections?
On the database server, ss lists what's listening. PostgreSQL is on port five four three two, but only at one twenty-seven dot zero dot zero dot one, localhost: the address only this machine can reach. Its setting, listen_addresses, says localhost. That's the default after you install it.
sudo ss -ltn 'sport = :5432'State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 200 127.0.0.1:5432 0.0.0.0:*
sudo -u postgres psql -c 'SHOW listen_addresses'listen_addresses ------------------ localhost (1 row)
Think of a shop. Shutters down: nobody's in, and you know at once. Open, but only by the staff door: from the street it looks just as closed. That's localhost. The road blocked: you never get there, you just wait. And at the door, a list of who may come in.

A star in listen_addresses means every address this server has. Ana puts it in its own file in the conf.d folder, and reloads. It prints nothing. The booking server tries again: refused. The log says why: listen_addresses cannot be changed without restarting the server. A reload re-reads the settings, but this one only counts when the server starts.
echo "listen_addresses = '*'" | sudo tee /etc/postgresql/16/main/conf.d/listen.conflisten_addresses = '*'
sudo systemctl reload postgresql@16-main
psql -h rink-db -U rink rinkpsql: error: connection to server at "rink-db" (192.168.71.10), port 5432 failed: Connection refused Is the server running on that host and accepting TCP/IP connections?
sudo tail -n 3 /var/log/postgresql/postgresql-16-main.log2026-11-17 19:42:33.301 UTC [275] LOG: received SIGHUP, reloading configuration files 2026-11-17 19:42:33.302 UTC [275] LOG: parameter "listen_addresses" cannot be changed without restarting the server 2026-11-17 19:42:33.302 UTC [275] LOG: configuration file "/etc/postgresql/16/main/conf.d/listen.conf" contains errors; unaffected changes were applied
So restart the cluster. Now ss shows zero dot zero dot zero dot zero: every address.
sudo systemctl restart postgresql@16-main
sudo ss -ltn 'sport = :5432'State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 200 0.0.0.0:5432 0.0.0.0:* LISTEN 0 200 [::]:5432 [::]:*
So which changes need a restart? The settings that decide how the server starts: the addresses it listens on, its port, its memory. pg_hba.conf, the list of who may log in, is read again on a reload. And postgresql.conf says which is which, in a comment next to each one.

A new error means progress
And the booking server gets a different error. No pg_hba.conf entry for host one ninety-two dot one sixty-eight dot seventy-one dot twenty, user rink, database rink. That's progress. The knock reached PostgreSQL, and it answered: its rules file has no line for this address yet. psql asks twice, with encryption and without, so the line shows twice.
psql -h rink-db -U rink rinkpsql: error: connection to server at "rink-db" (192.168.71.10), port 5432 failed: FATAL: no pg_hba.conf entry for host "192.168.71.20", user "rink", database "rink", SSL encryption connection to server at "rink-db" (192.168.71.10), port 5432 failed: FATAL: no pg_hba.conf entry for host "192.168.71.20", user "rink", database "rink", no encryption
Ana adds a host line: database rink, user rink, from the booking server's address, slash thirty-two meaning that one address only, checked with a password, scram. For this file, a reload is enough. psql asks for the password, and there are today's three sessions.
echo 'host rink rink 192.168.71.20/32 scram-sha-256' | sudo tee -a /etc/postgresql/16/main/pg_hba.confhost rink rink 192.168.71.20/32 scram-sha-256
sudo systemctl reload postgresql@16-main
psql -h rink-db -U rink rink -c 'SELECT * FROM sessions'Password for user rink: starts | name | places ----------+----------------+-------- 10:00:00 | Learn to skate | 6 13:30:00 | Public skate | 41 19:00:00 | Hockey drop-in | 3 (3 rows)
And the rink's website lists them too: learn to skate at ten, and three places left for hockey.

One more cause hides behind the same refusal: the wrong port. Ask for five four three three, where nothing listens, and you're refused at once.
psql -h rink-db -p 5433 -U rink rinkpsql: error: connection to server at "rink-db" (192.168.71.10), port 5433 failed: Connection refused Is the server running on that host and accepting TCP/IP connections?
When it hangs instead
A week later, the page takes ten seconds, then says: timeout expired. A different word. Refused is an answer. A timeout means no answer came back at all.

The server is up: ping gets its answers. psql, with a ten second limit, times out. Without a limit, it waited a hundred and thirty-two seconds before it gave up.
ping -c 2 -n rink-dbPING rink-db (192.168.71.10) 56(84) bytes of data. 64 bytes from 192.168.71.10: icmp_seq=1 ttl=64 time=0.179 ms 64 bytes from 192.168.71.10: icmp_seq=2 ttl=64 time=0.176 ms --- rink-db ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1052ms rtt min/avg/max/mdev = 0.176/0.177/0.179/0.001 ms
PGCONNECT_TIMEOUT=10 psql -h rink-db -U rink rinkpsql: error: connection to server at "rink-db" (192.168.71.10), port 5432 failed: timeout expired
Here's the difference on the wire. Refused: the app asks to connect, and the server's machine answers no straight away. Dropped: the firewall throws the request away. The app asks again, and again, and hears nothing. It's the blocked road from the shop: you never arrive, you just wait.

On the database server, ufw, Ubuntu's firewall, has been switched on, and it lets only SSH in. Everything else is dropped without a word. Allow the rink's network to port five four three two, and the booking server counts its three sessions again.
sudo ufw statusStatus: active To Action From -- ------ ---- 22/tcp ALLOW Anywhere
sudo ufw allow from 192.168.71.0/24 to any port 5432Rule added
psql -h rink-db -U rink rink -c 'SELECT count(*) FROM sessions'Password for user rink: count ------- 3 (1 row)
Before you call it fixed
Before you call it fixed, check from the app's own machine. pg_isready said no response before; now it says accepting connections. And after any change to postgresql.conf, restart, then look at pg_lsclusters, because a failed restart can stay silent.
pg_isready -h rink-dbrink-db:5432 - accepting connections
pg_lsclustersVer Cluster Port Status Owner Data directory Log file 16 main 5432 online postgres /var/lib/postgresql/16/main /var/log/postgresql/postgresql-16-main.log
Recap
So read the error's last words. No such file: not running. Refused: nothing listens at that address and port. Timeout: a firewall. No pg_hba.conf entry: you got through; add a line.

If your app is talking to its database again, subscribing brings more fixes, run on real servers.
Every error in this video came from a real PostgreSQL server, and every command and the lab files are linked in the description. Thanks for watching Cloud Explainers.
Sources
- PostgreSQL 16, Connections and Authentication (listen_addresses: default localhost, set only at server start; port 5432)
- PostgreSQL 16, Setting Parameters (unit names are case-sensitive: kB, MB, GB)
- PostgreSQL 16, The pg_hba.conf File (host lines; changes take effect on a reload)
- PostgreSQL 16, libpq connection parameters (a host name means TCP; no host means the Unix socket; connect_timeout; sslmode prefer tries SSL first)
- Ubuntu manual, pg_lsclusters and pg_ctlcluster (postgresql-common)
- Ubuntu Server documentation, Install and configure PostgreSQL (listen_addresses, pg_hba.conf)
- ufw(8), Ubuntu's firewall (default deny incoming, allow from a network to a port)
Demos: containers on our own Mac standing in for an ice rink's booking server and database server; the rink and its sessions are made up. Every error and log line was real. Watch the full video on YouTube.