ECONNREFUSED 🟩 Node.js

Error: connect ECONNREFUSED 127.0.0.1:5432

Nothing is listening at that host:port — the target service is down or you’re connecting to the wrong address.

Seen on: Node.js Docker

Meaning

The OS actively refused the TCP connection. Inside Docker, 127.0.0.1 refers to the container itself, not your host or other containers — a very common cause.

Common causes

  • Target service (DB, Redis, API) not running
  • Wrong port
  • Using localhost inside a container instead of the service name
  • Service listening only on 127.0.0.1 while you connect from elsewhere
  • Node 17+ resolving localhost to IPv6 ::1 while the service listens on IPv4 only

⚡ Quick fix

  1. Start the target service and check its port
  2. In Docker Compose, use the service name as host
  3. Use 127.0.0.1 instead of localhost (IPv6 issue)
  4. Make the service listen on 0.0.0.0 if accessed remotely

Detailed fix by platform

Docker

  1. From a container to the host machine: use host.docker.internal (Docker Desktop) or add extra_hosts: ["host.docker.internal:host-gateway"].

Node.js

  1. Retry on startup — databases often accept connections a few seconds after the app container starts.

How to diagnose

  1. Target — Which host:port in the message?
  2. Service — Is it running and listening there?
  3. Network — Same container/network/IP version?

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.