# Postgres

> Postgres, powered by Neon: a database of the team that scales its compute with the load, sleeps when idle and can be put back to any moment of its restore history.

Postgres is a resource of the team: a Postgres database, version 17, 18 or 19, powered by [Neon](https://neon.com). Its compute scales between two sizes with the load, it sleeps when nothing is connected, and it can be put back to any moment of its restore history. You attach a database to any number of your apps; each gets the connection as variables Laravel and Node.js already read (`DB_HOST`, `DB_PASSWORD`, `DATABASE_URL` and the rest).

Every Postgres client works with it: Laravel's `pgsql` driver, node-postgres (`pg`), Prisma, Drizzle, Knex, TypeORM, Sequelize, psql.

## Create one

Open **Resources**, **New Postgres database**:

| Field | What to enter |
| --- | --- |
| **Name** | Lower-case letters, digits and dashes, such as `db` or `reports` |
| **Region** | The region of the app that uses it. The database runs in Neon's nearest region; a far one adds latency to every query, and it cannot move later |
| **Version** | Postgres 17, 18 or 19; 18 unless you pick another. See [Version](#version) |
| **Preset** | **Dev**, **Prod**, **Scale** or **Custom**, see [Presets](#presets) |

With **Custom** you pick the fields yourself:

| Field | What to enter |
| --- | --- |
| **Smallest compute** | The size it never goes under while it runs, in CU. 1 CU is about 4 GB of memory |
| **Largest compute** | The most it may scale to under load, as far as your plan allows, see [Limits](#limits) |
| **Sleep after** | How long it waits with nothing connected before it sleeps: 1, 5 or 15 minutes idle, 5 unless you pick another, see [Sleep when idle](#sleep-when-idle) |

The dialog says what it costs before you create it with **Create database**. A database past the number your plan allows, or a compute size past its largest, is refused in the dialog with the reason. The database is ready in a few seconds; its page says when.

Sizes come in steps: 0.25, 0.5, 1, 2, 3, 4 CU. Storage is not chosen: every database of the team gets the cap of the plan, and writes fail once it is full.

### Version

A new database runs Postgres 17, 18 or 19, as picked in the dialog. Pick the version your app is tested with; without a reason for another, keep 18.

- Minor releases (bug and security fixes) are applied for you, the next time the database wakes or restarts.
- The major version cannot change later. To move to another one, create a new database on it, copy the data over with `pg_dump` and `pg_restore`, attach the new database to the app and delete the old one.
- Postgres 19 is new: a few extensions are not available on it yet. Check [Neon's list of extensions](https://neon.com/docs/extensions/pg-extensions) before you pick it.

The database's page and the list under **Resources** say the version it runs.

### Presets

A preset fills in the compute range, **Sleep when idle** and the restore history for you. Each one shows **At most** what a month can cost: its largest size running around the clock for the longest month, 31 days, asleep or not. Storage, restore history and transfer past 100 GB a month are charged by use, with no ceiling, on top.

| Preset | Compute | Sleeps when idle | Restore history | At most |
| --- | --- | --- | --- | --- |
| **Dev** | 0.25 CU | After 5 minutes | 1 day | $29.57 a month |
| **Prod** | 0.25 to 1 CU | After 5 minutes | 7 days | $118.30 a month |
| **Scale** | 1 to 4 CU | Never | 7 days | $473.18 a month |

A preset past your plan is lowered to what the plan allows, and the dialog says so: on Pro, Scale goes up to 2 CU; on Hobby, Prod keeps 3 days of restore history and Scale runs at 1 CU; during the trial every preset is 0.25 CU with 1 day of history and sleeps when idle. Everything a preset sets can be changed on the database's page later.

## Attach it to an app

On the database's page, **Attach to an app**, pick the app and a variable prefix: `DB` for the first database on an app. The app gets these variables, set by the platform like `PORT`:

| Variable | Value |
| --- | --- |
| `DB_CONNECTION` | `pgsql` |
| `DB_URL` | `postgresql://app:password@<host>:5432/app?sslmode=require&channel_binding=require` |
| `DB_HOST` | The database's host, a Neon host name such as `ep-….neon.tech` |
| `DB_PORT` | `5432` |
| `DB_DATABASE` | `app` |
| `DB_USERNAME` | `app` |
| `DB_PASSWORD` | The password |
| `DATABASE_URL` | The same URL as `DB_URL`, for Node.js tools |
| `PGCHANNELBINDING` | `require`, read by psql and PDO for every connection (see below); set once per app, whatever the prefix |

They reach the app with its next redeploy: the pill next to the app's status says **New resources**, and **Redeploy now** applies it. They are listed under **Variables** in **Provided by the platform**; the URLs and the password are readable only by the owner and the admins. **Detach** takes them off again, with the next redeploy.

A second database on the same app gets a prefix of its own, `DB_REPORTS` for the one named `reports`, or any prefix you type: `DB_REPORTS_URL`, `DB_REPORTS_HOST`, `DB_REPORTS_PORT`, `DB_REPORTS_DATABASE`, `DB_REPORTS_USERNAME` and `DB_REPORTS_PASSWORD`, without `DB_CONNECTION` and `DATABASE_URL`.

## Connect from Laravel

Laravel reads the `DB_*` variables as they are; nothing to change in `config/database.php`. `DB_URL` carries `sslmode=require` and `channel_binding=require`, which Neon asks for: the connection is encrypted, and the sign-in is bound to the server's certificate so nobody in between can stand in for the database. Laravel passes `sslmode` on to Postgres and leaves `channel_binding` out of the connection it builds, which is why the app also gets `PGCHANNELBINDING=require`: the Postgres client library reads it for every connection, so your Laravel app gets both without any change. The variable applies to every Postgres connection of the app: if the app also connects to another Postgres server that cannot bind the sign-in to TLS, add your own `PGCHANNELBINDING` variable with the value `prefer`; the platform then leaves yours as it is.

```php
// config/database.php (as Laravel ships it)
'pgsql' => [
    'driver' => 'pgsql',
    'url' => env('DB_URL'),
    'host' => env('DB_HOST', '127.0.0.1'),
    // ...
],
```

Run the migrations as a [deploy command](https://arrangic.com/docs/deploy-commands.md): `php artisan migrate --force`. A second database is a second connection in `config/database.php` that reads `env('DB_REPORTS_URL')`.

## Connect from Node.js

```js

const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });
const { rows } = await pool.query('select now()');
```

Prisma reads `DATABASE_URL` from `schema.prisma` (`url = env("DATABASE_URL")`), Drizzle and Knex take it as their connection string. Run migrations as a [deploy command](https://arrangic.com/docs/deploy-commands.md), such as `npx prisma migrate deploy`.

## Pooled or direct

The page lists two hosts:

- **Host,** direct to the database. This is what apps get. Migrations, prepared statements, `LISTEN` and advisory locks need it, because they keep a session.
- **Pooled host,** through Neon's connection pooler in transaction mode. It takes thousands of short connections, for many replicas, workers or serverless code. It keeps no session: no prepared statements across queries, no `SET` that lasts, no advisory locks. Use it only when the direct host runs out of connections, by setting your own variable with the **Pooled URL**.

## Connect from your computer

The database answers from anywhere, over TLS and with the password. On its page the owner and the admins reveal the password and the URLs, or **Copy as .env** copies the variables above with their values.

```bash
psql "postgresql://app:<password>@<host>:5432/app?sslmode=require&channel_binding=require"
```

## Compute

A database scales between its **Smallest compute** and **Largest compute** by itself, as the load asks, and is billed for the size it runs at. Change the range on its page under **Compute and storage**; the change is applied at once and connections may drop for a moment. A size past the largest your plan allows is refused. The card says the most a month can cost: the largest compute you pick running around the clock, plus the data and restore history the database keeps today at their monthly prices. It follows the size as you change it, and a sleeping database shows it too. With **Custom** in the dialog, the same figure follows the largest compute you pick.

## Sleep when idle

The switch **Sleep when idle** on the database's page is on for a new database.

- **On:** after the idle time you choose with nothing connected, the database sleeps and its compute costs nothing. The next connection wakes it, which takes under a second.
- **Off:** it keeps running with nothing connected, so a query never waits for it to wake. Its compute is charged around the clock, at least the smallest size.

While the switch is on, **Sleep after** next to it sets how long the database waits with nothing connected: 1, 5 or 15 minutes idle, 5 for a new database. You pick it in the **New Postgres database** dialog too, and change it any time on the database's page; it takes effect in a moment, with connections left open. Every plan has all three.

- **Shorter** saves compute: a database that gets a few queries an hour runs for less of it.
- **Longer** wakes less often: each wake adds about half a second to the first request after it, so an app with a query every few minutes is better served by 15 minutes.

**During the trial a database always sleeps when idle,** after the idle time the team chose. Switching **Sleep when idle** off is refused with "During the trial a database sleeps when idle. Choose a plan to keep it running." Once you choose a plan, you can switch it off.

**A scheduler keeps it awake.** Laravel's scheduler running every minute, a queue worker on the `database` driver or anything else that queries every few minutes never lets it sleep, so its compute runs all month. Keep queues in a [Valkey cache](https://arrangic.com/docs/redis.md), and count on a full month of the smallest compute when an app queries all the time.

When the team's apps are paused because the trial or the plan ended, the credit is used up or a payment stays unpaid, the database refuses connections until the team may run apps again. Its data is kept.

## Metrics

The database's page charts its compute and its data, over the last hour up to the days your plan keeps: 3 on Hobby and during the trial, 7 on Pro, 15 on Business ([how far back you can look](https://arrangic.com/docs/metrics.md#how-far-back-you-can-look)). A range further back than your plan reaches is shown with a lock. Times are in UTC.

| Group | Charts |
| --- | --- |
| **Compute** | **Compute:** the CU it ran at on average, against a line at its largest compute. **Awake:** the share of the time it was awake, in % |
| **Data** | **Storage:** the data it holds, against the storage cap of your plan. **Restore history:** what the history keeps |

Compute and restore history come from the hourly usage the bill is made of, so they move by the hour and the last hour or two arrive a little later. Storage and awake are checked every five minutes. A stretch nothing was counted for stays empty rather than reading as zero. The charts reload every minute.

Looking at the charts never wakes the database: they are drawn from what the platform has already counted, without asking the database anything, so a sleeping one stays asleep.

## Slow queries

**Show slow queries** on the database's page lists the slowest statements Postgres has recorded since its statistics were last reset, slowest by mean time first:

| Column | What it shows |
| --- | --- |
| **Statement** | The normalised text Postgres keeps, with the values taken out (`$1`, `$2`), cut at 2,000 characters |
| **Calls** | How many times it ran |
| **Mean** | Its average time |
| **Total** | Its time over all the calls |
| **Rows** | The rows it returned or changed over all the calls |

Up to 20 statements a read. **Read again** reads them anew. Only the owner and the admins see this, since a statement's text can carry data.

- **Reading wakes the database.** It opens a session on it, so a sleeping database wakes and runs a few seconds of compute. Each person can read (or enable) a database's slow queries 10 times a minute, 20 times a minute across all databases, and a team 30 times a minute in all; one read of a database runs at a time, and when many reads run across the platform at once the page asks you to try again in a minute. A database that is not ready, whose connections are turned off, or whose team's apps are paused is not read.
- **The statistics come from the `pg_stat_statements` extension.** While it is not on, the page says "No statistics yet" and **Enable pg_stat_statements** turns it on: it runs `CREATE EXTENSION pg_stat_statements` on the database as its own role. The extension keeps a little memory for its statistics. You can also run the same statement yourself with psql.
- **They start over** when the database restarts and when it sleeps, so after a wake the list holds only what ran since. Under the list the page says since when.
- **How it is read.** With the database's own role, in a read-only session with short time limits (5 seconds to connect, 5 seconds a statement, 2 seconds waiting on a lock), in a separate short-lived process that is stopped after 20 seconds whatever happens, so a read never waits longer than that. Only our fixed statements run; nothing you wrote is ever run. The reads count among the statements too. Each read is noted in the team's activity.

"The database took too long to answer. It may be waking up: try again in a moment." means the time limit ran out while it woke: read again.

## Restore

Neon keeps the history of every change for the days of the **Restore history** on the database's page: 1, 3, 7 days, as far as your plan allows (3 on Hobby, 7 on Pro and Business, 1 during the trial). A longer history costs more, see [What it costs](#what-it-costs).

To put the database back, pick a moment under **Put it back to**, in your own time zone, then **Restore** and type the database's name to confirm. The data goes back to how it was at that moment and everything written after it is lost. Connections drop for a moment; the host, the password and the variables stay the same, so apps need nothing. A moment outside the restore history, or in the future, is refused.

## A new password

Should the password get out, **New password** on the database's page, under Connection, replaces it: the old one stops working at once. Every attached app gets the new `DB_URL`, `DB_PASSWORD` and `DATABASE_URL` and is marked for a redeploy; its connections fail until you redeploy it, so do that right after. Only the owner and the admins can do this.

## What it costs

A database is counted by the hour in three parts, plus transfer:

- **Compute:** $0.159 per CU-hour while it runs. 0.25 CU running a full hour is a quarter of that. Asleep, nothing. A database at 1 CU around the clock for 31 days comes to $118.30, the most the **Prod** preset can cost in compute.
- **Storage:** $0.525 per GB a month of the data it holds.
- **Restore history:** $0.30 per GB a month of the changes the history keeps. It grows with how much is written, not with the size of the data.
- **Transfer:** the first 100 GB a database sends out each month are included; past that $0.15 per GB.

Asleep, a database costs $0 in compute: only its storage and restore history are counted. With **Sleep when idle** off it never sleeps, so its compute runs all month, at least at its smallest size. The most each preset's compute can cost in a month, at its largest size around the clock: **Dev** $29.57, **Prod** $118.30, **Scale** $473.18.

An example: a database at 0.25 CU that is awake 8 hours a day and asleep the rest, for 30 days, holding 2 GB of data with 0.5 GB of restore history and sending less than 100 GB:

| Part | Counted | Cost |
| --- | --- | --- |
| Compute | 0.25 CU for 240 hours awake | $9.54 |
| Compute asleep | The other hours | $0 |
| Storage | 2 GB for the month | $1.05 |
| Restore history | 0.5 GB for the month | $0.15 |
| Transfer | Under 100 GB | $0 |
| **The month** | | **$10.74** |

A database deleted on the tenth is charged for ten days. The bill has one line per database, with the parts inside it; [Pricing and billing](https://arrangic.com/docs/billing.md#postgres) lists every price.

### During the trial

A database made during the [trial](https://arrangic.com/docs/billing.md#the-trial) is free while the trial lasts: its usage does not count against the trial credit. Its page says "Free during the trial: this database's usage is not charged. Once you choose a plan, it is charged by use from then on." When you choose a plan, the database stays as it is, with the same connection details, and from then on it is charged by use like any other database. The trial's [limits](#limits) stay the same.

### Usage this billing period

The database's page shows what it used so far in the team's current billing period, counted every hour; the last hour or two arrive a little later.

| Figure | What it is |
| --- | --- |
| **Compute** | CU-hours: the size it ran at, by the hours it ran |
| **Storage** | The data it held, on average over the period |
| **Restore history** | What the history kept, on average over the period |
| **Transfer** | What it sent out, with the 100 GB free a month |

The owner and the admins see what each part comes to and the total **So far**. Members see the same figures without the amounts.

## Delete

**Delete** on the database's page, then **Delete database** with the name typed to confirm. The database, its data and its restore history go at once, and every attached app loses the variables with its next redeploy. It cannot be restored.

## Limits

- Databases per team, the largest compute and the storage cap: 1 up to 1 CU and 10 GB on Hobby, 3 up to 2 CU and 50 GB on Pro, 10 up to 4 CU and 100 GB on Business, 1 up to 0.25 CU and 512 MB during the trial.
- Restore history: 1, 3, 7 days, up to 3 on Hobby, 7 on Pro and Business, 1 during the trial.
- Metrics: 3 days on Hobby and during the trial, 7 on Pro, 15 on Business.
- Slow queries: up to 20 statements a read, each cut at 2,000 characters; 10 reads a minute per person and database.
- One database can be attached to any number of the team's apps.
- Regions: Neon's nearest region to the ones apps run in. A database cannot move to another region.

## Who can do what

Members create a database, attach and detach it, change its compute, switch **Sleep when idle**, choose how long it waits before it sleeps and choose the restore history. They see its metrics and its usage this billing period, without the amounts. Only the owner and the admins see the password and the URLs, see what the usage comes to, read the slow queries and enable `pg_stat_statements`, replace the password, restore it to a moment and delete it. See [Teams](https://arrangic.com/docs/teams.md).

---

This page on the site: https://arrangic.com/docs/postgres  
All pages of the documentation: https://arrangic.com/llms.txt
