Launch offer: everything is 100% off. Download any skill free, no sign-up. Get the skills
TechFixError fixesBroke after deploy
Broke after deploy

ECONNREFUSED 127.0.0.1:5432 in production

The error
Error: connect ECONNREFUSED 127.0.0.1:5432
    errno: -111,
    code: 'ECONNREFUSED',
    syscall: 'connect',
    address: '127.0.0.1',
    port: 5432
Short answer

Your app is looking for Postgres on localhost, which in production means the server itself, and there is no database there. Set the real database URL in your host's production variables and redeploy.

Why it happens

127.0.0.1 (or ::1) is "this machine". Port 5432 is Postgres. Locally that is your own database. In production there is nothing listening there, so the connection is refused.

The usual reasons:

Fix it by hand

  1. Check the variable on your host. On Vercel, open Settings, then Environment Variables, and confirm DATABASE_URL (or whatever your code reads) exists and has Production ticked. Other hosts have the same idea under a different name.
  2. Check the code reads it. Pass the URL explicitly so a missing value is obvious:
    import pg from 'pg';
    if (!process.env.DATABASE_URL) throw new Error('DATABASE_URL is not set');
    const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });
  3. Use a database production can reach. If the only database you have is on your laptop, create a hosted one and copy its connection string into the production variable.
  4. In Docker Compose, use the service name as the host. If your database service is called db, the URL is postgres://user:pass@db:5432/app, not localhost.
  5. Redeploy. On Vercel, changed environment variables only apply to new deployments, so the running one keeps the old value until you deploy again.
  6. If the error changes to a timeout or P1001, the address is now right but the database is refusing your host. See the P1001 fix.

Stop it happening again

Let TechFix do this for you

Broke After Deploy $9

Broke After Deploy flags database addresses that point at your own computer, env variables your code uses that production may not have, and config that only exists in a gitignored .env. Give it your host's variable names and missing ones become definite findings.

  • Checks your project for 37 known causes of "works locally, breaks in production" and runs your production build.
  • Paste the error and it finds the cause in your code, then lists what will break next.
  • Verdict first. Every finding shows the file and line, what it breaks, and the fix as a diff that is applied only with your OK.

Broke After Deploy: how it works, with examples · Install guide

Works with Claude Code, Claude.ai, Cursor, Codex and other agents that read SKILL.md files. No installs, no accounts, works offline. No-questions refund.

Sources (checked 2026-10-13)