# Postgres database requirements (https://docs.opensequence.dev/docs/self-hosting/database)

What OpenSequence needs from Postgres: version, schemas, how migrations run, pool size behind a pooler, and how to back up and restore.

OpenSequence keeps all of its state in one Postgres database. Read this page when you provision that database, size its connections or plan backups.

```bash
SEQ_DB_DSN=postgres://oseq:secret@db.internal:5432/opensequence   # the only database setting
SEQ_DB_POOL=10                                                     # connections per process (1 to 100)
SEQ_RUN_MIGRATIONS=1                                               # apply migrations at boot
```

## Requirements [#requirements]

Postgres 14 or newer, with no extensions. Ids are generated by the service (prefixed ULIDs), timers are rows, the scheduler uses lease rows, and the `postgres` queue is a table read with `FOR UPDATE SKIP LOCKED`.

The service uses no session-level features: no advisory locks, no `LISTEN`, and prepared statements are turned off in the driver. So a transaction pooler works, such as PgBouncer in transaction mode or a cloud provider's pooler.

## The schemas [#the-schemas]

The service creates and owns two schemas and touches nothing else, so it can share a database with your app, such as a Supabase project. Use a role that can create schemas, and leave `seq` and `seq_auth` to OpenSequence.

| Schema | Owner | Tables |
|---|---|---|
| `seq` | the engine | `projects`, `api_keys`, `webhook_endpoints`, `webhook_deliveries`, `idempotency_keys`, `senders`, `sender_channels`, `contacts`, `suppressions`, `sequences`, `versions`, `enrollments`, `runs`, `messages`, `tasks`, `signals`, `signal_effects`, `flags`, `job_locks`, `queue_messages`, `queue_dead`, plus `migrations` and `migration_lock` |
| `seq_auth` | the auth module | `credentials` (a project's provider secrets), `connections` (a sender's SMTP and IMAP passwords), plus `migrations` and `migration_lock` |

Every tenant-owned table carries `project_id`. Secrets are AES-256-GCM ciphertext under `SEQ_ENCRYPTION_KEYS`, and each row records the id of the key that encrypted it. They live in `seq_auth.credentials`, `seq_auth.connections` and `seq.webhook_endpoints`.

## Migrations [#migrations]

The migrations are SQL files shipped in the image, one folder per schema. Two ways run them, and both do the same thing:

```bash
SEQ_RUN_MIGRATIONS=1                 # every process migrates at boot, before it opens its port
node dist/cli.js migrate             # oseq migrate: once, from a pipeline or a Job
```

- Each migration runs in its own transaction, serialized by `SELECT … FOR UPDATE` on the schema's `migration_lock` row. Replicas that start together wait for each other instead of racing, and a transaction pooler is fine.
- Applied migrations are recorded in `seq.migrations` and `seq_auth.migrations` by tag and SHA-256 hash. A file changed after it was applied is refused with an error, never silently re-run.
- Running again is a no-op: `oseq migrate` prints `already up to date`.
- A failed migration rolls back its transaction. At boot, the process then exits with code 1.

When `SEQ_RUN_MIGRATIONS` is off, nothing checks the schema at boot. Run `oseq migrate` before you start the new image. See [Upgrades](/docs/self-hosting/upgrades) for the order.

## The queue [#the-queue]

The queue lives in Postgres by default (`SEQ_QUEUE=postgres`), so you need no Redis. Messages sit in `seq.queue_messages` and are read with `FOR UPDATE SKIP LOCKED`. Enqueuing joins the transaction that records the work, so a recorded step and its queued message never diverge.

| `SEQ_QUEUE` | Use it for |
|---|---|
| `postgres` | The default. Nothing else to run. |
| `redis` | More throughput on large installs, with `SEQ_REDIS_DSN` and Redis persistence on. A message can arrive twice after a crash; the executors are idempotent. |
| `memory` | Development and tests, one process only. |

The worker reads four queues: `seq_email`, `seq_sms`, `seq_signals` and `seq_webhooks`. Their depths are in `GET /v1/admin/engine/health`.

## Pool size [#pool-size]

`SEQ_DB_POOL` (default 10) is per process. A worker reads four queues (`seq_email`, `seq_sms`, `seq_signals`, `seq_webhooks`) with up to `SEQ_CONCURRENCY` messages in flight each, so 32 by default, plus the scheduler jobs. They share the pool: a query waits for a free connection rather than failing. Most work is short, so 10 is enough at the default concurrency. Raise both together.

Behind a pooler, size the pooler for the sum over every process: `replicas × SEQ_DB_POOL` for each role.

## Backups and restore [#backups-and-restore]

```bash
pg_dump --format=custom --schema=seq --schema=auth "$SEQ_DB_DSN" > oseq.dump
pg_restore --dbname="$NEW_DSN" oseq.dump
```

Back up the database and `SEQ_ENCRYPTION_KEYS` together. A database restored without the key that encrypted its secrets has unreadable credentials: every mailbox password, Allo and Attio key and webhook secret must be entered again.

There is no state outside the database, except queue messages when `SEQ_QUEUE=redis`. Losing those costs only a delay: the reaper rebuilds the work from the tables.

## Growth [#growth]

The ledgers (`runs`, `messages`, `signals`, `webhook_deliveries`) grow by a few rows per enrollment per step. The `retention` job, which runs at boot and then daily, deletes:

| Rows | After |
|---|---|
| `idempotency_keys` | 1 day |
| `queue_dead` | 30 days |
| delivered `webhook_deliveries` | 30 days |

Runs, messages, signals and tasks are kept until the contact is erased. The `retention_days` project setting is accepted but not enforced yet. See [Data retention](/docs/security/data-retention).
