You have:
Both need to communicate over Docker’s virtual network — not your machine’s localhost.
docker run --name postgresDb \\
-e POSTGRES_PASSWORD=sweabhishek \\
-d -p 5432:5432 \\
postgres
✅ This runs Postgres, exposes it on your local port 5432, and names the container postgresDb.
However — your Node.js app container cannot access it via localhost, because each container has its own isolated network namespace.
localhost doesn’t work inside containersInside the Node.js container:
localhost means itself, not your host machine.localhost:5432 won’t connect.You need both containers on the same Docker network.
docker network create mynetwork
This creates a bridge network (like a virtual LAN) inside Docker.