Prompt
Why does my database keep disconnecting after deploy?
Latest observation
A database that “keeps disconnecting after deploy” is usually one of a few common issues, and the deploy often just makes them show up more clearly.
Most common causes
1) New app instances are opening too many connections
If your deploy starts multiple workers/pods/containers, each one may create its own pool. That can exceed the DB’s connection limit and cause disconnects.
Check:
- Number of app replicas/workers increased after deploy
- Connection pool size per instance too high
- DB max connections being hit
Fix:
- Lower pool size
- Use a shared/managed pooler if available
- Make sure old instances shut down cleanly
2) Your app is not reusing connections correctly
If the code creates a new DB connection per request or leaks connections, deploys can worsen the issue.
Check:
- Connections opened but not closed
- Missing
finally/ cleanup logic - ORM/client misconfiguration
Fix:
- Use a connection pool
- Ensure connections are released/closed properly
- Follow your framework’s recommended DB client pattern
3) Environment variables or credentials changed during deploy
A fresh deploy may use different DB host, password, SSL setting, or timeout values.
Check:
DATABASE_URL/ host / username / password- SSL requirements
- Network allowlist / security group rules
Fix:
- Compare old vs new env vars
- Re-deploy with confirmed correct secrets
- Verify the app is pointing to the same DB
4) The deploy causes brief downtime or container restarts
Connections are dropped when the app process restarts, which can look like “database disconnecting.”
Check:
- Are you seeing disconnects only during deploy?
- Does the app recover after a few seconds?
Fix:
- Use graceful shutdown
- Health checks / readiness probes
- Rolling deploys instead of full restarts
5) DB idle timeout / load balancer / proxy is closing connections
Some platforms close idle connections after a certain time.
Check:
- Disconnects happen after inactivity
- Logs mention “server closed the connection” or timeout
Fix:
- Enable keepalive
- Set pool
maxLifetime/idleTimeoutappropriately - Reconnect automatically on failure
6) Long-running migrations or lock contention
A deploy that runs migrations can block or destabilize app connections.
Check:
- Migrations run as part of deploy
- Errors around locks, timeouts, deadlocks
Fix:
- Run migrations separately
- Make migrations backward-compatible
- Keep transactions short
7) Network / security rules changed
Deploying to a new environment can change IPs, VPCs, firewall rules, or private networking.
Check:
- Can the new app host reach the DB host?
- Any firewall/security group changes?
Fix:
- Allow new app IPs/subnets
- Confirm VPC/private link configuration
What to look for in logs
Search for messages like:
too many connectionsconnection refusedconnection timed outserver closed the connectionECONNRESETSSL connection errorauthentication failed
These usually point directly to the cause.
Fast debugging checklist
- Compare app env vars before/after deploy
- Check DB connection count and max connections
- Check app logs for exact error message
- Confirm pool size per instance
- Verify deploy strategy and graceful shutdown
- Check network/firewall/SSL settings
If you want, paste:
- the exact error message,
- your DB type,
- deployment platform,
- and the connection setup code,
and I can help narrow it down quickly.