Skip to main content

Configure SQLite for production use with Django.

Project description

dj-lite 💡

Use SQLite in production with Django

Overview

Simplify deploying and maintaining production Django websites by using SQLite in production. dj-lite helps enable the best performance for SQLite for small to medium-sized projects. It requires Django 5.1+.

I also wrote the definitive guide to using Django SQLite in production which has more details about the actual server setup and operations.

Also read through the official Django SQLite notes for more low-level information.

Installation

  1. Install dj-lite with pip, uv, poetry, etc.
pip install dj-lite

OR

uv add dj-lite
  1. In settings.py add the following.
# settings.py

from dj_lite import sqlite_config

DATABASES = {
  "default": sqlite_config(BASE_DIR),
}
  1. That's it! You're all set to go with the default configuration.

Default configuration

{
    "ENGINE": "django.db.backends.sqlite3",
    "NAME": Path("db.sqlite3"),
    "OPTIONS": {
        "transaction_mode": "IMMEDIATE",
        "timeout": 5,
        "init_command": """PRAGMA journal_mode=WAL;
PRAGMA synchronous=NORMAL;
PRAGMA temp_store=MEMORY;
PRAGMA mmap_size=134217728;
PRAGMA journal_size_limit=27103364;
PRAGMA cache_size=2000;""",
    },
}

Args and Kwargs

The sqlite_config method takes in many arguments to tweak the database settings.

base_dir: Path

The directory where the database file will be stored. Required.

file_name: str

Name of the SQLite database file. Defaults to 'db.sqlite3'.

engine: str

Django database backend to use. Defaults to 'django.db.backends.sqlite3'.

transaction_mode: TransactionMode

The transaction locking behavior. Defaults to 'IMMEDIATE'.

timeout: int

Time in seconds to wait for a database lock before raising an error. Defaults to 5.

init_command: str

Custom SQL command to execute when the database connection is created. If None, will be generated from other parameters.

journal_mode: JournalMode

The journal mode for the database. Defaults to 'WAL'.

synchronous: Synchronous

How aggressively SQLite syncs data to disk. Defaults to 'NORMAL'.

temp_store: TempStore

How to store temporary objects. Defaults to 'MEMORY'.

mmap_size: int

Maximum number of bytes to use for memory-mapped I/O. Defaults to 134217728.

journal_size_limit: int

Maximum size of the journal in bytes. Defaults to 27103364.

cache_size: int

Maximum number of database disk pages to hold in memory. Defaults to 2000.

pragmas: dict

Additional PRAGMA statements to include in the init command. These will override any conflicting settings from other parameters.

What is even happening here?

The Django defaults for SQLite are fine for local dev or running tests, but they are not great for production use -- specifically when there are concurrent reads/writes to the database. dj-lite tunes SQLite so it can be safely used in production.

Pragmas

When SQLite opens a database connection, settings (called pragmas) can be passed in to tune the performance. dj-lite comes with highly tuned defaults for these pragmas.

PRAGMA journal_mode=WAL;
PRAGMA synchronous=NORMAL;
PRAGMA temp_store=MEMORY;
PRAGMA mmap_size=134217728;
PRAGMA journal_size_limit=27103364;
PRAGMA cache_size=2000;

What about the foreign_keys pragma?

You might notice that the SQLite foreign_keys pragma is not included above. That is because it is the one pragma that is always passed in when Django creates a connection to SQLite.

Transaction Mode

According to the Django documentation, SQLite supports three transaction modes: DEFERRED, IMMEDIATE, and EXCLUSIVE -- the default is DEFERRED. However, "[to] make sure your transactions wait until timeout before raising “Database is Locked”, change the transaction mode to IMMEDIATE."

In my experience, using IMMEDIATE has been ok as long as database queries are short.

Inspiration

Developing

Run the tests

  1. uv pip install -e .
  2. uv run pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dj_lite-1.1.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dj_lite-1.1.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file dj_lite-1.1.0.tar.gz.

File metadata

  • Download URL: dj_lite-1.1.0.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for dj_lite-1.1.0.tar.gz
Algorithm Hash digest
SHA256 27674d97379fe62c1b729dfc522c3ccc74f29363636f4c9fb34641213a360688
MD5 038635d5dbaf35fdce4d0ed966a24308
BLAKE2b-256 402e174a38ca2fb9ab9b9431cba2c0b7bf753c32b05fcbf6f1a8041480dacd6d

See more details on using hashes here.

Provenance

The following attestation bundles were made for dj_lite-1.1.0.tar.gz:

Publisher: publish.yml on adamghill/dj-lite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dj_lite-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: dj_lite-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for dj_lite-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6e7937a39eccebfa36fb0eb3a4bed7d56066439ef4cf4b3ce44f66bcd9191471
MD5 50a9fb4c703d758dc94978cf136b4256
BLAKE2b-256 120b17a3b1d07fe6e7d45d44d8695253d8f26150ecafaef771689ad7df350a02

See more details on using hashes here.

Provenance

The following attestation bundles were made for dj_lite-1.1.0-py3-none-any.whl:

Publisher: publish.yml on adamghill/dj-lite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page