This simple Django utility allows you to utilize the 12factor inspired DATABASE_URL environment variable to configure your Django application.
The dj_database_url.config method returns a Django database connection dictionary, populated with all the data specified in your URL. There is also a conn_max_age argument to easily enable Django’s connection pool.
If you’d rather not use an environment variable, you can pass a URL in directly instead to dj_database_url.parse.
Supported Databases
Support currently exists for PostgreSQL, PostGIS, MySQL, MySQL (GIS), Oracle, Oracle (GIS), Redshift, and SQLite.
Installation
Installation is simple:
$ pip install dj-database-url
Usage
Configure your database in settings.py from DATABASE_URL:
import dj_database_url DATABASES['default'] = dj_database_url.config(conn_max_age=600)
Provide a default:
DATABASES['default'] = dj_database_url.config(default='postgres://...')
Parse an arbitrary Database URL:
DATABASES['default'] = dj_database_url.parse('postgres://...', conn_max_age=600)
The conn_max_age attribute is the lifetime of a database connection in seconds and is available in Django 1.6+. If you do not set a value, it will default to 0 which is Django’s historical behavior of using a new database connection on each request. Use None for unlimited persistent connections.
URL schema
Engine |
Django Backend |
URL |
|---|---|---|
PostgreSQL |
django.db.backends.postgresql [1] |
postgres://USER:PASSWORD@HOST:PORT/NAME [2] |
PostGIS |
django.contrib.gis.db.backends.postgis |
postgis://USER:PASSWORD@HOST:PORT/NAME |
MSSQL |
sql_server.pyodbc |
mssql://USER:PASSWORD@HOST:PORT/NAME |
MySQL |
django.db.backends.mysql |
mysql://USER:PASSWORD@HOST:PORT/NAME |
MySQL (GIS) |
django.contrib.gis.db.backends.mysql |
mysqlgis://USER:PASSWORD@HOST:PORT/NAME |
SQLite |
django.db.backends.sqlite3 |
sqlite:///PATH [3] |
SpatiaLite |
django.contrib.gis.db.backends.spatialite |
spatialite:///PATH [3] |
Oracle |
django.db.backends.oracle |
oracle://USER:PASSWORD@HOST:PORT/NAME [4] |
Oracle (GIS) |
django.contrib.gis.db.backends.oracle |
oraclegis://USER:PASSWORD@HOST:PORT/NAME |
Redshift |
django_redshift_backend |
redshift://USER:PASSWORD@HOST:PORT/NAME |
Release files for django-database-url 1.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django-database-url-1.0.3.tar.gz | 5.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_database_url-1.0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.2 kB
Release files / django-database-url-1.0.3.tar.gz
| Download URL | django-database-url-1.0.3.tar.gz |
|---|---|
| Size | 5.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
141077780995d38c4a68a0d32d5affb016f288f54ff4f6296a9f1fee9e44aae3
|
|
BLAKE2b-256 checksum How to use checksums |
ca3aae60cee85877e634b2e48807942bdc0959fe12f26db6b3da365a3f1d2c01
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10
|
Release files / django_database_url-1.0.3-py3-none-any.whl
| Download URL | django_database_url-1.0.3-py3-none-any.whl |
|---|---|
| Size | 5.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d2e652fd9f4435b80725ad15691d7ea33ead56950614b9acc451c778f8bd2a1f
|
|
BLAKE2b-256 checksum How to use checksums |
37d52ae1392790cdecaa49f330acfc39983e4a4021f2da407e634930ded9ba2a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10
|