Skip to content

Database Connection Failed

Error message

                Error: connect ECONNREFUSED <db-host>:5432
              

Quick summary

The application cannot establish a connection to the database, usually because the database is down, unreachable, or credentials are wrong.

Why this happens

  • The database server is not running or has crashed

  • Network issues prevent the application from reaching the database

  • Database credentials (username, password) are incorrect

  • The database connection pool is exhausted — too many connections

Minimal example

✗ Broken code
# App cannot connect to database
DATABASE_URL=postgres://user:pass@db:5432/mydb
# Error: ECONNREFUSED — db is down
✓ Fixed code
# Check and restart database
kubectl get pods -l app=database
kubectl rollout restart deployment/database
# Wait for DB to be ready, then restart app
kubectl rollout restart deployment/app

The database is down. The fix restarts the database and then the application.

How to diagnose

  • Check if the database is running: kubectl get pods -l app=database

  • Test connectivity: kubectl exec <app-pod> -- nc -zv <db-host> <db-port>

  • Verify database credentials in the Secret or environment variables

  • Check database connection limits and current connections

How to fix

  • Start or restart the database if it is down

  • Fix network connectivity or firewall rules between app and database

  • Correct the database credentials in the Secret/ConfigMap

  • Increase the database max connections or fix connection leaks in the app

How to prevent

  • Use connection pooling with appropriate pool sizes

  • Monitor database health and connection counts

  • Use init containers to wait for the database before starting the app

Related Resources

Related Glossary

← Back to language