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.
server closed the connection unexpectedlydatabase is locked 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 not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- 2006 MySQL ERROR 2006 (HY000): MySQL server has gone away
- could not connect PostgreSQL: could not connect to server / connection refused — Is the server running on host and accepting TCP/IP connections?
- Hibernate ConstraintViolationException org.hibernate.exception.ConstraintViolationException: could not execute statement
Most viewed in Python
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 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.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 12 Oct 2026