# Database

This page says what the app's database is, how to read it from outside
the app, and what the platform holds of it. It teaches `durable-sdk db
status`, `durable-sdk db branches` and `durable-sdk db read-string
[--rotate] [--revoke] [--env-file <path>]`.

## What stands

`durable-sdk apps create` makes one Postgres (version 18) per app in the
app's [region](https://durablesdk.com/docs/hosting/regions.md):

- the `main` branch, production's; the engine's tables under the schema
  `durable` and yours under `public`, migrated by the build
  ([Hosting](https://durablesdk.com/docs/hosting.md#the-build));
- the `preview` branch, copy-on-write from `main`, every preview
  deployment's ([Deploy](https://durablesdk.com/docs/hosting/deploy.md#previews));
- compute by tier: Free 0.25 CU fixed, Pro 0.25 to 2 CU, each suspended
  after five minutes idle and woken by the next connection (the first
  request after a sleep waits a second or two); Enterprise by contract;
- history by tier: 1 day on Free, 7 on Pro, 30 on Enterprise. The
  history is what a restore to a moment will read (DS-031).

The compute's numbers on the page are the compute's own, as sized when
the app was made, never the plan's of the moment you read them: a tier
change does not resize a standing database until a resize lands.

### An app scaffolded before 0.1.1

The template's `durable.config.ts` sizes the engine's one pool for the
tier (`POOL_MAX` in `platform.ts`: 5 on Free, 7 on Pro, 20 on
Enterprise) and bounds every connection attempt at ten seconds, so a
compute waking from a suspend never holds an invocation to the
function's deadline. An app made on an earlier tree keeps the tree it
was scaffolded on (`durable-sdk deploy` uploads your own files; the
platform renders nothing into them), so it takes the pool when you
build it this way, with your plan's number:

```ts
import { poolConfig } from "@durable-sdk/postgres";
import { attachPool } from "@durable-sdk/vercel";
import pg from "pg";

const POOL_MAX = 7; // Free 5, Pro 7, Enterprise 20

const pool = attachPool(
  new pg.Pool({
    ...poolConfig(process.env.DATABASE_URL!, {
      max: POOL_MAX,
      applicationName: "durable-app",
    }),
    idleTimeoutMillis: 10_000,
    connectionTimeoutMillis: 10_000,
  }),
);
```

`poolConfig` turns the URL's `sslmode` into the explicit `ssl` setting
(no warning on a cold start) and drops the parameters only a Prisma
client reads. A `durable-sdk init --refresh` that re-renders the
engine's files over a standing tree is not here yet.

## Is it awake, how big is it

```bash
durable-sdk db status
```

```text
durable-sdk db status: orders
  status       active · us-east
  compute      0.25 CU to 2 CU · suspends after 5 min idle · asleep (since 2026-10-29 08:05)
  storage      1.3 GB
  history      7 days · today's state restorable until 2026-11-06 10:00
  maintenance  Mondays 03:00–04:00 UTC
  read string  standing since 2026-10-28 13:00
```

```bash
durable-sdk db branches
```

```text
durable-sdk db branches: orders (2 branches)
  name      kind        size      created
  main      production  1.2 GB    2026-10-28 12:00
  preview   preview     100 MB    2026-10-28 12:01
```

`GET /apps/:slug/database` answers `{ status, region, compute: { minCu,
maxCu, suspendSeconds, state, lastActiveAt, suspendedAt }, storageBytes,
history: { days, retainedUntil }, maintenanceWindow, branches: [{ name,
kind, sizeBytes, createdAt }], live, readString: { present, createdAt,
rotatedAt } }`; the dashboard's Database page shows the same. The
compute's `state` is `active` (awake), `idle` (asleep; the next
connection wakes it) or `init` (starting); `storageBytes` is the
branches' logical sizes summed; `retainedUntil` is the moment until
which the state as of now stays restorable; `maintenanceWindow` is the
weekly hour the platform may restart the compute for updates. The live
half is read from the platform once a minute, never on every request,
and never by connecting, so a sleeping database stays asleep; when it
could not be read, `live` is `false`, the live fields are null or empty,
and the rest of the page stands. A branch is named by its name and kind
(`production`, `preview`, `other` for a restore's backup), never by an
identifier.

## The read string

```bash
durable-sdk db read-string
```

```text
postgresql://read_3f9c2a1e:…@<host>/app?sslmode=require
Shown once. Rotate with: durable-sdk db read-string --rotate
```

The platform creates one role per app on the `main` branch inside a
keyed run, through the sealed owner credential:

```sql
CREATE ROLE read_3f9c2a1e LOGIN NOINHERIT NOCREATEDB NOCREATEROLE NOBYPASSRLS CONNECTION LIMIT 5;
GRANT CONNECT ON DATABASE app TO read_3f9c2a1e;
GRANT pg_read_all_data TO read_3f9c2a1e;
ALTER ROLE read_3f9c2a1e SET statement_timeout = '30s';
```

The URL is handed back once and stored nowhere readable; the role's
password is sealed. A second `read-string` on an app with a standing
role is 409 `plane.database.read_string_exists` with the remedy
`--rotate`. Write it straight into an env file instead of printing it:

```bash
durable-sdk db read-string --env-file .env.local
```

```text
Wrote 1 variable to .env.local (not printed).
```

### What it can do

- Read every table of both schemas: your rows, the engine's runs,
  steps, the ledger, the outbox. `SELECT * FROM durable.ledger ORDER BY
seq` is the audit from outside the app.
- Serve a BI tool, a notebook, a nightly export, a second service that
  only reads.
- Run `durable-sdk verify --url "$URL"`: the read-only probe that says
  where the engine's schema stands against the package.

### What it cannot do

- Write: `pg_read_all_data` grants reads alone, and the role cannot
  undo that itself (`NOINHERIT`, `NOBYPASSRLS`, no `CREATEROLE`).
- Hold more than five connections, or run a statement past 30 seconds.
- Reach the `preview` branch: the role lives on `main`.
- Replace the app's own credential: the app connects as the owner the
  platform sealed at creation, which nobody is handed.

The host inside the read string is the one provider hostname a tenant
sees, said on [the security page](https://durablesdk.com/security) beside the subprocessors.

## Rotate and revoke

```bash
durable-sdk db read-string --rotate
durable-sdk db read-string --revoke
```

`--rotate` resets the role's password and prints the new URL once; the
old URL stops working at once. `--revoke` drops the role. Both need a
key of scope `admin`; a minted or rotated string is a ledger row under
your account naming the role, never the password.

## Backups and restore

The database's history (the days above) is a continuous record every
moment inside it can be read from. Per-app restore to a moment is coming
(DS-031); until then the platform's operator restores from the history
on request.

## Your own migrations

Your tables are yours: any migration tool that runs inside the build
script works. The template's build runs `durable-sdk migrate
--baseline=if-missing` first, for the engine's tables, against
`DATABASE_URL_UNPOOLED`; put yours after it in `package.json`:

```json
{
  "scripts": {
    "build": "durable-sdk migrate --baseline=if-missing && node scripts/migrate.mjs && tsc --noEmit"
  }
}
```

A build whose migration fails is `failed` with its lines in `durable-sdk
deployments events <id>`; production stays on the previous deployment.

---

Prompt for an agent:

```text
Read https://durablesdk.com/docs/hosting/database.md, then read an app's database on durablesdk.com from a laptop or a tool; run durable-sdk doctor --host before and after; a refused line is a refusal id: durable-sdk explain <id>
```
