Skip to content

Troubleshooting guide

PostgreSQL connection refused: how to fix it

The client reached the host and nothing accepted on that port. Postgres is stopped, is listening on another address, or your client is pointed at the wrong place.

3 min read11 sectionsUpdated

On this page

Key points

  • listen_addresses defaults to localhost.
  • pg_hba.conf errors look different from a refused connection.
  • localhost inside a container is that container.
  • Crash recovery can make Postgres refuse connections for a while.

What it means

The exact words matter. Connection refused means nothing is listening. no pg_hba.conf entry means you reached Postgres, and it rejected your host or user. timeout expired suggests a firewall dropping packets. the database system is starting up means wait for recovery.

Common causes

  • The server is stopped, or failed to start, for example because the disk is full.
  • listen_addresses = 'localhost' blocks remote clients.
  • Docker or Compose: using localhost instead of the service name.
  • A different port configured, or two clusters on one machine.
  • A firewall rejecting traffic.

Diagnose it

Find out what is listening, and test from where your application runs.

bash
# Is it listening, and on which address?
sudo ss -ltnp | grep 5432

# Status and recent logs
systemctl status postgresql --no-pager
sudo journalctl -u postgresql -n 40 --no-pager

# Readiness check, from the app host or container
pg_isready -h db.example.internal -p 5432

# Effective settings (once connected locally)
sudo -u postgres psql -c "SHOW listen_addresses; " -c "SHOW port;"
sudo -u postgres psql -c "SHOW hba_file;"

Fixes

Start Postgres, and read the log if it will not start. For remote access, set listen_addresses to a private interface, and restart. Add a narrow pg_hba.conf line for your application's network, then reload. In Compose, connect to the service name. Open port 5432 only to the hosts that need it, using a firewall or security group.

Safely handling restarts

Applications should expect the database to disappear for a moment. A failover or restart gives a short period when connections are refused, so use a connection pool with retry and backoff, and make startup wait for the database with a pg_isready loop or an init container rather than crashing. A very frequent cause of startup failure is a full disk, particularly from WAL files retained by an abandoned replication slot, so if Postgres refuses to start, check df -h on its data volume before anything else.

Catch it automatically

A TCP check on port 5432 from your private network reports refused connections within a minute. PostgreSQL monitoring adds restarts and disk use, and an alert on retained WAL can prevent the disk filling up.

Frequently asked questions

What is the difference between refused and no pg_hba.conf entry?

Refused means you never reached Postgres. No pg_hba.conf entry means you reached it and it rejected the host or user.

Do I need a restart to change listen_addresses?

Yes. pg_hba.conf only needs a reload.

Should Postgres be open to the internet?

No. Use private networks, or a VPN.

Check it now, free

Watch your own site and services with VitalRix

Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.