mirror of
https://github.com/jazzband/dj-database-url.git
synced 2026-03-16 22:20:24 +00:00
Use Database URLs in your Django Application.
|
|
||
|---|---|---|
| .gitignore | ||
| .travis.yml | ||
| dj_database_url.py | ||
| LICENSE | ||
| Makefile | ||
| README.rst | ||
| setup.cfg | ||
| setup.py | ||
| test_dj_database_url.py | ||
| tox.ini | ||
DJ-Database-URL
~~~~~~~~~~~~~~~
.. image:: https://secure.travis-ci.org/kennethreitz/dj-database-url.png?branch=master
:target: http://travis-ci.org/kennethreitz/dj-database-url
This simple Django utility allows you to utilize the
`12factor <http://www.12factor.net/backing-services>`_ inspired
``DATABASE_URL`` environment variable to configure your Django application.
Supported databases
-------------------
Support currently exists for PostgreSQL, PostGIS, MySQL, MySQL (GIS) and SQLite.
Installation
------------
Installation is simple::
$ pip install dj-database-url
Usage
-----
Configure your database in ``settings.py`` from ``DATABASE_URL``
(``default`` and ``conn_max_age`` are optional)::
import dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://...', conn_max_age=600 )}
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_psycopg2`` | ``postgres://USER:PASSWORD@HOST:PORT/NAME`` [1]_ |
+-------------+--------------------------------------------+--------------------------------------------------+
| PostGIS | ``django.contrib.gis.db.backends.postgis`` | ``postgis://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`` [2]_ |
+-------------+--------------------------------------------+--------------------------------------------------+
.. [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
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``.