Error fix · Developer errors

PostgreSQL "Connection refused" on Port 5432 - Diagnose and Fix

Fix psql connection refused on port 5432. Check whether Postgres is running, listen_addresses, pg_hba.conf, firewalls, Docker port mapping and cloud rules.

Error messagecould not connect to server: Connection refused. Is the server running on host and accepting TCP/IP connections on port 5432?

By SiteDownCheck Editorial · Updated November 18, 2026 · 4 min read

"Connection refused" means a machine answered on the network and told you nothing is listening on that port, or a firewall actively rejected the attempt. That is different from a timeout, where nobody answers. It narrows the problem to a short list of causes.

psql: error: connection to server at "db.example.com" (203.0.113.10), port 5432 failed:
Connection refused
	Is the server running on that host and accepting TCP/IP connections?

Older clients phrase it as could not connect to server: Connection refused. A related but separate message is No such file or directory ... Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?, which means a local socket connection and usually indicates the server is stopped.

Decision order

  1. Is PostgreSQL running at all?
  2. Is it listening on the interface you are connecting to?
  3. Is anything between you and it blocking port 5432?
  4. Are you aiming at the right host and port?

Step 1: is the server running?

On the database host:

sudo systemctl status postgresql
pg_lsclusters
ps aux | grep -E "[p]ostgres( |:)"

If it is stopped or failed, read why:

sudo journalctl -u postgresql -n 50 --no-pager
sudo tail -n 50 /var/log/postgresql/postgresql-*-main.log

Typical start failures: the disk is full, a stale postmaster.pid after a crash, a bad edit in postgresql.conf, a port conflict, or a major version upgrade that left the cluster down. Fix the cause, then sudo systemctl start postgresql.

Step 2: what is it listening on?

sudo ss -ltnp | grep 5432

Output such as 127.0.0.1:5432 means it accepts local connections only, so remote clients are refused. That is the default, because listen_addresses defaults to localhost. Check the setting and the actual port:

SHOW listen_addresses;
SHOW port;
SHOW config_file;

To accept connections from other machines, edit postgresql.conf:

listen_addresses = '*'        # or a specific private IP such as '10.0.0.5'
port = 5432

This needs a restart, not just a reload: sudo systemctl restart postgresql. Then confirm ss shows 0.0.0.0:5432 or your chosen address. Binding to * on a public IP exposes the database to the internet, so restrict access by firewall.

Step 3: allow the client in pg_hba.conf

Listening is not authorization. Without a matching rule, you get a different error, no pg_hba.conf entry for host, but it is the next step once the refusal is gone:

host  appdb  appuser  198.51.100.20/32  scram-sha-256

Reload with SELECT pg_reload_conf();.

Step 4: firewall, security groups and NAT

Test from the client machine, not the server:

nc -vz db.example.com 5432

You can also use the Port Checker to see whether port 5432 is open from the public internet. Interpret results this way:

  • Refused immediately: nothing is listening, or a firewall rule uses REJECT.
  • Hangs until timeout: a firewall is silently dropping packets (DROP), or a cloud security group blocks it. See connection timed out.

On the server, check the host firewall:

sudo ufw status
sudo iptables -L -n | grep 5432
sudo firewall-cmd --list-all

In AWS, GCP and Azure, the security group, network ACL or firewall rule must allow TCP 5432 from your source address. Find your own public address with What Is My IP if you need to allow-list it. Managed databases often only accept connections from inside the same VPC or from allow-listed IPs.

Step 5: Docker, tunnels and wrong targets

  • Docker: the container must publish the port: docker run -p 5432:5432 postgres. From another container, use the service name, not localhost. Inside a container, localhost is the container itself.
  • SSH tunnel: ssh -L 5433:localhost:5432 user@dbhost, then connect to localhost:5433. A dead tunnel gives "refused".
  • Different port: some distros and managed services use another port. Check SHOW port; on the server.
  • Wrong host name or stale DNS: resolve it with dig +short db.example.com. Compare with the DNS Checker. After a migration, the name may still point to the old server. The Website Hosting Checker and IP Lookup tell you where an address lives.
  • IPv6 versus IPv4: localhost can resolve to ::1 while Postgres listens only on IPv4. Use 127.0.0.1 to test.

Verify

pg_isready -h db.example.com -p 5432
psql "host=db.example.com port=5432 dbname=appdb user=appuser" -c "SELECT 1;"

pg_isready prints "accepting connections" when all is well. Then add an Uptime Monitor with a TCP port check on 5432 so you hear about the next outage first.

Next steps

FAQ

What is the difference between refused and timed out?

Refused is a fast reply saying no service is listening or access is rejected. A timeout means packets vanish, typically a dropped firewall rule.

Should I open port 5432 to the whole internet?

Avoid it. Allow only your application servers or a VPN, and require TLS and strong passwords if a public listener is unavoidable.

Why can I connect locally but not remotely?

Because listen_addresses defaults to localhost. Set it to the server's address, restart, and open the firewall.

Does a reload apply listen_addresses?

No. It requires a restart.

Get notified when this fix updates

Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.

This page is general information written by our editorial team and updated when documentation changes. Servers and providers differ, so test changes safely first. See our editorial policy and disclaimer.