BlogPostgres

Error: connect ECONNREFUSED 127.0.0.1:5432 How to fix it.

Your app tried to connect to a Postgres database on its own machine, and there is none. It needs the hosted database instead.

How the Agent API works
What you see
Error: connect ECONNREFUSED 127.0.0.1:5432
    at TCPConnectWrap.afterConnect [as oncomplete]
  • What it meansNothing listens on 127.0.0.1:5432
  • Fix oneCreate a hosted database
  • Fix twoRead DATABASE_URL
  • Fix threeRemove the localhost fallback

What it means

127.0.0.1 is the machine the app runs on, and 5432 is the usual Postgres port. On your laptop, a local Postgres may be running there. On the server, nothing is. The app was told to look for a database on its own machine.

How to fix it

  1. Make sure your app has a database

    Ask your agent: "Add a Postgres database to this project." Antideploy creates one beside your app and sets DATABASE_URL. See how to add a Postgres database.

  2. Read DATABASE_URL

    Your code must use the variable and not a fixed address.

  3. Remove the localhost fallback

    A line such as process.env.DATABASE_URL || "postgres://localhost/mydb" quietly uses localhost when the variable is missing. Remove the fallback, so a missing variable fails loudly.

Node
const pool = new pg.Pool({
  connectionString: process.env.DATABASE_URL,
});

If you use your own database

If you use Supabase, your own Neon project or another Postgres, save its connection string as DATABASE_URL. A value you set yourself always wins, and Antideploy creates nothing.

01Questions

Connection questions, answered.

Anything else? Write to us and a person answers.

support@antideploy.com
Which address does the database have?

You do not need to know. Read DATABASE_URL from the environment. It is set in the running app.

Why did it work locally?

A local Postgres was running on your machine.

Does this failed deploy cost anything?

No. Failed deploys never count against your plan.

Deploy something. Start with one sentence.

Paste one sentence into your coding agent, click Approve once, and get a live link. No card, no trial clock.

Start from GitHub or a folder
Prompt copied Paste it into Claude Code, Codex or Cursor and press Enter.