Registry / database / dj-database-url

dj-database-url

JSON →
library3.1.2pypypi✓ verified 35d ago

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.

databaseweb-framework
Install & Compatibility
Where this runs
tested against v3.1.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.925 runs
installs and imports cleanly · install 0.0s · import 0.037s · 66.3MB
glibc
py 3.103.925 runs
installs and imports cleanly · install 3.4s · import 0.036s · 67MB
65MB installed
● package 65MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Primary function for configuring from DATABASE_URL environment variable.

from dj_database_url import config

Used to parse a database URL string directly.

from dj_database_url import parse

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"
Debug
Known footguns
breakingVersion 3.0.0 introduced a new decorator registry pattern for database connection string checks and broke some API compatibility. It also updated supported Python and Django versions.
breakingVersion 2.3.0 removed support for Python 3.8 and Django 3.
gotchaFor performance and reliability in production, it's highly recommended to explicitly set `conn_max_age` (for persistent connections) and `conn_health_checks` (for checking connection validity) when using `dj_database_url.config()`.
gotchaWhen connecting to SQLite with an absolute file path, ensure you use four slashes (e.g., `sqlite:////full/path/to/your/database/file.sqlite`). The extra slashes are necessary because the 'file' portion is treated as the database's filename, not a hostname.
gotchaOracle database URLs expect the format `user:password` separated by a colon, not a forward slash as sometimes seen in other Oracle tools. You can also omit `HOST` and `PORT` and provide a full DSN string or TNS name in the `NAME` part of the URL.
gotcha`dj_database_url.config()` will raise a `UserWarning` if no `DATABASE_URL` environment variable is set and no `default` URL is provided in the function call.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
20 hits · last 30 days
gptbot
4
ahrefsbot
4
claudebot
4
bingbot
2
chatgpt-user
2
Resources