# Error: connect ECONNREFUSED 127.0.0.1:5432 (Node.js)

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
```

## 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`

## Sources

- [Node.js errors: common system errors](https://nodejs.org/api/errors.html)
- [Node.js dns module (address ordering)](https://nodejs.org/api/dns.html)

---

To see this error the next time it happens in production, with the stack trace and the steps before it: Runheed records it and serves it to your coding agent over MCP. Setup instructions an agent can follow: https://runheed.com/SETUP.txt

Human-readable version: https://runheed.com/errors/econnrefused
