PostgreSQL error 08006 (Connection Failure) explained: what it means, why it happens, and how to fix it — with copy-paste code examples.
Add connection retry with backoff in the client. If that does not apply, enable TCP keepalives / lower tcp_keepalives_interval to detect dead connections — the full checklist is below.
SQLSTATE: 08006
Official name: Connection Failure
Service: PostgreSQL
The connection to the server failed during the operation.
ALTER SYSTEM SET tcp_keepalives_idle = 60;
ALTER SYSTEM SET tcp_keepalives_interval = 10;
ALTER SYSTEM SET tcp_keepalives_count = 5;
SELECT pg_reload_conf();
Short keepalive probes surface dead connections quickly so clients get 08006 and can retry instead of hanging.
Use pool_pre_ping=True so dead connections are detected and replaced before use.
engine = create_engine(url, pool_pre_ping=True, pool_recycle=1800)
Set CONN_MAX_AGE and enable persistent connections with health checks.
DATABASES = {'default': {'CONN_MAX_AGE': 60, 'CONN_HEALTH_CHECKS': True}}
Use the connection pooler URL (port 6543) for serverless clients to avoid 08006 on cold connections.
postgresql://postgres.[ref]:[key]@aws-0-[region].pooler.supabase.com:6543/postgres
Most often this happens when network interruption between client and server mid-transaction, or when server restart or crash during a long-running query.
Add connection retry with backoff in the client.
This page documents fixes for: sqlalchemy, django, supabase.
Recommendations are editorial — DB Error Reference takes no payment or affiliate fees for tool listings.
This page is based on the official PostgreSQL documentation linked below and adds practical troubleshooting guidance on top.