PostgreSQL: could not connect to server / connection refused — Is the server running on host and accepting TCP/IP connections?
The client reached no PostgreSQL listener at that host/port — the server is down, listening elsewhere, or not accepting remote connections.
Meaning
By default PostgreSQL listens only on localhost (listen_addresses = 'localhost') and port 5432. Remote clients also need a matching pg_hba.conf line and an open firewall port. In Docker, localhost means the app container itself.
Common causes
- PostgreSQL service stopped or crashed (disk full, bad config after edit)
listen_addresseslimited to localhost while connecting remotely- Different port (5433 when multiple versions are installed)
- Firewall / cloud security group blocking 5432
- Docker: using localhost instead of the service name
- Socket path mismatch: “could not connect to server: No such file or directory … /var/run/postgresql/.s.PGSQL.5432”
⚡ Quick fix
sudo systemctl status postgresqland read the log if it’s down- Check
SHOW listen_addresses;andSHOW port; - Set
listen_addresses = '*'+ a pg_hba.conf host line for remote access, then restart - In Docker Compose, connect to
db:5432
Detailed fix by platform
Linux
- Check service, port and listeners:bash
sudo systemctl status postgresql sudo ss -ltnp | grep postgres pg_lsclusters # Debian/Ubuntu: shows version, port, status sudo tail -n 50 /var/log/postgresql/postgresql-*-main.log
Docker
DATABASE_URL=postgres://app:secret@db:5432/app— use the compose service name; add a healthcheck so the app waits for the DB.
How to diagnose
- Service — Running?
- Listener — Which address/port does it listen on?
- Network — Firewall / container network?
- Auth — Next error after connecting: pg_hba.conf?
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 2 Oct 2026