Database
Overview¶
SpeedPy supports PostgreSQL (recommended), SQLite (development), and MySQL. The database is configured via the DATABASE_URL environment variable using django-environ.
Configuration¶
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 errorsCONN_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:
| Database | CI_COLLATION |
|---|---|
| PostgreSQL | und-x-icu |
| SQLite | NOCASE |
| MySQL | utf8mb4_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