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
- Install
dj-litewithpip,uv,poetry, etc.
pip install dj-lite
OR
uv add dj-lite
- In
settings.pyadd the following.
# settings.py
import dj_lite import sqlite_config
DATABASES = {
"default": sqlite_config(BASE_DIR),
}
- 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
- Litestack
- Django, SQLite, and the Database is Locked Error
- Gotchas with SQLite in Production
- Django SQLite Production Config
- DjangoCon Europe 2024 | Django, SQLite, and Production
- DjangoCon Europe 2023 | Use SQLite in production
Developing
Run the tests
uv pip install -e .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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dj_lite-1.2.0.tar.gz.
File metadata
- Download URL: dj_lite-1.2.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
31f160102bfb970148275ff589be80cb48b2fe72d920b673daf5ac50c94ab08e
|
|
| MD5 |
39099b212406d53f36c007e3d798845e
|
|
| BLAKE2b-256 |
f44b53bb37649865c972e3a2e4cfb1bc387cd3ad6b52220f9f18920c1e9a25af
|
Provenance
The following attestation bundles were made for dj_lite-1.2.0.tar.gz:
Publisher:
publish.yml on adamghill/dj-lite
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dj_lite-1.2.0.tar.gz -
Subject digest:
31f160102bfb970148275ff589be80cb48b2fe72d920b673daf5ac50c94ab08e - Sigstore transparency entry: 454615008
- Sigstore integration time:
-
Permalink:
adamghill/dj-lite@06b97550e4b0f75e1a958af4548c13cfc09ff960 -
Branch / Tag:
refs/tags/1.2.0 - Owner: https://github.com/adamghill
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@06b97550e4b0f75e1a958af4548c13cfc09ff960 -
Trigger Event:
release
-
Statement type:
File details
Details for the file dj_lite-1.2.0-py3-none-any.whl.
File metadata
- Download URL: dj_lite-1.2.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f82349fdce0dad3d90e57edad442e2719eddc2b6ae94de5b2e7146f6df45de00
|
|
| MD5 |
82eceb21451cf89670054cee2b2943f3
|
|
| BLAKE2b-256 |
8ee071512d75a91b9ec77a60799e1c296841d314364e19c3bcb16009e19f557d
|
Provenance
The following attestation bundles were made for dj_lite-1.2.0-py3-none-any.whl:
Publisher:
publish.yml on adamghill/dj-lite
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dj_lite-1.2.0-py3-none-any.whl -
Subject digest:
f82349fdce0dad3d90e57edad442e2719eddc2b6ae94de5b2e7146f6df45de00 - Sigstore transparency entry: 454615030
- Sigstore integration time:
-
Permalink:
adamghill/dj-lite@06b97550e4b0f75e1a958af4548c13cfc09ff960 -
Branch / Tag:
refs/tags/1.2.0 - Owner: https://github.com/adamghill
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@06b97550e4b0f75e1a958af4548c13cfc09ff960 -
Trigger Event:
release
-
Statement type: