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.3.0.tar.gz (5.8 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.3.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for dj_lite-1.3.0.tar.gz
Algorithm Hash digest
SHA256 188ec328bbf9c3c44f023083390afddefa49056d72df4a760ba020e56a43bac4
MD5 cc53d138f2e512deed194caa07c9265f
BLAKE2b-256 38e928ab1e18e7b66d4f9158db11f4c8ad7fe1654d726ffc6762d13efb435a1a

See more details on using hashes here.

Provenance

The following attestation bundles were made for dj_lite-1.3.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.3.0-py3-none-any.whl.

File metadata

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

File hashes

Hashes for dj_lite-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0e9500633fa262f817712f81825de305a033db08b262dfa87abdfdd7d82485ae
MD5 be61388038272b2f992774d65e2d5550
BLAKE2b-256 f8d56380d66beb1170011ea5b47ddbe1bbf9391d7e68465a07269fb55f710a8b

See more details on using hashes here.

Provenance

The following attestation bundles were made for dj_lite-1.3.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