ECONNREFUSED for Postgres on nodeJS with dockers
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
If a Node.js app running with Docker reports ECONNREFUSED when connecting to PostgreSQL, the problem is usually not PostgreSQL syntax or the Node client library. It is usually one of three issues: the app is connecting to the wrong host, the database container is not ready yet, or the connection settings inside the container network are different from the settings on your local machine. Once you check those assumptions in the right order, the fix is usually straightforward.
The Most Common Mistake: Using localhost
Inside Docker Compose, localhost inside the application container means the application container itself, not the database container. If your Node service tries to connect to localhost:5432, it will usually fail unless PostgreSQL is running in the same container.
In Compose, use the service name as the hostname:
Here, db is the correct hostname for the application container.
Minimal Node.js Connection Example
Using the pg package:
If this throws ECONNREFUSED, the next question is whether the database is actually listening yet.
Container Startup Order Is Not Readiness
depends_on controls startup order, but it does not guarantee PostgreSQL is ready to accept connections when the Node app starts.
That means an application can start, try one connection immediately, and fail even though the database container exists.
Use a health check or simple retry logic:
This is a practical startup hardening step for local development and CI.
Verify From Inside the Containers
When debugging, test the environment from the network where the app runs.
Useful commands:
If pg_isready says PostgreSQL is healthy and the app still fails, the issue is usually host, port, credentials, or network naming.
Port Mapping Is Often Misunderstood
If both services are in the same Compose network, the app should connect to container port 5432, not the host-mapped port used from your laptop shell. Host port mappings matter for traffic entering Docker from outside, not for one container talking to another on the internal network.
This is another reason localhost plus a mapped host port often fails inside the app container.
Common Pitfalls
- Using
localhostfrom the Node.js container instead of the database service name. - Assuming
depends_onmeans PostgreSQL is ready for connections. - Using the host-mapped port instead of the database container port for internal container-to-container traffic.
- Forgetting to verify the app container's actual environment variables.
- Debugging only from the host machine instead of from inside the running containers.
Summary
- In Docker Compose, the Node app should usually connect to PostgreSQL using the database service name, not
localhost. - '
ECONNREFUSEDoften means the target host or readiness assumption is wrong.' - Add retry logic or health-aware startup handling because
depends_onis not enough. - Verify connectivity from inside the application container.
- Keep Docker networking and host-port mapping concepts separate when diagnosing connection failures.
Related reading
- ECS/ECR is common practice to have one repository per image and associated versions?
- ElasticSearch in Windows docker image vm max map count
- Enable Ingress controller on Docker Desktop with WLS2
- Enterprise Library Unity vs Other IoC Containers
- EF Core add-migration Build Failed
- EF LINQ include multiple and nested entities
- edit-config for ios usage descriptions doc.find is not a function
- Efficient computation of n choose k in Node.js

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.