# SQLite vs Postgres

Keep local SQLite with Litebackup for backups, replicas, and failover when one host can handle the writes.

## Try a backup and restore

### Litebackup

```bash
# Start continuous SQLite backup and restore a separate copy with Litebackup.
# Runs on Linux and macOS.
# Install a Litebackup server at litebackup.com/install before joining.
curl --fail --silent --show-error --location https://litebackup.com/cli.sh | bash
export LITEBACKUP_API_KEY=JOIN_TOKEN@HOST
litebackup install service --api-key "${LITEBACKUP_API_KEY}"

# Back up the app's SQLite database and send the first version.
litebackup start /var/lib/app/main.sqlite app/
litebackup sync app/main.sqlite

# After the sync succeeds, restore a separate copy and check it.
RESTORE_DIR=$(mktemp -d)
litebackup restore app/main.sqlite "${RESTORE_DIR}/restored.sqlite"
sqlite3 -bail -readonly "${RESTORE_DIR}/restored.sqlite" 'PRAGMA integrity_check;'
```

### SQLite

```bash
# Make a consistent copy of a live SQLite database and check the copy.
# Runs on Linux and macOS.
RESTORE_DIR=$(mktemp -d)
sqlite3 -bail -readonly /var/lib/app/main.sqlite \
    ".backup '${RESTORE_DIR}/restored.sqlite'"
sqlite3 -bail -readonly "${RESTORE_DIR}/restored.sqlite" 'PRAGMA integrity_check;'
```

### Postgres

```bash
# Dump a Postgres database and restore it into a separate test database.
# Runs on Linux and macOS.
RESTORE_DIR=$(mktemp -d)
if [[ "${DATABASE_URL:?Set the source Postgres URL}" == \
    "${RESTORE_DATABASE_URL:?Set the test Postgres URL}" ]]; then
    printf '%s\n' 'Use a separate test database for the restore.' >&2
elif pg_dump --format=custom --file="${RESTORE_DIR:?}/app.dump" "${DATABASE_URL}"; then
    pg_restore \
        --exit-on-error \
        --no-owner \
        --no-privileges \
        --dbname="${RESTORE_DATABASE_URL}" \
        "${RESTORE_DIR}/app.dump"
fi
```

## Keep SQLite when one host writes each database

Many teams look at Postgres because they want backups they can restore, a way back after a bad deploy, reads on more than one machine, and a standby when the writer host fails. If one host can handle the writes for each database, Litebackup adds those capabilities to the SQLite files your app already uses.

Your app keeps its local SQLite file, its driver, and its SQL. Litebackup runs beside the app as the user that owns the database and sends committed changes to your Litebackup server, which encrypts the backups and archives them to your bucket. Restore a retained version by version number or time into a new file. Run readonly replicas on other machines, or let a standby host take over writes after the writer stops.

Moving to Postgres means migrating your schema and data, switching drivers, and connecting to a database server. Some queries may need changes. It is the right move when several hosts need to write the same tables at once, or when the app needs something only Postgres offers.

## What Litebackup adds to SQLite

| Need | Local SQLite with Litebackup | Postgres |
| --- | --- | --- |
| Backups | Continuous backups reach your server first and your bucket after the archive delay. [How backups work](/back-up-sqlite-while-your-app-writes) | A dump on a schedule, or a base backup with archived WAL. |
| Recover after a mistake | Restore a retained version into a new file and check it with SQLite. [Restore guide](/sqlite-point-in-time-restore) | Restore a dump, or recover a base backup with archived WAL to a chosen time. |
| Reads on other machines | Readonly replicas as ordinary SQLite files, usually a few seconds behind. [Replication guide](/sqlite-replication) | Hot standbys over the network, which can be behind the primary. |
| Writer failover | One origin writes, and a replica can take over after the origin's lease expires. [Failover guide](/failover) | Promote a standby, then route connections to it and keep the old primary from accepting writes. |
| Where data lives | Your app and server hold local files. Your server encrypts backups before they reach the bucket. | The Postgres server you run, or your provider's platform. |
| App changes | Backups keep the local file, driver, and SQL. Failover requires routing writes to the active host. | A Postgres driver, a database connection, and a migration of schema and data. |

## What it costs to run Litebackup

- **A Litebackup server** on a Linux VM with a public IP and port 443 open, installed with one command. It keeps recent versions on its disk and holds the bucket credentials and the encryption keys.
- **A bucket you own** on Amazon S3, Cloudflare R2, Google Cloud Storage, or another S3-compatible store. Retention takes space on the server's disk and in the bucket, and the console estimates both before you save.
- **The Litebackup client on each app machine**, joined with a token from the console and run as the user that owns the databases.
- **A recovery kit or passphrase** kept somewhere other than the server and its bucket, so a replacement server can read your backups.

The client sends changes asynchronously. If the writer host fails, recovery starts from the versions that reached your server or bucket. The bucket follows the server after the archive delay, which defaults to 15 minutes.

Failover keeps one writer per database. A replica can become the origin after the writer's lease expires and starts from the last change your server received. Your load balancer has to send writes to the current origin, and a replica can be a few seconds behind, so read from the origin when a page must show its own latest write. The [failover guide](/failover) covers the health route and what happens when an old writer comes back.

