How to Fix Connection Refused Error in PostgreSQL
connection refused error in PostgreSQL usually means the server is not accepting connections on the specified host or port. To fix it, ensure the PostgreSQL server is running, the postgresql.conf allows listening on the right IP, and pg_hba.conf permits your client connection.Why This Happens
This error happens when your client tries to connect but the PostgreSQL server is not reachable or refusing connections. Common reasons include the server not running, listening only on localhost, firewall blocking the port, or client IP not allowed.
psql -h 192.168.1.100 -U user dbnameThe Fix
First, check if PostgreSQL is running with sudo systemctl status postgresql. Then, edit postgresql.conf to set listen_addresses = '*' to allow connections from any IP. Next, update pg_hba.conf to add your client's IP with proper authentication method. Finally, restart PostgreSQL to apply changes.
# In postgresql.conf listen_addresses = '*' # In pg_hba.conf host all all 192.168.1.0/24 md5 # Restart server sudo systemctl restart postgresql
Prevention
Always verify your PostgreSQL server is running before connecting. Keep listen_addresses and pg_hba.conf updated for your network. Use firewalls to allow port 5432 only for trusted IPs. Regularly test connections after configuration changes to catch issues early.
Related Errors
- Timeout expired: Server is reachable but not responding quickly; check server load and network.
- Authentication failed: Connection reached server but credentials are wrong; verify username and password.
- Could not translate host name: DNS or hostname error; check your connection string.