PostgreSQL error 08003 (Connection Does Not Exist) explained: what it means, why it happens, and how to fix it — with copy-paste code examples.
Enable pool health checks / pre-ping so stale connections are recycled. If that does not apply, acquire a fresh connection from the pool on each request — the full checklist is below.
SQLSTATE: 08003
Official name: Connection Does Not Exist
Service: PostgreSQL
The operation was attempted on a connection that does not exist (already closed or never opened).
from sqlalchemy import create_engine
engine = create_engine(
url,
pool_pre_ping=True,
pool_recycle=300,
pool_size=10,
max_overflow=5,
)
pre_ping issues a cheap SELECT 1 before handing out a pooled connection, replacing dead ones that would raise 08003.
Use pool_pre_ping and pool_recycle to drop dead pooled connections before use.
engine = create_engine(url, pool_pre_ping=True, pool_recycle=300)
Set CONN_HEALTH_CHECKS=True and an appropriate CONN_MAX_AGE.
DATABASES = {'default': {
'CONN_MAX_AGE': 60,
'CONN_HEALTH_CHECKS': True
}}
Most often this happens when reusing a connection object after it was closed by the pool, or when calling a query after a previous fatal error severed the connection.
Enable pool health checks / pre-ping so stale connections are recycled.
This page documents fixes for: sqlalchemy, django.
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.