Skip to main content

Run Claviger on PostgreSQL, with one or several replicas

DEVELOPER GUIDE

Point Claviger at a PostgreSQL database, know what changes from SQLite, back it up, and run several replicas.

Who this guide is for

Developers and self-hosters who run Claviger and can set its environment variables and reach a PostgreSQL 16 or 17 server.

Why it matters

PostgreSQL is supported but opt-in. SQLite stays the default. On PostgreSQL the zip backups no longer hold the database, so a missing pg_dump schedule means no database backup at all.

What you will do, in one sentence

Set CLAVIGER_DB_URL to an empty database, add CLAVIGER_MULTI_REPLICA=true only if you need several replicas, and back up with pg_dump.

Screens are captured from a seeded test system. Every name, address and number on the screens is invented. Prepared 15 November 2026.

1. Turn PostgreSQL on​

Set one environment variable before you start Claviger:

CLAVIGER_DB_URL='postgres://claviger:[email protected]:5432/claviger?sslmode=require'
  • Unset, the default, means SQLite with the behavior you already know.
  • PostgreSQL 16 or 17 is supported. CI runs the Postgres tests on both.
  • Claviger creates its tables in the public schema. Use an empty, dedicated database.
  • The connection string holds the password. Keep it in your secret store.
  • The request logs file auxiliary.db stays a local SQLite file in pb_data, one for each replica. It is disposable. OTLP log export can ship the logs somewhere durable.

To check which engine runs, call GET /api/health as a superuser. The answer has data.database, which is sqlite or postgres.

2. What differs from SQLite​

The same request on the same data gives the same records on both databases. The tests compare about 400 cases. These differences remain:

CaseOn PostgreSQL
A text field compared with a number, such as title = 5Error 400. Postgres does not coerce types.
A bool field compared with a number, such as active = 1Error 400. Use true and false.
A multiple select, relation or file field compared as a wholeMatches jsonb text. Use ?= or :each.
JSON valuesStored as JSONB. Key order and spacing are not kept. Parsed JSON is the same.
SortingText sorts byte by byte. Z sorts before a.

Your own SQL runs as written, so it must be PostgreSQL SQL:

  • View collections. Quote names that have capitals, and give the view an id column.
  • The superuser /api/sql endpoint and $app.db() in JS hooks.
  • Raw SQL in your own migrations. Prefer the portable builders where you can.

Claviger does not translate your SQL. Dates stay text in the same format as before.

3. Back up and restore​

Zip backups are a SQLite feature. On PostgreSQL a backup still works. It writes the storage files and the local auxiliary.db, but not the database. The archive manifest says so.

  • Back up the database with pg_dump, pg_basebackup, or point-in-time recovery from your provider. Claviger does not run it for you.
  • Keep the dump and the storage files of the same moment together. The file names are referenced from the records.
  • Verifying such an archive reports skipped: database is Postgres. That is not a failure.
  • A zip restore is refused with a 400. Restore the database with pg_restore or point-in-time recovery, then copy the storage files back.
  • Leave LITESTREAM_* unset. Litestream and the handover are SQLite machinery.
No zip restore on PostgreSQL

POST /api/backups/{key}/restore is refused and changes nothing. Plan your restore with pg_restore before you need it.

4. Run several replicas​

Several replicas are opt-in and work on PostgreSQL only. Without the flag, run one replica.

CLAVIGER_DB_URL='postgres://...'
CLAVIGER_MULTI_REPLICA=true

Claviger refuses to start with the flag on SQLite. Every replica must have:

  • The same version, and the same pb_hooks and pb_migrations.
  • The same encryption environment variable and the same key-encryption key (CLAVIGER_KEK*).
  • The same shared file storage. Local disk does not work. Configure S3.

Plan your database connections. Each replica uses its pool plus one dedicated LISTEN connection, and a cron job borrows one more. Size max_connections for replicas x (pool + 2).

If you use PgBouncer, run it in session mode for CLAVIGER_DB_URL. In transaction mode LISTEN does not work.

What Claviger shares across replicas:

  • Realtime events, through LISTEN/NOTIFY. Delivery is at most once.
  • Cron jobs. Each tick runs once across all replicas.
  • Webhook deliveries. Each due delivery is claimed by one replica.
  • Settings, collections, sessions and field-key caches.
  • Passkey ceremonies, so sticky sessions are not needed.
  • Rate limits that guard credentials. The generic API limit stays per replica, so the real ceiling is up to N times the setting.

What stays on each replica: request logs, the auxiliary.db clean-up jobs, realtime connections, and some in-memory cooldowns.

Sticky sessions for realtime only

The realtime stream and its subscription call must reach the same replica. Use your platform's sticky sessions for /api/realtime. Everything else works behind a plain round-robin load balancer.

  • Rolling updates overlap replicas. There is no handover.
  • Keep replica clocks within about a second.
  • /api/health answers from the replica you reach.
  • Anyone who can LISTEN on the database can read the notifications. Do not grant that to other applications.

5. Quick reference card​

Turn on​

  1. Empty PostgreSQL 16 or 17 database.
  2. CLAVIGER_DB_URL='postgres://...'.
  3. GET /api/health shows postgres.

Back up​

  1. pg_dump on your schedule.
  2. Keep the storage files of the same moment.
  3. Restore with pg_restore.

Several replicas​

  1. CLAVIGER_MULTI_REPLICA=true.
  2. S3 storage, shared keys, same version.
  3. Sticky sessions for /api/realtime.

SQLite stays the default. Nothing here applies to it.