Cause 1 of 5
The service is not running.
Start it, then verify something is actually listening: lsof -i :5432 or ss -ltn | grep 5432. If it exits straight after starting, its own log holds the reason — read that rather than retrying the client.
Node.js · 5 common causes
Updated
The connection reached the host and was actively refused — nothing is listening on that port.
Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1595:16) {
errno: -61,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5432
}Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is ECONNREFUSED.
Cause 1 of 5
Start it, then verify something is actually listening: lsof -i :5432 or ss -ltn | grep 5432. If it exits straight after starting, its own log holds the reason — read that rather than retrying the client.
Cause 2 of 5
Each container has its own loopback interface. Use the compose service name as the hostname — postgres:5432, not 127.0.0.1:5432 — since services resolve each other by name on the shared network.
Cause 3 of 5
A process bound to 127.0.0.1 refuses connections from other machines and other containers. Bind 0.0.0.0 to accept them — and if you do that, make sure the port is not also exposed to the public internet by your firewall or security group.
Cause 4 of 5
Compose's depends_on waits for the container to start, not for the database to accept connections. Add a healthcheck and condition: service_healthy, or retry the connection with a short exponential backoff at startup.
Cause 5 of 5
Some resolvers answer localhost as ::1 first. Put 127.0.0.1 in the connection string explicitly, or configure the server to listen on both stacks.
Use service names rather than localhost in every containerised config, and give each dependency a healthcheck so the app waits for readiness instead of racing it.
nc -zv host 5432 or curl -v telnet://host:5432 gives an immediate answer, and separates 'refused' from 'no route' clearly. Run it from the same place your app runs — from inside the container, not from the host.Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.
Open the editor