Skip to content

PostgreSQL Connection Refused — How to Fix

DodaTech Updated 2026-06-22 2 min read

In this tutorial, you'll learn about PostgreSQL Connection Refused. We cover key concepts, practical examples, and best practices.

PostgreSQL connection refused is a critical error that blocks your applications, migrations, and admin access. This guide covers every common cause and fix.

What You'll Learn

How to diagnose and fix PostgreSQL connection refused errors by checking the server status, configuration, firewall, and authentication settings.

Why It Matters

A database connection failure takes down every application that depends on it — fixing it fast is essential.

Real-World Use

Your web application returns 500 errors because the Rails/Django backend can't reach PostgreSQL after a server restart.

Step 1 — Check PostgreSQL Status

sudo systemctl status postgresql

Expected output: Shows active (running) if the service is up. If it shows inactive or failed, start it:

sudo systemctl start postgresql

Step 2 — Test Connection Locally

psql -U postgres -d postgres -h localhost

Expected output: A psql prompt if the local connection works. If it fails, the issue is in pg_hba.conf or the server configuration.

Step 3 — Check Listen Addresses

PostgreSQL by default listens only on localhost. Edit <a href="/databases/postgresql/">postgresql</a>.conf:

sudo nano /etc/postgresql/16/main/postgresql.conf

Find and update:

listen_addresses = '*'

Expected behavior: After restart, PostgreSQL accepts connections on all network interfaces:

sudo systemctl restart postgresql

Common Causes and Fixes

pg_hba.conf Rejects Connection

The host-based authentication file controls who can connect. Add an entry:

host    all             all             0.0.0.0/0               md5

Expected behavior: Allows password-authenticated connections from any IP. Reload with:

sudo systemctl reload postgresql

Firewall Blocking Port 5432

Check and allow the port:

sudo ufw status | grep 5432
sudo ufw allow 5432/tcp

For cloud providers, check the security group or firewall rules in your VPC.

PostgreSQL Running on a Different Port

Check which port PostgreSQL is actually using:

sudo netstat -tlnp | grep postgres

Expected output: Shows the IP and port PostgreSQL is listening on, e.g., 0.0.0.0:5432.

Connection String Wrong

Verify your connection string:

postgresql://user:password@host:5432/database

Common mistakes:

  • Wrong hostname or IP
  • Missing database name
  • Special characters in password not URL-encoded

Additional Troubleshooting

# Check the error message and stack trace for more context
echo "Review the full error output to identify the root cause"

If the above steps do not resolve the issue, examine the complete error message and stack trace. Often the key detail is in the middle of the traceback rather than the final line. Search for the error message in the project documentation or issue tracker for additional solutions.

Prevention Tips

  • Set up monitoring alerts for PostgreSQL connectivity
  • Use connection pooling (PgBouncer) for production
  • Document firewall rules and ports in your runbook
  • Test remote connections after every config change

Quick Reference

Symptom Check Fix
Can't connect from remote listen_addresses config Set to *
Authentication failed pg_hba.conf Add host entry with md5
Connection timeout Firewall Allow port 5432/tcp
Service not running systemctl status Start or restart PostgreSQL

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro