2012-04-30 17:25:03 +00:00
DJ-Database-URL
2012-09-24 15:45:34 +00:00
~~~~~~~~~~~~~~~
2012-04-30 17:25:03 +00:00
2016-12-13 21:55:24 +00:00
.. image :: https://secure.travis-ci.org/kennethreitz/dj-database-url.svg?branch=master
2012-09-24 15:44:31 +00:00
:target: http://travis-ci.org/kennethreitz/dj-database-url
2012-06-19 15:33:59 +00:00
2012-04-30 17:25:03 +00:00
This simple Django utility allows you to utilize the
`12factor <http://www.12factor.net/backing-services> `_ inspired
2012-04-30 17:37:58 +00:00
`` DATABASE_URL `` environment variable to configure your Django application.
2016-02-03 00:20:44 +00:00
The `` dj_database_url.config `` method returns a Django database connection
2016-02-03 00:19:28 +00:00
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
2016-02-03 00:20:44 +00:00
instead to `` dj_database_url.parse `` .
2016-02-03 00:19:28 +00:00
2018-09-17 12:14:27 +00:00
-----------
If you're interested in financially supporting Kenneth Reitz open source, consider `visiting this link <https://cash.me/$KennethReitz> `_ . Your support helps tremendously with sustainability of motivation, as Open Source is no longer part of my day job.
2016-02-03 00:19:28 +00:00
Supported Databases
2014-04-18 06:35:07 +00:00
-------------------
2016-02-03 00:19:28 +00:00
Support currently exists for PostgreSQL, PostGIS, MySQL, MySQL (GIS),
2016-10-25 08:25:01 +00:00
Oracle, Oracle (GIS), Redshift, and SQLite.
2014-04-18 06:35:07 +00:00
Installation
------------
Installation is simple::
$ pip install dj-database-url
2012-04-30 17:25:03 +00:00
Usage
-----
2016-02-03 00:19:28 +00:00
Configure your database in `` settings.py `` from `` DATABASE_URL `` ::
2012-04-30 17:25:03 +00:00
2014-12-12 09:58:37 +00:00
import dj_database_url
2016-02-03 00:19:28 +00:00
DATABASES['default'] = dj_database_url.config(conn_max_age=600)
Provide a default::
2016-02-10 12:57:35 +00:00
DATABASES['default'] = dj_database_url.config(default='postgres://...')
2012-04-30 17:25:03 +00:00
2012-04-30 19:08:20 +00:00
Parse an arbitrary Database URL::
2016-02-03 00:19:28 +00:00
DATABASES['default'] = dj_database_url.parse('postgres://...', conn_max_age=600)
2015-05-11 05:35:47 +00:00
2016-02-03 00:19:28 +00:00
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
2015-05-11 05:35:47 +00:00
request. Use `` None `` for unlimited persistent connections.
2012-04-30 17:25:03 +00:00
2014-04-18 06:35:07 +00:00
URL schema
----------
2016-06-05 16:49:35 +00:00
+-------------+-----------------------------------------------+--------------------------------------------------+
| Engine | Django Backend | URL |
+=============+===============================================+==================================================+
2019-07-12 19:29:15 +00:00
| PostgreSQL | `` django.db.backends.postgresql `` | `` postgres://USER:PASSWORD@HOST:PORT/NAME `` [1]_ |
2016-06-05 16:49:35 +00:00
+-------------+-----------------------------------------------+--------------------------------------------------+
| PostGIS | `` django.contrib.gis.db.backends.postgis `` | `` postgis://USER:PASSWORD@HOST:PORT/NAME `` |
+-------------+-----------------------------------------------+--------------------------------------------------+
2017-08-21 15:32:22 +00:00
| MSSQL | `` sql_server.pyodbc `` | `` mssql://USER:PASSWORD@HOST:PORT/NAME `` |
+-------------+-----------------------------------------------+--------------------------------------------------+
2016-06-05 16:49:35 +00:00
| 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 `` [2]_ |
+-------------+-----------------------------------------------+--------------------------------------------------+
| SpatiaLite | `` django.contrib.gis.db.backends.spatialite `` | `` spatialite:///PATH `` [2]_ |
+-------------+-----------------------------------------------+--------------------------------------------------+
| Oracle | `` django.db.backends.oracle `` | `` oracle://USER:PASSWORD@HOST:PORT/NAME `` [3]_ |
+-------------+-----------------------------------------------+--------------------------------------------------+
| Oracle (GIS)| `` django.contrib.gis.db.backends.oracle `` | `` oraclegis://USER:PASSWORD@HOST:PORT/NAME `` |
+-------------+-----------------------------------------------+--------------------------------------------------+
2017-08-03 21:06:20 +00:00
| Redshift | `` django_redshift_backend `` | `` redshift://USER:PASSWORD@HOST:PORT/NAME `` |
2016-10-25 08:25:01 +00:00
+-------------+-----------------------------------------------+--------------------------------------------------+
2014-02-04 15:18:18 +00:00
.. [1] With PostgreSQL, you can also use unix domain socket paths with
`percent encoding <http://www.postgresql.org/docs/9.2/interactive/libpq-connect.html#AEN38162> `_ :
`` postgres://%2Fvar%2Flib%2Fpostgresql/dbname `` .
.. [2] SQLite connects to file based databases. The same URL format is used, omitting
2014-04-18 06:35:07 +00:00
the hostname, and using the "file" portion as the filename of the database.
This has the effect of four slashes being present for an absolute file path:
`` sqlite:////full/path/to/your/database/file.sqlite `` .
2014-10-24 18:48:14 +00:00
.. [3] Note that when connecting to Oracle the URL isn't in the form you may know
from using other Oracle tools (like SQLPlus) i.e. user and password are separated
by `` : `` not by `` / `` . Also you can omit `` HOST `` and `` PORT ``
and provide a full DSN string or TNS name in `` NAME `` part.