Skip to main content

Move an install from SQLite to PostgreSQL

DEVELOPER GUIDE

Copy an existing SQLite install into an empty PostgreSQL database with claviger migrate-db, check the copy, and switch over.

Who this guide is for

Developers and self-hosters who run Claviger on SQLite and can stop the server, reach PostgreSQL 16 or 17, and set environment variables.

Why it matters

The copy is a snapshot of the moment the command starts. Writes after that are lost, and the key-encryption key does not travel with the data. Rehearse first, and keep the SQLite files until you are sure.

What you will do, in one sentence

Stop the server, run claviger migrate-db --dry-run, run it for real, then start Claviger with CLAVIGER_DB_URL set.

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

1. Before you copy​

claviger migrate-db copies the data.db of a data directory into an empty PostgreSQL database. It is a one-way, offline copy. It opens the source read only and never changes it, so the SQLite install stays a valid fallback.

Check these first:

  • The target database is empty. The command refuses a database that has any table, and it never merges.
  • The source is not older than the binary. If it is, start the current version on it once, stop it, then migrate.
  • You stop the server. A running server owns the source directory, and the command refuses unless you pass --offline-ok.
  • Your view collections are written for PostgreSQL, or you plan to use --skip-views.
Stop the server first

The copy is a snapshot. Anything written to SQLite after the command starts is not copied. --offline-ok is your statement that no server is running.

2. Rehearse, then copy​

claviger migrate-db --dir=/pb_data \
--to='postgres://claviger:[email protected]:5432/claviger?sslmode=require' --dry-run

Fix what the rehearsal reports. Then run the copy:

claviger migrate-db --dir=/pb_data \
--to='postgres://claviger:[email protected]:5432/claviger?sslmode=require' --verbose

The command does this, in order:

  1. Refuses a target that has tables, and refuses a source that a server owns.
  2. Builds the schema on the target and imports the collections.
  3. Copies the rows of every table in insertion order, --batch rows for each transaction. The default is 1000.
  4. Verifies. It compares the row count of every table, a checksum over the ids, and up to 20 sample rows for each table.

Any mismatch fails the command and names the table and the row. Encrypted values and the wrapped data keys are copied byte for byte. Ids, timestamps, password hashes, tokens and signing keys stay the same, so sessions, API keys and signed tokens keep working.

If the command stops half way, run it again with --resume. It continues after the last committed batch. It refuses when the source changed since the run started. Raise --batch for a remote database, because round trips bind the speed.

3. Switch over​

  1. Set CLAVIGER_DB_URL where Claviger starts, such as the container or the unit file.
  2. If you use field encryption, run claviger keys check. It opens every data key, read only, and fails with keys check: FAILED when it cannot.
  3. Start the server:
export CLAVIGER_DB_URL='postgres://claviger:[email protected]:5432/claviger?sslmode=require'
claviger serve --dir=/pb_data

Keep the pb_data directory. The storage files and auxiliary.db stay there. The old request logs are not copied.

Leave these unchanged:

  • The key-encryption key: CLAVIGER_KEK, CLAVIGER_KEK_FILE, or CLAVIGER_KEK_AZURE_KEY_ID. The wrapped data keys travel with the data, but the key does not.
  • PB_ENCRYPTION_KEY. The stored settings are copied as they are.

4. After the switch​

  • Backups. Zip backups no longer hold the database. Take a pg_dump before you delete anything, and keep the dump and the storage files of the same moment together.
  • View collections. Their SQL runs as written, so a view written for SQLite often fails on Postgres. With --skip-views, the copy leaves them out and lists them. Recreate them in PostgreSQL SQL.
  • Tables you made by hand with raw SQL are not copied. The report lists them as not copied. The same goes for columns that the Postgres table does not have. A warning names them.
  • Roll back by unsetting CLAVIGER_DB_URL. The SQLite files were never touched. Anything written on Postgres since the switch stays there.
Rolling back loses new writes

After you unset CLAVIGER_DB_URL, the SQLite files are as they were at the copy. Records created on PostgreSQL since then are not in them.

5. Quick reference card​

Copy​

  1. Stop the server.
  2. claviger migrate-db --dir=... --to=... --dry-run
  3. Run it again with --verbose. Use --resume after an interruption.

Switch​

  1. Set CLAVIGER_DB_URL.
  2. claviger keys check if you use field encryption.
  3. claviger serve --dir=/pb_data

Afterward​

  1. pg_dump on a schedule.
  2. Keep the SQLite files until you are sure.

The command never changes the SQLite source.