Details on the server, encryption, retention, and recovering a lost server are in [Server and buckets](/run-your-litebackup-server-and-bucket).

## When Postgres is the right choice

- **Several app hosts write the same tables.** SQLite runs one write transaction per database at a time, and all of them run on the writer host. When sending every write through one host is too slow for the app, Postgres lets many connections from many hosts write concurrently.
- **The app needs Postgres itself.** Choose Postgres if your app depends on extensions, range types, or database roles and grants.
- **The code runs without a disk.** Serverless platforms and containers without a persistent volume have nowhere to keep a database file.
- **Your team already runs Postgres well.** Existing backups, monitoring, and failover count for a lot. Do not move away from them without a reason.

## Concurrent writes

SQLite runs inside your app and stores a database in a file. In [WAL mode](https://sqlite.org/wal.html), other connections can read while a transaction writes, and each database still allows one write transaction at a time. A long transaction holds up every other writer even when the database is small, so keep write transactions short and measure lock waits and write latency during your busiest period.

Postgres runs as a server that app processes reach over the network. It runs concurrent transactions that change different rows, and updates to the same row still wait on a lock.

When independent tenants or jobs can commit on their own, a database per tenant gives each its own writer and keeps SQLite's one-writer limit per file. Read about [one SQLite database per tenant](/sqlite-database-for-every-tenant-or-agent).

## Three production examples

- **One app host with short writes.** An order management app keeps its database on the host and backs it up continuously to your bucket. Restore a version onto a fresh host and run the app against it before the first deploy.
- **Many tenants with independent records.** A database per tenant lets different writers work on different files, and each tenant restores on its own. Pick Postgres if transactions often change records for several tenants together.
- **Several API servers changing shared records.** Choose Postgres when any server needs to write the same tables and routing every write to one host is too slow for the app.

## Try a backup and restore

The tabs above make a backup and restore it into a separate copy, so you can check each database the way you will check it in production.

Install a Litebackup server first, then run the Litebackup tab on the app host with a join token from the console. Both SQLite tabs use the SQLite shell to check the new file. The check should print `ok`.

### Set up a Postgres restore target

The Postgres tab needs the Postgres client tools and two connection URLs. Set `DATABASE_URL` to the source database and `RESTORE_DATABASE_URL` to a separate, empty test database. The restore skips the source's ownership and grants.

The [official Postgres image](https://hub.docker.com/_/postgres) reads `POSTGRES_USER`, `POSTGRES_PASSWORD`, and `POSTGRES_DB` when it initializes a fresh container. This example starts a disposable database on localhost and builds `RESTORE_DATABASE_URL` for it. Use the source server's major version for the container and client tools. This example uses Postgres 17.

```bash
# Start an empty Postgres database for the restore test.
# Runs on Linux and macOS.
POSTGRES_USER=restore_user
POSTGRES_PASSWORD=local-restore-password
POSTGRES_DB=restore_test

# --rm = remove the container when it stops
docker run \
    --detach \
    --rm \
    --name litebackup-postgres-restore \
    --publish 127.0.0.1:55432:5432 \
    --env "POSTGRES_USER=${POSTGRES_USER}" \
    --env "POSTGRES_PASSWORD=${POSTGRES_PASSWORD}" \
    --env "POSTGRES_DB=${POSTGRES_DB}" \
    postgres:17

export RESTORE_DATABASE_URL="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@127.0.0.1:55432/${POSTGRES_DB}"
```

Wait for Postgres to start, set `DATABASE_URL` to your app's connection URL, then run the Postgres tab from the same shell. After the restore succeeds, list the restored tables.

```bash
# List the tables in the restored Postgres database.
psql --no-psqlrc --dbname="${RESTORE_DATABASE_URL:?}" --command='\dt'
```

Query a recent record you expect to find and run the app against the restored database. If you used the container, stop it when the test is done.

```bash
# Stop the disposable Postgres container and let Docker remove it.
docker stop litebackup-postgres-restore
```

## What to test before you decide

- **Write contention.** Run your busiest period against SQLite and watch for `database is locked` and for lock waits that make requests slow. If writes from several hosts still wait too long after you have shortened transactions, that is the case for Postgres.
- **A restore onto a fresh host.** For SQLite with Litebackup, restore a version from before a test change and check that the record you changed is back. For Postgres, restore the dump into the empty test database and list its tables.
- **Writer failover.** Stop the writer in a test deployment and check where writes go and that the new writer has the records you expect. Check that your routing cuts off writes to the old writer while it is unreachable.
- **Replica delay.** Read a replica after a write on the origin and confirm the delay is acceptable for the pages that use it.
- **A second copy.** For data you cannot lose, keep a second, independent backup, so no single system holds the only copy.

For hosted Postgres, see how [Neon](/vs-neon) and [Supabase](/vs-supabase) compare with local SQLite and Litebackup.

## Install

Start with [running SQLite in production](/sqlite-in-production) if the writer fits on one host. Then [install your Litebackup server](/install), join the app machine, and [back up the first database](/sqlite-backup). The [command line](/cli) and the [SDK](/sdk) list every command.
