PostgreSQL "Connection refused … Is the server running?": find out why nothing answers on 5432
Fix PostgreSQL "connection refused": check that it runs, read why it did not start, then fix listen_addresses, the port, the firewall or Docker.
by OhSudo ··
Not sure this is your cause?Find it step by step: Can't connect to the database →
Part of a learning path:SQL and PostgreSQL, step by step
The application (or psql) can’t reach the database:
psql: error: 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?Without -h, psql tries the local socket file instead, and the message changes:
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?Connection refused means the network worked, but nothing listens at that address and port. PostgreSQL isn’t running, or it runs but doesn’t listen where the client knocks. (A wrong password would give a different error — see password authentication failed.)
- Clientconnects to 127.0.0.1:5432TCP
- listen_addresses and portwhere PostgreSQL listensrefused(fails)
- Nothing therestopped, or elsewhere(breaks here)
TL;DR
1. Is PostgreSQL running?
accepting connections → it runs and listens there (then your client uses another address or port — section 4). no response → continue.
Note
Debian and Ubuntu: postgresql.service is only a wrapper that says “active (exited)” even when the database is down. The real service is per cluster — postgresql@16-main for version 16 — and pg_lsclusters shows every cluster with its port and status (online / down). On RHEL-family systems it’s simply postgresql.
2. Why didn’t it start?
postgres[688]: LOG: invalid value for parameter "shared_buffers": "1GBB"postgres[688]: HINT: Valid units for this parameter are "B", "kB", "MB", "GB", and "TB".postgres[688]: FATAL: configuration file "/etc/postgresql/16/main/postgresql.conf" contains errorsOn Debian/Ubuntu, read /var/log/postgresql/postgresql-16-main.log too (and journalctl -u postgresql@16-main). The usual reasons:
- A typo in
postgresql.conf— like above: PostgreSQL refuses to start with an invalid setting, and names it. - The disk is full —
could not write … No space left on device; see No space left on device. - Another PostgreSQL already uses the port —
could not bind IPv4 address "127.0.0.1": Address already in use; see Address already in use. - Permissions on the data directory —
data directory "…" has invalid permissions: it must belong topostgresand have mode0700.
Fix the setting, then start it:
3. It runs, but remote clients are refused
By default PostgreSQL listens only on localhost. From the server itself everything works; from another machine (or a container) it’s “connection refused”. Check where it listens:
LISTEN 0 200 127.0.0.1:5432 0.0.0.0:* users:(("postgres",pid=882,fd=6))To accept connections from the network, three things are needed:
listen_addressesinpostgresql.conf— the server’s address, or'*'for all — then restart (a reload isn’t enough for this setting).- A
hostline inpg_hba.conffor the clients’ network, for examplehost shop app 10.0.1.0/24 scram-sha-256, then reload. - The firewall must allow port 5432 from those clients (
ufw,firewalld, the cloud security group).
Don’t open 5432 to the whole internet; limit it to the networks that need it.
4. The client knocks on the wrong door
- Another port. On Debian/Ubuntu, a second cluster gets 5433, 5434… —
pg_lsclustersshows which port each one has. - Docker. Inside a container,
localhostis the container itself; a database container is reached by its service name (db:5432), and a database on the host throughhost.docker.internal— see connection refused to localhost from a container. - A database container that isn’t ready yet. It starts listening only after initialization; use a healthcheck with
pg_isreadyanddepends_on: condition: service_healthy— the Docker Compose Generator adds both.
Shell, we have a problem!
Similar, but different: timeout expired / Connection timed out — packets are dropped on the way (firewall, wrong IP, routing), so nothing even refuses; see “Connection timed out” vs “Connection refused”. no pg_hba.conf entry for host … — the connection reached PostgreSQL, but no pg_hba.conf line allows it: see that article. FATAL: sorry, too many clients already — it runs fine and is full; check max_connections and connection pools. If the server is up and full, see too many clients already. For everyday admin commands, see the PostgreSQL Admin Cheat Sheet.
$ ./practice.sh
Practice this in the terminal
Open the Terminal Playground with this article’s scenario loaded. It is safe and runs in your browser.
Found this useful? Share it.
Found a mistake? Let us know at contact@ohsudo.com.