Error
Error Code:
08006
PostgreSQL Error 08006: Client Connection Failure
Description
Error 08006, 'connection failure', indicates that a client application was unable to establish a connection with the PostgreSQL database server. This typically occurs when the client cannot reach the server or the server refuses the connection attempt.
Error Message
connection failure
Known Causes
4 known causesPostgreSQL Server Unavailable
The PostgreSQL server process is not running or is not accessible from the client's network location.
Invalid Connection Parameters
The hostname, port, username, or password provided by the client application is incorrect or misconfigured.
Network Firewall Blocking
A firewall on either the client or server machine, or an intermediate network device, is preventing the connection.
General Network Connectivity Issues
Underlying network problems, such as a disconnected cable or routing issues, are preventing communication.
Solutions
5 solutions available1. Check PostgreSQL is Running easy
Verify server is started and healthy
1
Check server status
sudo systemctl status postgresql
2
Start if stopped
sudo systemctl start postgresql
3
Check logs for errors
sudo tail -100 /var/log/postgresql/postgresql-14-main.log
2. Check Network Connectivity easy
Verify network path to server
1
Test connectivity
ping postgresql-server
telnet postgresql-server 5432
2
Check if port is listening
ss -tlnp | grep 5432
3
Check firewall
sudo ufw status
sudo iptables -L -n | grep 5432
3. Check listen_addresses medium
Ensure server accepts remote connections
1
Check current setting
SHOW listen_addresses;
2
Update in postgresql.conf
# Listen on all interfaces:
listen_addresses = '*'
# Or specific IP:
listen_addresses = '192.168.1.100'
3
Restart PostgreSQL
sudo systemctl restart postgresql
4. Implement Connection Retry medium
Handle temporary connection failures
1
Python retry logic
import psycopg2
import time
def get_connection(dsn, max_retries=5):
for i in range(max_retries):
try:
return psycopg2.connect(dsn)
except psycopg2.OperationalError as e:
if 'connection' in str(e).lower():
time.sleep(2 ** i) # Exponential backoff
continue
raise
raise Exception('Could not connect after retries')
5. Check SSL/TLS Configuration medium
Fix SSL connection issues
1
Try connecting without SSL
psql "host=server dbname=db sslmode=disable"
2
Check SSL settings
SHOW ssl;
3
Connection string with SSL
psql "host=server dbname=db sslmode=require"