dj-database-url

3.1.2 · active · verified Thu Apr 09

dj-database-url is a simple Django utility that allows you to configure your Django application's database using a single 12-factor inspired `DATABASE_URL` environment variable. It parses the URL and returns a Django-compatible database connection dictionary, supporting various backend types like PostgreSQL, MySQL, SQLite, and Oracle. Currently at version 3.1.2, it is actively maintained by the Jazzband community with regular updates.

Warnings

Install

Imports

Quickstart

To quickly set up `dj-database-url` in your Django `settings.py`, use the `config()` function. It reads the `DATABASE_URL` environment variable, falling back to a SQLite default if not present. It's recommended to include `conn_max_age` and `conn_health_checks` for production reliability.

import os
import dj_database_url

# In your Django settings.py
DATABASES = {
    'default': dj_database_url.config(
        default=os.environ.get('DATABASE_URL', 'sqlite:///db.sqlite3'),
        conn_max_age=600,
        conn_health_checks=True,
    )
}

# Example of how DATABASE_URL might be set in your environment (e.g., .env file or deployment config)
# export DATABASE_URL="postgres://user:password@host:port/dbname"
# export DATABASE_URL="sqlite:///path/to/my/db.sqlite3"

view raw JSON →