After deployment, the app can't connect to the database. How do you troubleshoot it?
Short answer
Work from the network up: can the app reach the database host and port, then are the credentials right, then does the database allow this user from this place. Read the exact error first — it usually tells you which layer is failing.
Read the error — it points to the layer
| Error you see | What it means | Where to look |
|---|---|---|
UnknownHostException / getaddrinfo ENOTFOUND |
The hostname can't be resolved | DNS, Service name, typo in DB_HOST |
Connection timed out |
Network path is blocked | Security groups, firewall, NetworkPolicy, wrong port |
Connection refused |
Host reachable but nothing listening on that port | Database not running, wrong port, bound to localhost only |
Access denied for user / password authentication failed |
Wrong username or password | Secret value, env variable not set |
Unknown database / database does not exist |
Wrong database name | DB_NAME, database not created |
SSL required / certificate errors |
TLS settings don't match | Add sslmode=require or the right CA certificate |
Step by step (Kubernetes example)
# 1. What does the app say?
kubectl logs deploy/orders-api | tail -20
# 2. Are the settings actually there?
kubectl exec deploy/orders-api -- env | grep DB_
# 3. Does the name resolve and is the port open — from inside the cluster?
kubectl run dbtest --rm -it --image=nicolaka/netshoot --restart=Never -- \
sh -c "nslookup postgres.db.svc.cluster.local && nc -zv postgres.db.svc.cluster.local 5432"
# 4. Do the credentials work? Log in with a client
kubectl run psql --rm -it --image=postgres:16 --restart=Never -- \
psql "postgresql://app_user:[email protected]:5432/orders" -c "select 1"
On cloud databases (RDS, Azure Database): check the database's security group or firewall allows the app's subnet or node security group on the port (3306 for MySQL, 5432 for PostgreSQL, 27017 for MongoDB).
Example — a real case
The logs show Connection timed out to orders-db.xxxx.ap-south-1.rds.amazonaws.com:5432. nc -zv from a Pod also times out. The RDS security group only allows the old EC2 instance's security group, not the EKS node group. Adding an inbound rule for port 5432 from the node security group fixes it.
What to say in the interview
"I start with the exact error, because it tells me the layer. Unknown host means DNS or a wrong name; timeout means network rules like security groups or NetworkPolicies; connection refused means the database isn't listening on that port; access denied means credentials. Then I verify from inside the Pod: check the env variables, test DNS and the port with nslookup and nc, and try logging in with a database client. On RDS, the security group is the most common culprit."
Follow-up questions you may get
- The password has special characters and login fails only from the app? It may need URL-encoding inside a connection URL (for example
@becomes%40). - It works locally but not in the cluster?
localhostinside a container means the container itself, not your laptop — use the Service name.