| 1 |
DJ-Database-URL |
| 2 |
~~~~~~~~~~~~~~~ |
| 3 |
|
| 4 |
.. image:: https://jazzband.co/static/img/badge.png |
| 5 |
:target: https://jazzband.co/ |
| 6 |
:alt: Jazzband |
| 7 |
|
| 8 |
.. image:: https://github.com/jazzband/dj-database-url/actions/workflows/test.yml/badge.svg |
| 9 |
:target: https://github.com/jazzband/dj-database-url/actions/workflows/test.yml |
| 10 |
|
| 11 |
.. image:: https://codecov.io/gh/jazzband/dj-database-url/branch/master/graph/badge.svg?token=7srBUpszOa |
| 12 |
:target: https://codecov.io/gh/jazzband/dj-database-url |
| 13 |
|
| 14 |
This simple Django utility allows you to utilize the |
| 15 |
`12factor <http://www.12factor.net/backing-services>`_ inspired |
| 16 |
``DATABASE_URL`` environment variable to configure your Django application. |
| 17 |
|
| 18 |
The ``dj_database_url.config`` method returns a Django database connection |
| 19 |
dictionary, populated with all the data specified in your URL. There is |
| 20 |
also a `conn_max_age` argument to easily enable Django's connection pool. |
| 21 |
|
| 22 |
If you'd rather not use an environment variable, you can pass a URL in directly |
| 23 |
instead to ``dj_database_url.parse``. |
| 24 |
|
| 25 |
Installation |
| 26 |
------------ |
| 27 |
|
| 28 |
Installation is simple: |
| 29 |
|
| 30 |
.. code-block:: console |
| 31 |
|
| 32 |
$ pip install dj-database-url |
| 33 |
|
| 34 |
Usage |
| 35 |
----- |
| 36 |
|
| 37 |
1. If ``DATABASES`` is already defined: |
| 38 |
|
| 39 |
- Configure your database in ``settings.py`` from ``DATABASE_URL``: |
| 40 |
|
| 41 |
.. code-block:: python |
| 42 |
|
| 43 |
import dj_database_url |
| 44 |
|
| 45 |
DATABASES['default'] = dj_database_url.config( |
| 46 |
conn_max_age=600, |
| 47 |
conn_health_checks=True, |
| 48 |
) |
| 49 |
|
| 50 |
- Provide a default: |
| 51 |
|
| 52 |
.. code-block:: python |
| 53 |
|
| 54 |
DATABASES['default'] = dj_database_url.config( |
| 55 |
default='postgres://...', |
| 56 |
conn_max_age=600, |
| 57 |
conn_health_checks=True, |
| 58 |
) |
| 59 |
|
| 60 |
- Parse an arbitrary Database URL: |
| 61 |
|
| 62 |
.. code-block:: python |
| 63 |
|
| 64 |
DATABASES['default'] = dj_database_url.parse( |
| 65 |
'postgres://...', |
| 66 |
conn_max_age=600, |
| 67 |
conn_health_checks=True, |
| 68 |
) |
| 69 |
|
| 70 |
2. If ``DATABASES`` is not defined: |
| 71 |
|
| 72 |
- Configure your database in ``settings.py`` from ``DATABASE_URL``: |
| 73 |
|
| 74 |
.. code-block:: python |
| 75 |
|
| 76 |
import dj_database_url |
| 77 |
|
| 78 |
DATABASES = { |
| 79 |
'default': dj_database_url.config( |
| 80 |
conn_max_age=600, |
| 81 |
conn_health_checks=True, |
| 82 |
), |
| 83 |
} |
| 84 |
|
| 85 |
- You can provide a default, used if the ``DATABASE_URL`` setting is not defined: |
| 86 |
|
| 87 |
.. code-block:: python |
| 88 |
|
| 89 |
DATABASES = { |
| 90 |
'default': dj_database_url.config( |
| 91 |
default='postgres://...', |
| 92 |
conn_max_age=600, |
| 93 |
conn_health_checks=True, |
| 94 |
) |
| 95 |
} |
| 96 |
|
| 97 |
- Parse an arbitrary Database URL: |
| 98 |
|
| 99 |
.. code-block:: python |
| 100 |
|
| 101 |
DATABASES = { |
| 102 |
'default': dj_database_url.parse( |
| 103 |
'postgres://...', |
| 104 |
conn_max_age=600, |
| 105 |
conn_health_checks=True, |
| 106 |
) |
| 107 |
} |
| 108 |
|
| 109 |
``conn_max_age`` sets the |CONN_MAX_AGE setting|__, which tells Django to |
| 110 |
persist database connections between requests, up to the given lifetime in |
| 111 |
seconds. If you do not provide a value, it will follow Django’s default of |
| 112 |
``0``. Setting it is recommended for performance. |
| 113 |
|
| 114 |
.. |CONN_MAX_AGE setting| replace:: ``CONN_MAX_AGE`` setting |
| 115 |
__ https://docs.djangoproject.com/en/stable/ref/settings/#conn-max-age |
| 116 |
|
| 117 |
``conn_health_checks`` sets the |CONN_HEALTH_CHECKS setting|__ (new in Django |
| 118 |
4.1), which tells Django to check a persisted connection still works at the |
| 119 |
start of each request. If you do not provide a value, it will follow Django’s |
| 120 |
default of ``False``. Enabling it is recommended if you set a non-zero |
| 121 |
``conn_max_age``. |
| 122 |
|
| 123 |
.. |CONN_HEALTH_CHECKS setting| replace:: ``CONN_HEALTH_CHECKS`` setting |
| 124 |
__ https://docs.djangoproject.com/en/stable/ref/settings/#conn-health-checks |
| 125 |
|
| 126 |
Strings passed to `dj_database_url` must be valid URLs; in |
| 127 |
particular, special characters must be url-encoded. The following url will raise |
| 128 |
a `ValueError`: |
| 129 |
|
| 130 |
.. code-block:: plaintext |
| 131 |
|
| 132 |
postgres://user:p#ssword!@localhost/foobar |
| 133 |
|
| 134 |
and should instead be passed as: |
| 135 |
|
| 136 |
.. code-block:: plaintext |
| 137 |
|
| 138 |
postgres://user:p%23ssword!@localhost/foobar |
| 139 |
|
| 140 |
`TEST <https://docs.djangoproject.com/en/stable/ref/settings/#test>`_ settings can be configured using the ``test_options`` attribute:: |
| 141 |
|
| 142 |
DATABASES['default'] = dj_database_url.config(default='postgres://...', test_options={'NAME': 'mytestdatabase'}) |
| 143 |
|
| 144 |
|
| 145 |
Supported Databases |
| 146 |
------------------- |
| 147 |
|
| 148 |
Support currently exists for PostgreSQL, PostGIS, MySQL, MySQL (GIS), |
| 149 |
Oracle, Oracle (GIS), Redshift, CockroachDB, Timescale, Timescale (GIS) and SQLite. |
| 150 |
|
| 151 |
If you want to use |
| 152 |
some non-default backends, you need to register them first: |
| 153 |
|
| 154 |
.. code-block:: python |
| 155 |
|
| 156 |
import dj_database_url |
| 157 |
|
| 158 |
# registration should be performed only once |
| 159 |
dj_database_url.register("mysql-connector", "mysql.connector.django") |
| 160 |
|
| 161 |
assert dj_database_url.parse("mysql-connector://user:password@host:port/db-name") == { |
| 162 |
"ENGINE": "mysql.connector.django", |
| 163 |
# ...other connection params |
| 164 |
} |
| 165 |
|
| 166 |
Some backends need further config adjustments (e.g. oracle and mssql |
| 167 |
expect ``PORT`` to be a string). For such cases you can provide a |
| 168 |
post-processing function to ``register()`` (note that ``register()`` is |
| 169 |
used as a **decorator(!)** in this case): |
| 170 |
|
| 171 |
.. code-block:: python |
| 172 |
|
| 173 |
import dj_database_url |
| 174 |
|
| 175 |
@dj_database_url.register("mssql", "sql_server.pyodbc") |
| 176 |
def stringify_port(config): |
| 177 |
config["PORT"] = str(config["PORT"]) |
| 178 |
|
| 179 |
@dj_database_url.register("redshift", "django_redshift_backend") |
| 180 |
def apply_current_schema(config): |
| 181 |
options = config["OPTIONS"] |
| 182 |
schema = options.pop("currentSchema", None) |
| 183 |
if schema: |
| 184 |
options["options"] = f"-c search_path={schema}" |
| 185 |
|
| 186 |
@dj_database_url.register("snowflake", "django_snowflake") |
| 187 |
def adjust_snowflake_config(config): |
| 188 |
config.pop("PORT", None) |
| 189 |
config["ACCOUNT"] = config.pop("HOST") |
| 190 |
name, _, schema = config["NAME"].partition("/") |
| 191 |
if schema: |
| 192 |
config["SCHEMA"] = schema |
| 193 |
config["NAME"] = name |
| 194 |
options = config.get("OPTIONS", {}) |
| 195 |
warehouse = options.pop("warehouse", None) |
| 196 |
if warehouse: |
| 197 |
config["WAREHOUSE"] = warehouse |
| 198 |
role = options.pop("role", None) |
| 199 |
if role: |
| 200 |
config["ROLE"] = role |
| 201 |
|
| 202 |
URL schema |
| 203 |
---------- |
| 204 |
|
| 205 |
+----------------------+-----------------------------------------------+--------------------------------------------------+ |
| 206 |
| Engine | Django Backend | URL | |
| 207 |
+======================+===============================================+==================================================+ |
| 208 |
| PostgreSQL | ``django.db.backends.postgresql`` [1]_ | ``postgres://USER:PASSWORD@HOST:PORT/NAME`` [2]_ | |
| 209 |
| | | ``postgresql://USER:PASSWORD@HOST:PORT/NAME`` | |
| 210 |
+----------------------+-----------------------------------------------+--------------------------------------------------+ |
| 211 |
| PostGIS | ``django.contrib.gis.db.backends.postgis`` | ``postgis://USER:PASSWORD@HOST:PORT/NAME`` | |
| 212 |
+----------------------+-----------------------------------------------+--------------------------------------------------+ |
| 213 |
| MSSQL | ``sql_server.pyodbc`` | ``mssql://USER:PASSWORD@HOST:PORT/NAME`` | |
| 214 |
+----------------------+-----------------------------------------------+--------------------------------------------------+ |
| 215 |
| MSSQL [5]_ | ``mssql`` | ``mssqlms://USER:PASSWORD@HOST:PORT/NAME`` | |
| 216 |
+----------------------+-----------------------------------------------+--------------------------------------------------+ |
| 217 |
| MySQL | ``django.db.backends.mysql`` | ``mysql://USER:PASSWORD@HOST:PORT/NAME`` [2]_ | |
| 218 |
+----------------------+-----------------------------------------------+--------------------------------------------------+ |
| 219 |
| MySQL (GIS) | ``django.contrib.gis.db.backends.mysql`` | ``mysqlgis://USER:PASSWORD@HOST:PORT/NAME`` | |
| 220 |
+----------------------+-----------------------------------------------+--------------------------------------------------+ |
| 221 |
| SQLite | ``django.db.backends.sqlite3`` | ``sqlite:///PATH`` [3]_ | |
| 222 |
+----------------------+-----------------------------------------------+--------------------------------------------------+ |
| 223 |
| SpatiaLite | ``django.contrib.gis.db.backends.spatialite`` | ``spatialite:///PATH`` [3]_ | |
| 224 |
+----------------------+-----------------------------------------------+--------------------------------------------------+ |
| 225 |
| Oracle | ``django.db.backends.oracle`` | ``oracle://USER:PASSWORD@HOST:PORT/NAME`` [4]_ | |
| 226 |
+----------------------+-----------------------------------------------+--------------------------------------------------+ |
| 227 |
| Oracle (GIS) | ``django.contrib.gis.db.backends.oracle`` | ``oraclegis://USER:PASSWORD@HOST:PORT/NAME`` | |
| 228 |
+----------------------+-----------------------------------------------+--------------------------------------------------+ |
| 229 |
| Redshift | ``django_redshift_backend`` | ``redshift://USER:PASSWORD@HOST:PORT/NAME`` | |
| 230 |
+----------------------+-----------------------------------------------+--------------------------------------------------+ |
| 231 |
| CockroachDB | ``django_cockroachdb`` | ``cockroach://USER:PASSWORD@HOST:PORT/NAME`` | |
| 232 |
+----------------------+-----------------------------------------------+--------------------------------------------------+ |
| 233 |
| Timescale [6]_ | ``timescale.db.backends.postgresql`` | ``timescale://USER:PASSWORD@HOST:PORT/NAME`` | |
| 234 |
+----------------------+-----------------------------------------------+--------------------------------------------------+ |
| 235 |
| Timescale (GIS) [6]_ | ``timescale.db.backend.postgis`` | ``timescalegis://USER:PASSWORD@HOST:PORT/NAME`` | |
| 236 |
+----------------------+-----------------------------------------------+--------------------------------------------------+ |
| 237 |
|
| 238 |
.. [1] The django.db.backends.postgresql backend is named django.db.backends.postgresql_psycopg2 in older releases. For |
| 239 |
backwards compatibility, the old name still works in newer versions. (The new name does not work in older versions). |
| 240 |
.. [2] With PostgreSQL or CloudSQL, you can also use unix domain socket paths with |
| 241 |
`percent encoding <http://www.postgresql.org/docs/9.2/interactive/libpq-connect.html#AEN38162>`_: |
| 242 |
``postgres://%2Fvar%2Flib%2Fpostgresql/dbname`` |
| 243 |
``mysql://uf07k1i6d8ia0v@%2fcloudsql%2fproject%3alocation%3ainstance/dbname`` |
| 244 |
.. [3] SQLite connects to file based databases. The same URL format is used, omitting |
| 245 |
the hostname, and using the "file" portion as the filename of the database. |
| 246 |
This has the effect of four slashes being present for an absolute file path: |
| 247 |
``sqlite:////full/path/to/your/database/file.sqlite``. |
| 248 |
.. [4] Note that when connecting to Oracle the URL isn't in the form you may know |
| 249 |
from using other Oracle tools (like SQLPlus) i.e. user and password are separated |
| 250 |
by ``:`` not by ``/``. Also you can omit ``HOST`` and ``PORT`` |
| 251 |
and provide a full DSN string or TNS name in ``NAME`` part. |
| 252 |
.. [5] Microsoft official `mssql-django <https://github.com/microsoft/mssql-django>`_ adapter. |
| 253 |
.. [6] Using the django-timescaledb Package which must be installed. |
| 254 |
|
| 255 |
|
| 256 |
Contributing |
| 257 |
------------ |
| 258 |
|
| 259 |
We welcome contributions to this project. Projects can take two forms: |
| 260 |
|
| 261 |
1. Raising issues or helping others through the github issue tracker. |
| 262 |
2. Contributing code. |
| 263 |
|
| 264 |
Raising Issues or helping others: |
| 265 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 266 |
|
| 267 |
When submitting an issue or helping other remember you are talking to humans who have feelings, jobs and lives of their |
| 268 |
own. Be nice, be kind, be polite. Remember english may not be someone first language, if you do not understand or |
| 269 |
something is not clear be polite and re-ask/ re-word. |
| 270 |
|
| 271 |
Contributing code: |
| 272 |
^^^^^^^^^^^^^^^^^^ |
| 273 |
|
| 274 |
* Before writing code be sure to check existing PR's and issues in the tracker. |
| 275 |
* Write code to the pylint spec. |
| 276 |
* Large or wide sweeping changes will take longer, and may face more scrutiny than smaller confined changes. |
| 277 |
* Code should be pass `black` and `flake8` validation. |