Skip to content

How to Fix PostgreSQL Connection Refused Error

DodaTech Updated 2026-06-24 3 min read

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

The Problem

Your application or client tries to connect to PostgreSQL and gets:

psql: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?

PostgreSQL is not accepting connections. The service may be stopped, bound to localhost only, or blocked by a firewall.

Quick Fix

1. Check if PostgreSQL is running

sudo systemctl status postgresql

If inactive or failed, start it:

sudo systemctl start postgresql
sudo systemctl enable postgresql

Check the listening port:

sudo ss -tlnp | grep 5432

Expected output:

LISTEN 0 128 127.0.0.1:5432 0.0.0.0:*

2. Check listen_addresses

PostgreSQL may be configured to listen only on localhost. Edit <a href="/databases/postgresql/">postgresql</a>.conf:

sudo nano /etc/postgresql/16/main/postgresql.conf
# Wrong — localhost only
listen_addresses = 'localhost'

# Right — all interfaces (configure firewall)
listen_addresses = '*'

# Or specific IPs
listen_addresses = 'localhost,192.168.1.100'

Find the config file location:

sudo -u postgres psql -c "SHOW config_file;"

Restart after changing:

sudo systemctl restart postgresql

3. Check pg_hba.conf

The host-based authentication file may reject the connection. Add an entry for your network:

# /etc/postgresql/16/main/pg_hba.conf

# Wrong — no remote access rules
local   all   all                   peer
host    all   all   127.0.0.1/32    scram-sha-256

# Right — allow connections from your subnet
host    all   all   192.168.1.0/24  scram-sha-256
host    all   all   0.0.0.0/0       scram-sha-256

Reload after changing:

sudo systemctl reload postgresql

4. Check firewall rules

# UFW
sudo ufw status
sudo ufw allow 5432/tcp

# firewalld
sudo firewall-cmd --permanent --add-port=5432/tcp
sudo firewall-cmd --reload

5. Test the connection

# Test locally
psql -U postgres -d postgres -h localhost

# Test from remote
psql -U myuser -d mydb -h 192.168.1.50

Expected output: A psql prompt, confirming the connection works.

Prevention

  • Enable PostgreSQL to start on boot with systemctl enable <a href="/databases/postgresql/">postgresql</a>.
  • Use a connection pooler like PgBouncer for production deployments.
  • Document listen_addresses and pg_hba.conf settings in your runbook.
  • Set up monitoring alerts for PostgreSQL connectivity.

Common Mistakes with connection refused

  1. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  2. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  3. Using return to exit a function early instead of wrapping a pure value in the monad

These mistakes appear frequently in real-world POSTGRES code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### How do I find the PostgreSQL data directory and config files?

Run sudo -u postgres psql -c "SHOW data_directory;" for the data directory and sudo -u postgres psql -c "SHOW config_file;" for the config file path.

Can I change the default PostgreSQL port?

Yes. Edit <a href="/databases/postgresql/">postgresql</a>.conf and change port = 5433. Update the firewall rules and connection strings to use the new port. Restart PostgreSQL.

What does "Connection refused" mean vs "Connection timed out"?

Connection refused means the server actively rejected the connection (not running, wrong port, or firewall reject). Connection timed out means the client could not reach the server at all (wrong IP, network down, firewall drop).

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro