psql: could not connect to server: Connection refused
Error
psql: could not connect to server: Connection refused
PostgreSQL Connection Refused – Error Explained
One of the most frustrating issues for DBAs and developers is the PostgreSQL connection refused error. The full message usually looks like this:
psql: could not connect to server: Connection refused Is the server running on host "localhost" and accepting TCP/IP connections on port 5432?
Common Causes of PostgreSQL Connection Refused Error
The PostgreSQL connection refused problem can occur due to:
-
PostgreSQL service is not running
-
Wrong port number (default is 5432)
-
Firewall blocking the connection
-
TCP/IP connections not enabled in
postgresql.conf
-
Host not allowed in
pg_hba.conf
Solution:
1. Check if PostgreSQL service is running
netstat -nlt | grep 5432
If PostgreSQL is listening on another port, update your connection string.
3. Restart the PostgreSQL server
sudo systemctl restart postgresql
4. Enable TCP/IP connections
Edit postgresql.conf
and ensure:
listen_addresses = '*'
Then reload PostgreSQL:
6. Check firewall settings
Allow traffic on port 5432:
sudo ufw allow 5432/tcp
7. Test connection manually