Skip to main content

Database

Overview

SpeedPy supports PostgreSQL (recommended), SQLite (development), and MySQL. The database is configured via the DATABASE_URL environment variable using django-environ.

Configuration

DATABASES = {
"default": env.db(default="sqlite:///db.sqlite3"),
}

Connection String Examples

# PostgreSQL (recommended, used in Docker)
DATABASE_URL=postgres://speedpycom:speedpycom@db:5432/speedpycom

# SQLite (simple local development)
DATABASE_URL=sqlite:///db.sqlite3

# MySQL
DATABASE_URL=mysql://user:password@localhost:3306/dbname

PostgreSQL Settings

When PostgreSQL is detected, SpeedPy automatically enables:

if DATABASES["default"]["ENGINE"] == "django.db.backends.postgresql":
DATABASES["default"]["ATOMIC_REQUESTS"] = True
DATABASES["default"]["CONN_MAX_AGE"] = env.int("CONN_MAX_AGE", default=60)
  • ATOMIC_REQUESTS — wraps each request in a database transaction, automatically rolling back on errors
  • CONN_MAX_AGE — keeps database connections alive for 60 seconds (configurable), reducing connection overhead

Case-Insensitive Collation

SpeedPy configures a case-insensitive collation per database engine for use with the email field and other fields that need case-insensitive lookups:

DatabaseCI_COLLATION
PostgreSQLund-x-icu
SQLiteNOCASE
MySQLutf8mb4_unicode_ci

Use it in your own models:

from django.conf import settings

name = models.CharField(
max_length=255,
db_collation=settings.CI_COLLATION,
)

Docker Setup

The Docker Compose file includes a PostgreSQL service:

db:
image: postgres
environment:
- POSTGRES_USER=speedpycom
- POSTGRES_PASSWORD=speedpycom
- POSTGRES_DB=speedpycom

Running Migrations

# Make migrations
docker compose run --rm web python manage.py makemigrations
# or: make mm

# Apply migrations
docker compose run --rm web python manage.py migrate
# or: make m