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

URL: https://antideploy.com/blog/econnrefused-127-0-0-1-5432-cannot-connect-to-the-database

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

**What you see**

```
Error: connect ECONNREFUSED 127.0.0.1:5432
    at TCPConnectWrap.afterConnect [as oncomplete]
```

## At a glance

- **What it means**: Nothing listens on 127.0.0.1:5432
- **Fix one**: Create a hosted database
- **Fix two**: Read DATABASE_URL
- **Fix three**: Remove the localhost fallback

Published 7 October 2026. Facts checked 7 October 2026. 1 min read.

## 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](https://antideploy.com/blog/how-to-add-a-postgres-database-to-your-app).

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

## Connection questions, answered.

Anything else? Write to us and a person answers.

[support@antideploy.com](mailto: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.

## More guides, and the platform.

Each platform page covers one part of Antideploy: what it does, where it stops and the questions people ask. [See the whole platform](https://antideploy.com/platform), or [all the guides](https://antideploy.com/blog).

- [How to add a Postgres database to your app](https://antideploy.com/blog/how-to-add-a-postgres-database-to-your-app): Ask your agent. A Postgres 17 database is created beside your app and DATABASE_URL is set.
- [relation "users" does not exist](https://antideploy.com/blog/relation-does-not-exist-database-tables-missing): The database exists but the tables do not. Your migration did not run, or was not detected.
- [Why does my app work on localhost but not online?](https://antideploy.com/blog/why-does-my-app-work-on-localhost-but-not-online): Nine common reasons, how to check each one, and how to fix it.

- [Database](https://antideploy.com/platform/database)
- [Environment variables](https://antideploy.com/platform/environment-variables)
- [Deployments](https://antideploy.com/platform/deployments)

Facts on this page were checked against the live platform on 7 October 2026.

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

To set this up, give your coding agent this sentence:

```text
Set this project up to deploy on Antideploy. Fetch https://antideploy.com/agent.md and follow it.
```
