
# Postgres in the browser

The official `postgres:17-alpine` image, run in a tab: no server, no WASM build of Postgres, the
same binary Docker runs.

## 1. Build the image

```sh
untainer build postgres:17-alpine --probe tcp:5432 -e POSTGRES_PASSWORD=dev \
  --profile-script "psql -U postgres -c 'select 1'"
```

- `--probe tcp:5432` starts the image once while building: the build records when it is ready,
  lays the image out by what its start reads, and records the memory it needs (224 MiB for the
  start alone; with a script of real queries, 352 MiB: give `--profile-script` what your page will
  ask of it).
- `-e` is the environment of that start only; it is not recorded in the image.

Copy `dist/postgres-17-alpine/` next to your page, or publish it (see
[Hosting](/guides/hosting)).

## 2. Run it

```ts [main.ts]
import { createRuntime, loadImage } from 'untainer';

const image = await loadImage('/images/postgres-17-alpine/');
const rt = await createRuntime({ worker: true, images: [image] });
const db = await rt.run(image, { env: { POSTGRES_PASSWORD: 'dev' } });
await db.ready({ timeout: 120_000 });

const r = await db.exec('psql', ['-U', 'postgres', '-c', 'select version()'], { user: 'postgres' });
console.log(r.stdout);
```

- `worker: true` (the default, written out) keeps the VM off the page's thread.
- Without `memoryMiB`, the runtime boots with what the image recorded.
- `ready()` waits for the probe the build recorded (TCP 5432), after `initdb`.

Measured in Chrome from a server on the same machine ([`spikes/21-stores/browser.mjs`](https://github.com/OskarLebuda/untainer/blob/main/spikes/21-stores/browser.mjs)): ready in 5.9 s on the
first visit, after 12 requests and 29.2 MB. The chunks are kept in the origin's storage, so the
next visit downloads only the manifest.

## Keeping the data

The database lives in the container's writable layer, a tmpfs in the guest's memory, gone when
the tab closes. That layer is also bounded by half the VM's memory: a database that grows beyond
it fails with `No space left on device` ([`spikes/30-guard-healthy/postgres-load.ts`](https://github.com/OskarLebuda/untainer/blob/main/spikes/30-guard-healthy/postgres-load.ts)). To keep the
data across visits, give the runtime a persistent disk and put the data directory on it:

```ts [disk.ts]
import { createRuntime, diskVolume, loadImage } from 'untainer';

const image = await loadImage('/images/postgres-17-alpine/');
const rt = await createRuntime({ worker: true, images: [image], disk: { name: 'app-db' } });
const db = await rt.run(image, {
  env: { POSTGRES_PASSWORD: 'dev' },
  volumes: { '/var/lib/postgresql/data': diskVolume('pgdata') },
});
await db.ready({ timeout: 120_000 });
await db.exec('psql', ['-U', 'postgres', '-c', 'create table if not exists notes (body text)'], {
  user: 'postgres',
});
```

The disk is an ext4 file system in the origin's IndexedDB; one runtime at a time has it (a second
tab gets `ERR_UNSUPPORTED`). The browser tests check that its files outlive a reload
([`test/browser.browser.test.ts`](https://github.com/OskarLebuda/untainer/blob/main/packages/untainer/test/browser.browser.test.ts)); [`examples/compose`](https://github.com/OskarLebuda/untainer/tree/main/examples/compose) runs Postgres on a volume, on Node.
