About Turso as SQLite Replacement

Turso is an SQLite-compatible embedded database written in Rust, featuring an asynchronous interface.

The django-libsql library lets you use Turso in your Django project as a replacement for the standard SQLite database.

As investigated, you would run the local Turso database in a Docker container, which you can run from a Django project's directory:

$ curl -fsSL https://raw.githubusercontent.com/aaronkazah/django-libsql/main/scripts/docker.sh -o run_turso_docker.sh
$ chmod +x run_turso_docker.sh
$ ./run_turso_docker.sh

The script will create private and public keys for communication. And run the Docker image.

Then you would install a couple of Python dependencies:

(.venv)$ pip install cryptography==46.0.3 PyJWT==2.10.1

And connect to it with Django settings like:

import jwt
import time
from cryptography.hazmat.primitives import serialization

with open(BASE_DIR / "jwt_private_key.pem", "rb") as key_file:
    private_key = serialization.load_pem_private_key(
        key_file.read(),
        password=None
    )

payload = {
    "sub": "client_id",
    "exp": int(time.time()) + 3600  # Token expires in 1 hour
}

token = jwt.encode(payload, private_key, algorithm="EdDSA")

DATABASES = {
    "default": {
        "ENGINE": "libsql.db.backends.sqlite3",
        "NAME": f"ws://0.0.0.0:8080?authToken={token}",
        "TEST": {
            "ENGINE": "libsql.db.backends.sqlite3",
            "NAME": f"ws://0.0.0.0:8080?authToken={token}",
        }
    }
}

At the time of writing, django-libsql works only with Django 5.0.x, Turso Database is in Beta. And in the Docker volume, the database iku.db is not a single file like with the original SQLite, but a directory with a bunch of files. Use it at your own consideration.

Tips and Tricks Programming Databases Django 5.2 SQLite Turso