sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server … failed
SQLAlchemy’s DB driver raised an operational error — connection failures, lost connections or locked SQLite databases.
Seen on:
Python
Meaning
The text in parentheses is the driver’s own error: connection refused, password authentication failed, server closed the connection, or “database is locked” for SQLite.
Common causes
- Database unreachable / credentials wrong
- Connection dropped by the server (idle timeout)
- SQLite locked by another writer
- Too many connections
⚡ Quick fix
- Read the driver error in parentheses
- Use pool_pre_ping=True and pool_recycle to avoid stale connections
- Check DATABASE_URL
- For SQLite, avoid concurrent writers or use WAL
Detailed fix by platform
Python
engine = create_engine(url, pool_pre_ping=True, pool_recycle=1800)
How to diagnose
- Driver error — Text inside the parentheses
- Timing — At startup or after idle?
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 2 Oct 2026