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.
pip install dj-database-urlVerified import paths — ran on the pinned version, not inferred.
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.
Review the official documentation for the new registry pattern if you have custom database string implementations or are extending functionality. Ensure your Python and Django versions meet the new requirements (e.g., Python >=3.10, Django >=4.x).
Upgrade your Python environment to 3.9+ (preferably 3.10+) and your Django project to Django 4.0+.
Pass `conn_max_age=600` (or another appropriate value) and `conn_health_checks=True` to `dj_database_url.config()`.
Use the `sqlite:////` prefix for absolute file paths, or `sqlite:///./relative/path.sqlite3` for relative paths.
Format Oracle URLs with `user:pass@host:port/dbname` or use DSN/TNS in the `NAME` part as per Oracle's connection string specifications, ensuring proper colon separation for user/password.
Always provide a `default` argument to `dj_database_url.config()` (e.g., `default='sqlite:///db.sqlite3'`) or ensure the `DATABASE_URL` environment variable is set in your execution environment.
Run `pip install dj-database-url` to install the package.
Ensure the `DATABASE_URL` environment variable is set in your environment or provide a `default` argument to `dj_database_url.config()` in your `settings.py` (e.g., `dj_database_url.config(default='sqlite:///db.sqlite3')`).
Verify that the `DATABASE_URL` environment variable contains the correct host, port, username, and password. Ensure the database server is running and accessible from where your Django application is deployed, checking for any network or firewall issues.
Ensure the `DATABASE_URL` environment variable is properly set and accessible in the environment where the command is being run, or provide a valid `default` URL to `dj_database_url.config()` (e.g., `DATABASES = {'default': dj_database_url.config(default='sqlite:///:memory:')}`).