Error: connect ECONNREFUSED 127.0.0.1:5432

Node.js · updated 2026-11-05

Raised by the database driver (pg, Knex, Sequelize) when it opens a connection, so it appears at startup or on the first query. Nothing accepted the TCP connection on 127.0.0.1 port 5432, and the OS refused it immediately.

Error: connect ECONNREFUSED 127.0.0.1:5432
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1611:16) {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 5432
}

Quick fix

Make sure Postgres is running and listening where your app connects. Run pg_isready -h 127.0.0.1 -p 5432 from the same machine or container as the app. If the app runs in Docker, replace localhost with the database service name, since localhost inside a container is the container itself.

  1. Run pg_isready -h <host> -p 5432 from where the app runs.
  2. Start the database (brew services start postgresql@16, docker compose up db, or systemctl start postgresql).
  3. Log the host and port the app resolves at startup, without the password.
  4. In Docker Compose, connect to the service name (db:5432), not localhost.
  5. Add a healthcheck plus depends_on condition: service_healthy so the app waits for Postgres.
bash
pg_isready -h 127.0.0.1 -p 5432
# 127.0.0.1:5432 - no response            -> nothing is listening, start Postgres
# 127.0.0.1:5432 - accepting connections  -> the server is up, check the app's config

# Inside Docker Compose, use the service name instead of localhost
DATABASE_URL=postgres://app:secret@db:5432/app
Fix it with your coding agent

Paste this into Claude Code, Cursor or Codex in the project that throws it. The agent reads this guide in plain text and applies the fix that matches your stack.

prompttext
Read https://runheed.com/errors/econnrefused.md and fix this error in this project.

Why it happens

Postgres is not running

The service crashed, never started after a reboot, or a Homebrew upgrade switched to a versioned service (postgresql@16) that nobody started. pg_isready answers this in one command.

localhost inside a container

In Docker, 127.0.0.1 is the container's own loopback interface. A database in another container is reachable by its Compose service name, and a database on the host machine by host.docker.internal on Docker Desktop.

localhost resolves to ::1

Since Node 17, DNS results keep the operating system order, so localhost can resolve to the IPv6 address ::1 first. On Node 17, 18 before 18.18 and 19, when Postgres listens only on 127.0.0.1 you get connect ECONNREFUSED ::1:5432; newer versions try both addresses by default. Use 127.0.0.1 explicitly or have Postgres listen on both.

Wrong host or port

A second Postgres install listening on 5433, a DATABASE_URL that failed to load (so the driver falls back to its localhost:5432 default), or a typo in the connection string all produce the same refusal.

App starts before the database is ready

In Compose or Kubernetes the app container boots while Postgres is still initializing. A plain depends_on only waits for the container to start, not for the database to accept connections.

Other fixes

  • Retry the first connection with backoff for a few seconds at startup instead of exiting on the first refusal.
  • Check listen_addresses in postgresql.conf and restart Postgres if it should accept TCP connections from other hosts.
  • If the error is an AggregateError listing both ::1 and 127.0.0.1, nothing listens on either: start the server instead of changing the address.

Same error, other wording

  • Error: connect ECONNREFUSED ::1:5432
  • AggregateError [ECONNREFUSED]
  • Error: connect ECONNREFUSED 127.0.0.1:3306

Let your coding agent catch the next one

Runheed records this error in production with the stack trace, the breadcrumbs before it and, when you set it, the agent session that shipped the code, and serves it to Claude Code, Cursor or any MCP client. With it connected, you can ask:

> Find the ECONNREFUSED 5432 errors in production, show which host and port the driver was dialing and whether they began right after a deploy, and add a startup retry with backoff to the database connection.

Setup for Node.js and Express or Vercel Functions, then have your agent fix and resolve it.

Related errors

More in Network and HTTP

Other error topics

Free tools: Source map stack trace decoder · Uptime and SLA downtime calculator · Error budget calculator · Stack trace formatter · JSON log viewer · HTTP status code reference.

References: Node.js errors: common system errors, Node.js dns module (address ordering)

Spotted a mistake? Email hello@greatwork.company. About Runheed.