How to Fix PostgreSQL Connection Refused Error
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_addressesandpg_hba.confsettings in your runbook. - Set up monitoring alerts for PostgreSQL connectivity.
Common Mistakes with connection refused
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto 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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro