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.
- Run pg_isready -h <host> -p 5432 from where the app runs.
- Start the database (brew services start postgresql@16, docker compose up db, or systemctl start postgresql).
- Log the host and port the app resolves at startup, without the password.
- In Docker Compose, connect to the service name (db:5432), not localhost.
- Add a healthcheck plus depends_on condition: service_healthy so the app waits for Postgres.
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/appPaste 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.
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
- Error: read ECONNRESET
- TypeError: fetch failed
- Error: listen EADDRINUSE: address already in use :::3000
More in Network and HTTP
- Error: connect ETIMEDOUT 10.0.4.12:5432Node.js
- Error: self-signed certificate in certificate chainNode.js
- Error: getaddrinfo ENOTFOUND api.example.comNode.js
- Error: write EPIPENode.js
- Access to fetch at 'https://api.example.com/orders' from origin 'https://app.example.com'...Node.js
- AxiosError: Request failed with status code 401Node.js
- FunctionsHttpError: Edge Function returned a non-2xx status codeSupabase
- requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.example.com'...Python
- httpx.ReadTimeout: The read operation timed outPython
- ConnectionResetError: [Errno 104] Connection reset by peerPython
- BrokenPipeError: [Errno 32] Broken pipePython
- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer...Python
- Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the clientNode.js
- 422 Unprocessable Entity: {"detail":[{"type":"missing","loc":["body","email"],"msg":"Field...Python
- No signatures found matching the expected signature for payload. Are you passing the raw...Node.js
- TokenExpiredError: jwt expiredNode.js
- Error: 429 You exceeded your current quota, please check your plan and billing details.Node.js
Other error topics
- error: insert or update on table "orders" violates foreign key constraint...Databases
- PrismaClientInitializationError: Can't reach database server at `db.example.com:5432`Databases
- error: relation "todo" does not existDatabases
- ImportError: libGL.so.1: cannot open shared object file: No such file or directoryImports and modules
- SyntaxError: Cannot use import statement outside a moduleImports and modules
- ImportError: attempted relative import with no known parent packageImports and modules
- Error: NEXT_REDIRECTReact and Next.js rendering
- Objects are not valid as a React child (found: object with keys {id, name, total}). If...React and Next.js rendering
- Dynamic server usage: Route /x couldn't be rendered statically because it used `headers`.React and Next.js rendering
- ValueError: too many values to unpack (expected 2)Undefined, None and type errors
- TypeError: Cannot destructure property 'id' of 'undefined' as it is undefined.Undefined, None and type errors
- KeyError: 'email'Undefined, None and type errors
- TypeError: Object of type datetime is not JSON serializableJSON, parsing and validation
- SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSONJSON, parsing and validation
- ZodError: [ { "expected": "string", "code": "invalid_type", "path": [ "email" ]...JSON, parsing and validation
- Error: ENOENT: no such file or directory, open '/app/config.json'Memory, async and the process
- django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.Memory, async and the process
- RuntimeWarning: coroutine 'fetch_user' was never awaitedMemory, async and the process
- McpError: MCP error -32001: Request timed outMCP servers and clients
- Bad Request: No valid session ID providedMCP servers and clients
- Error executing tool get_orderMCP servers and clients
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.