# How to run SQLite in production

Store each SQLite database on a persistent local volume, write from one host per database, and test a restore on another host.

## Find the database files before deployment

### Rails

```bash
# List the SQLite database paths Rails uses in production.
RAILS_ENV=production bin/rails runner '
  ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, include_hidden: true).each do |config|
    puts File.expand_path(config.database, Rails.root) if config.adapter == "sqlite3"
  end
'
```

### Django

```bash
# List the SQLite database paths in the current Django settings.
python manage.py shell --command '
from pathlib import Path

from django.conf import settings

for alias, database in settings.DATABASES.items():
    if database["ENGINE"] == "django.db.backends.sqlite3":
        print(alias, Path(database["NAME"]).resolve())
'
```

## Keep the database across deploys

Mount a persistent local volume where the application can write. Put the database and its journal files in the same directory on that volume. Reuse the mount when replacing a container. If the platform gives each application instance a separate disk, designate one writer host for the database. Avoid opening the same SQLite file from multiple hosts over a network filesystem.

Run the path check from the application directory with the production environment loaded. Use it for file-backed databases, then check that every printed path is inside a persistent mount. Make sure the app user can write to the parent directory so SQLite can create its journal files.

### Rails 8 with SQLite

Rails 8 generates a production configuration for SQLite. The [Rails SQLite configuration template](https://github.com/rails/rails/blob/v8.0.4/railties/lib/rails/generators/rails/app/templates/config/databases/sqlite3.yml.tt) can include separate files for the primary database, Solid Queue, Solid Cache, and Solid Cable under `storage/`. Check `config/database.yml` for the files your app actually uses. Persist the directory, including the queue file if it holds jobs that must survive a deploy. The Rails command also lists databases that Rails skips during its usual database tasks.

Run `RAILS_ENV=production bin/rails db:prepare` on the writer host as part of a deploy. Stop or drain workers before a change that makes the old app incompatible with the new schema. If the app and queue need a coordinated restore, take the copies with both writers paused and record which backups belong together.

### Django with SQLite

In production settings, use an absolute file path under the mount, such as `/var/lib/app/main.sqlite`, for `DATABASES["default"]["NAME"]`. Check each alias if the app has more than one database. With the production settings selected, run `python manage.py migrate` once on the writer host before starting workers that require the new schema.

Keep write transactions short and watch for `database is locked` during real traffic. The [Django SQLite notes](https://docs.djangoproject.com/en/5.2/ref/databases/#sqlite-notes) explain timeout and transaction settings. A longer timeout gives a writer more time to wait. If concurrent requests still spend too long waiting for writes, compare [SQLite and Postgres](/sqlite-vs-postgres) before adding more workers.

## Test a deploy and a restore

1. Replace an app container while keeping the volume, then confirm that a known record still exists.
2. [Back up the SQLite databases](/sqlite-backup) off the writer host. Take a backup before a schema migration.
3. Restore the files on another host and run `PRAGMA integrity_check;` on each one. Start the matching app release there, then check a known record and any jobs you expect to find in the queue.
4. Before switching production traffic to a replacement host, stop the old writer. Use [readonly replicas](/sqlite-replication) for reads on additional machines or [SQLite failover](/failover) when a standby must take over writes.

Keep application secrets and uploaded files in their own backup plan. A database restore only brings back the SQLite file.

## Install

See how to back up and restore a database from [Django or Python](/django-sqlite-backup) or [better-sqlite3](/better-sqlite3-backup). To keep continuous backups from a production host, [install Litebackup](/install).
