How do you test whether an app server can reach a database port?

BeginnerHands-onDatabase connections in apps
Answer

Short answer

Run a TCP connectivity check from the app's location (the server or inside the pod): nc -zv host port, telnet host port, or bash's built-in /dev/tcp. Then test a real login with the database client (psql, mysql, mongosh).

Commands

nc -zv db.prod.internal 5432
# Connection to db.prod.internal 5432 port [tcp/postgresql] succeeded!

telnet db.prod.internal 3306

timeout 3 bash -c '</dev/tcp/db.prod.internal/27017' && echo OPEN || echo CLOSED

# Inside Kubernetes when the app image has no tools:
kubectl run nettest --rm -it --image=nicolaka/netshoot -- bash
kubectl debug -it pod/orders-7d9 --image=nicolaka/netshoot     # ephemeral debug container

Then try a real login

psql "host=db.prod.internal port=5432 dbname=orders user=orders_app sslmode=require" -c "select 1"
mysql -h db.prod.internal -u shop_app -p -e "select 1"
mongosh "mongodb://app@m1:27017/orders?authSource=admin" --eval "db.runCommand({ping:1})"

Why from the app's location?

Testing from your laptop proves nothing about the app's security group, subnet or NetworkPolicy.

What to say in the interview

"From the app's own network position I use nc or /dev/tcp to test the port, using netshoot or kubectl debug if the image is minimal, then confirm with the real client and the app's credentials."

All Build Tools (Java, Node.js, Python, .NET) questions Roadmap: Connecting apps to databases

More on Database connections in apps