dj-database-url


Namedj-database-url JSON
Version 2.3.0 PyPI version JSON
download
home_pagehttps://github.com/jazzband/dj-database-url
SummaryUse Database URLs in your Django Application.
upload_time2024-10-23 10:05:19
maintainerNone
docs_urlNone
authorOriginal Author: Kenneth Reitz, Maintained by: JazzBand Community
requires_pythonNone
licenseBSD
keywords
VCS
bugtrack_url
requirements coverage mypy pyright
Travis-CI No Travis.
coveralls test coverage No coveralls.
            DJ-Database-URL
~~~~~~~~~~~~~~~

.. image:: https://jazzband.co/static/img/badge.png
   :target: https://jazzband.co/
   :alt: Jazzband

.. image:: https://github.com/jazzband/dj-database-url/actions/workflows/test.yml/badge.svg
   :target: https://github.com/jazzband/dj-database-url/actions/workflows/test.yml

.. image:: https://codecov.io/gh/jazzband/dj-database-url/branch/master/graph/badge.svg?token=7srBUpszOa
   :target: https://codecov.io/gh/jazzband/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.

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, CockroachDB, Timescale, Timescale (GIS) and SQLite.

Installation
------------

Installation is simple:

.. code-block:: console

    $ pip install dj-database-url

Usage
-----

1. If ``DATABASES`` is already defined:

- Configure your database in ``settings.py`` from ``DATABASE_URL``:

  .. code-block:: python

      import dj_database_url

      DATABASES['default'] = dj_database_url.config(
          conn_max_age=600,
          conn_health_checks=True,
      )

- Provide a default:

  .. code-block:: python

      DATABASES['default'] = dj_database_url.config(
          default='postgres://...',
          conn_max_age=600,
          conn_health_checks=True,
      )

- Parse an arbitrary Database URL:

  .. code-block:: python

      DATABASES['default'] = dj_database_url.parse(
          'postgres://...',
          conn_max_age=600,
          conn_health_checks=True,
      )

2. If ``DATABASES`` is not defined:

- Configure your database in ``settings.py`` from ``DATABASE_URL``:

  .. code-block:: python

      import dj_database_url

      DATABASES = {
          'default': dj_database_url.config(
              conn_max_age=600,
              conn_health_checks=True,
          ),
      }

- You can provide a default, used if the ``DATABASE_URL`` setting is not defined:

  .. code-block:: python

      DATABASES = {
          'default': dj_database_url.config(
              default='postgres://...',
              conn_max_age=600,
              conn_health_checks=True,
          )
      }

- Parse an arbitrary Database URL:

  .. code-block:: python

      DATABASES = {
          'default': dj_database_url.parse(
              'postgres://...',
              conn_max_age=600,
              conn_health_checks=True,
          )
      }

``conn_max_age`` sets the |CONN_MAX_AGE setting|__, which tells Django to
persist database connections between requests, up to the given lifetime in
seconds. If you do not provide a value, it will follow Django’s default of
``0``. Setting it is recommended for performance.

.. |CONN_MAX_AGE setting| replace:: ``CONN_MAX_AGE`` setting
__ https://docs.djangoproject.com/en/stable/ref/settings/#conn-max-age

``conn_health_checks`` sets the |CONN_HEALTH_CHECKS setting|__ (new in Django
4.1), which tells Django to check a persisted connection still works at the
start of each request. If you do not provide a value, it will follow Django’s
default of ``False``. Enabling it is recommended if you set a non-zero
``conn_max_age``.

.. |CONN_HEALTH_CHECKS setting| replace:: ``CONN_HEALTH_CHECKS`` setting
__ https://docs.djangoproject.com/en/stable/ref/settings/#conn-health-checks

Strings passed to `dj_database_url` must be valid URLs; in
particular, special characters must be url-encoded. The following url will raise
a `ValueError`:

.. code-block:: plaintext

    postgres://user:p#ssword!@localhost/foobar

and should instead be passed as:

.. code-block:: plaintext

    postgres://user:p%23ssword!@localhost/foobar

`TEST <https://docs.djangoproject.com/en/stable/ref/settings/#test>`_ settings can be configured using the ``test_options`` attribute::

    DATABASES['default'] = dj_database_url.config(default='postgres://...', test_options={'NAME': 'mytestdatabase'})


URL schema
----------

+----------------------+-----------------------------------------------+--------------------------------------------------+
| Engine               | Django Backend                                | URL                                              |
+======================+===============================================+==================================================+
| PostgreSQL           | ``django.db.backends.postgresql`` [1]_        | ``postgres://USER:PASSWORD@HOST:PORT/NAME`` [2]_ |
|                      |                                               | ``postgresql://USER:PASSWORD@HOST:PORT/NAME``    |
+----------------------+-----------------------------------------------+--------------------------------------------------+
| PostGIS              | ``django.contrib.gis.db.backends.postgis``    | ``postgis://USER:PASSWORD@HOST:PORT/NAME``       |
+----------------------+-----------------------------------------------+--------------------------------------------------+
| MSSQL                | ``sql_server.pyodbc``                         | ``mssql://USER:PASSWORD@HOST:PORT/NAME``         |
+----------------------+-----------------------------------------------+--------------------------------------------------+
| MSSQL [5]_           | ``mssql``                                     | ``mssqlms://USER:PASSWORD@HOST:PORT/NAME``       |
+----------------------+-----------------------------------------------+--------------------------------------------------+
| MySQL                | ``django.db.backends.mysql``                  | ``mysql://USER:PASSWORD@HOST:PORT/NAME`` [2]_    |
+----------------------+-----------------------------------------------+--------------------------------------------------+
| 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``      |
+----------------------+-----------------------------------------------+--------------------------------------------------+
| CockroachDB          | ``django_cockroachdb``                        | ``cockroach://USER:PASSWORD@HOST:PORT/NAME``     |
+----------------------+-----------------------------------------------+--------------------------------------------------+
| Timescale [6]_       | ``timescale.db.backends.postgresql``          | ``timescale://USER:PASSWORD@HOST:PORT/NAME``     |
+----------------------+-----------------------------------------------+--------------------------------------------------+
| Timescale (GIS) [6]_ | ``timescale.db.backend.postgis``              | ``timescalegis://USER:PASSWORD@HOST:PORT/NAME``  |
+----------------------+-----------------------------------------------+--------------------------------------------------+

.. [1] The django.db.backends.postgresql backend is named django.db.backends.postgresql_psycopg2 in older releases. For
       backwards compatibility, the old name still works in newer versions. (The new name does not work in older versions).
.. [2] With PostgreSQL or CloudSQL, 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``
       ``mysql://uf07k1i6d8ia0v@%2fcloudsql%2fproject%3alocation%3ainstance/dbname``
.. [3] 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``.
.. [4] 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.
.. [5] Microsoft official `mssql-django <https://github.com/microsoft/mssql-django>`_ adapter.
.. [6] Using the django-timescaledb Package which must be installed.


Contributing
------------

We welcome contributions to this project. Projects can take two forms:

1. Raising issues or helping others through the github issue tracker.
2. Contributing code.

Raising Issues or helping others:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

When submitting an issue or helping other remember you are talking to humans who have feelings, jobs and lives of their
own. Be nice, be kind, be polite. Remember english may not be someone first language, if you do not understand or
something is not clear be polite and re-ask/ re-word.

Contributing code:
^^^^^^^^^^^^^^^^^^

* Before writing code be sure to check existing PR's and issues in the tracker.
* Write code to the pylint spec.
* Large or wide sweeping changes will take longer, and may face more scrutiny than smaller confined changes.
* Code should be pass `black` and `flake8` validation.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/jazzband/dj-database-url",
    "name": "dj-database-url",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": null,
    "author": "Original Author: Kenneth Reitz, Maintained by: JazzBand Community",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/98/9f/fc9905758256af4f68a55da94ab78a13e7775074edfdcaddd757d4921686/dj_database_url-2.3.0.tar.gz",
    "platform": "any",
    "description": "DJ-Database-URL\n~~~~~~~~~~~~~~~\n\n.. image:: https://jazzband.co/static/img/badge.png\n   :target: https://jazzband.co/\n   :alt: Jazzband\n\n.. image:: https://github.com/jazzband/dj-database-url/actions/workflows/test.yml/badge.svg\n   :target: https://github.com/jazzband/dj-database-url/actions/workflows/test.yml\n\n.. image:: https://codecov.io/gh/jazzband/dj-database-url/branch/master/graph/badge.svg?token=7srBUpszOa\n   :target: https://codecov.io/gh/jazzband/dj-database-url\n\nThis simple Django utility allows you to utilize the\n`12factor <http://www.12factor.net/backing-services>`_ inspired\n``DATABASE_URL`` environment variable to configure your Django application.\n\nThe ``dj_database_url.config`` method returns a Django database connection\ndictionary, populated with all the data specified in your URL. There is\nalso a `conn_max_age` argument to easily enable Django's connection pool.\n\nIf you'd rather not use an environment variable, you can pass a URL in directly\ninstead to ``dj_database_url.parse``.\n\nSupported Databases\n-------------------\n\nSupport currently exists for PostgreSQL, PostGIS, MySQL, MySQL (GIS),\nOracle, Oracle (GIS), Redshift, CockroachDB, Timescale, Timescale (GIS) and SQLite.\n\nInstallation\n------------\n\nInstallation is simple:\n\n.. code-block:: console\n\n    $ pip install dj-database-url\n\nUsage\n-----\n\n1. If ``DATABASES`` is already defined:\n\n- Configure your database in ``settings.py`` from ``DATABASE_URL``:\n\n  .. code-block:: python\n\n      import dj_database_url\n\n      DATABASES['default'] = dj_database_url.config(\n          conn_max_age=600,\n          conn_health_checks=True,\n      )\n\n- Provide a default:\n\n  .. code-block:: python\n\n      DATABASES['default'] = dj_database_url.config(\n          default='postgres://...',\n          conn_max_age=600,\n          conn_health_checks=True,\n      )\n\n- Parse an arbitrary Database URL:\n\n  .. code-block:: python\n\n      DATABASES['default'] = dj_database_url.parse(\n          'postgres://...',\n          conn_max_age=600,\n          conn_health_checks=True,\n      )\n\n2. If ``DATABASES`` is not defined:\n\n- Configure your database in ``settings.py`` from ``DATABASE_URL``:\n\n  .. code-block:: python\n\n      import dj_database_url\n\n      DATABASES = {\n          'default': dj_database_url.config(\n              conn_max_age=600,\n              conn_health_checks=True,\n          ),\n      }\n\n- You can provide a default, used if the ``DATABASE_URL`` setting is not defined:\n\n  .. code-block:: python\n\n      DATABASES = {\n          'default': dj_database_url.config(\n              default='postgres://...',\n              conn_max_age=600,\n              conn_health_checks=True,\n          )\n      }\n\n- Parse an arbitrary Database URL:\n\n  .. code-block:: python\n\n      DATABASES = {\n          'default': dj_database_url.parse(\n              'postgres://...',\n              conn_max_age=600,\n              conn_health_checks=True,\n          )\n      }\n\n``conn_max_age`` sets the |CONN_MAX_AGE setting|__, which tells Django to\npersist database connections between requests, up to the given lifetime in\nseconds. If you do not provide a value, it will follow Django\u2019s default of\n``0``. Setting it is recommended for performance.\n\n.. |CONN_MAX_AGE setting| replace:: ``CONN_MAX_AGE`` setting\n__ https://docs.djangoproject.com/en/stable/ref/settings/#conn-max-age\n\n``conn_health_checks`` sets the |CONN_HEALTH_CHECKS setting|__ (new in Django\n4.1), which tells Django to check a persisted connection still works at the\nstart of each request. If you do not provide a value, it will follow Django\u2019s\ndefault of ``False``. Enabling it is recommended if you set a non-zero\n``conn_max_age``.\n\n.. |CONN_HEALTH_CHECKS setting| replace:: ``CONN_HEALTH_CHECKS`` setting\n__ https://docs.djangoproject.com/en/stable/ref/settings/#conn-health-checks\n\nStrings passed to `dj_database_url` must be valid URLs; in\nparticular, special characters must be url-encoded. The following url will raise\na `ValueError`:\n\n.. code-block:: plaintext\n\n    postgres://user:p#ssword!@localhost/foobar\n\nand should instead be passed as:\n\n.. code-block:: plaintext\n\n    postgres://user:p%23ssword!@localhost/foobar\n\n`TEST <https://docs.djangoproject.com/en/stable/ref/settings/#test>`_ settings can be configured using the ``test_options`` attribute::\n\n    DATABASES['default'] = dj_database_url.config(default='postgres://...', test_options={'NAME': 'mytestdatabase'})\n\n\nURL schema\n----------\n\n+----------------------+-----------------------------------------------+--------------------------------------------------+\n| Engine               | Django Backend                                | URL                                              |\n+======================+===============================================+==================================================+\n| PostgreSQL           | ``django.db.backends.postgresql`` [1]_        | ``postgres://USER:PASSWORD@HOST:PORT/NAME`` [2]_ |\n|                      |                                               | ``postgresql://USER:PASSWORD@HOST:PORT/NAME``    |\n+----------------------+-----------------------------------------------+--------------------------------------------------+\n| PostGIS              | ``django.contrib.gis.db.backends.postgis``    | ``postgis://USER:PASSWORD@HOST:PORT/NAME``       |\n+----------------------+-----------------------------------------------+--------------------------------------------------+\n| MSSQL                | ``sql_server.pyodbc``                         | ``mssql://USER:PASSWORD@HOST:PORT/NAME``         |\n+----------------------+-----------------------------------------------+--------------------------------------------------+\n| MSSQL [5]_           | ``mssql``                                     | ``mssqlms://USER:PASSWORD@HOST:PORT/NAME``       |\n+----------------------+-----------------------------------------------+--------------------------------------------------+\n| MySQL                | ``django.db.backends.mysql``                  | ``mysql://USER:PASSWORD@HOST:PORT/NAME`` [2]_    |\n+----------------------+-----------------------------------------------+--------------------------------------------------+\n| MySQL (GIS)          | ``django.contrib.gis.db.backends.mysql``      | ``mysqlgis://USER:PASSWORD@HOST:PORT/NAME``      |\n+----------------------+-----------------------------------------------+--------------------------------------------------+\n| SQLite               | ``django.db.backends.sqlite3``                | ``sqlite:///PATH`` [3]_                          |\n+----------------------+-----------------------------------------------+--------------------------------------------------+\n| SpatiaLite           | ``django.contrib.gis.db.backends.spatialite`` | ``spatialite:///PATH`` [3]_                      |\n+----------------------+-----------------------------------------------+--------------------------------------------------+\n| Oracle               | ``django.db.backends.oracle``                 | ``oracle://USER:PASSWORD@HOST:PORT/NAME`` [4]_   |\n+----------------------+-----------------------------------------------+--------------------------------------------------+\n| Oracle (GIS)         | ``django.contrib.gis.db.backends.oracle``     | ``oraclegis://USER:PASSWORD@HOST:PORT/NAME``     |\n+----------------------+-----------------------------------------------+--------------------------------------------------+\n| Redshift             | ``django_redshift_backend``                   | ``redshift://USER:PASSWORD@HOST:PORT/NAME``      |\n+----------------------+-----------------------------------------------+--------------------------------------------------+\n| CockroachDB          | ``django_cockroachdb``                        | ``cockroach://USER:PASSWORD@HOST:PORT/NAME``     |\n+----------------------+-----------------------------------------------+--------------------------------------------------+\n| Timescale [6]_       | ``timescale.db.backends.postgresql``          | ``timescale://USER:PASSWORD@HOST:PORT/NAME``     |\n+----------------------+-----------------------------------------------+--------------------------------------------------+\n| Timescale (GIS) [6]_ | ``timescale.db.backend.postgis``              | ``timescalegis://USER:PASSWORD@HOST:PORT/NAME``  |\n+----------------------+-----------------------------------------------+--------------------------------------------------+\n\n.. [1] The django.db.backends.postgresql backend is named django.db.backends.postgresql_psycopg2 in older releases. For\n       backwards compatibility, the old name still works in newer versions. (The new name does not work in older versions).\n.. [2] With PostgreSQL or CloudSQL, you can also use unix domain socket paths with\n       `percent encoding <http://www.postgresql.org/docs/9.2/interactive/libpq-connect.html#AEN38162>`_:\n       ``postgres://%2Fvar%2Flib%2Fpostgresql/dbname``\n       ``mysql://uf07k1i6d8ia0v@%2fcloudsql%2fproject%3alocation%3ainstance/dbname``\n.. [3] SQLite connects to file based databases. The same URL format is used, omitting\n       the hostname, and using the \"file\" portion as the filename of the database.\n       This has the effect of four slashes being present for an absolute file path:\n       ``sqlite:////full/path/to/your/database/file.sqlite``.\n.. [4] Note that when connecting to Oracle the URL isn't in the form you may know\n       from using other Oracle tools (like SQLPlus) i.e. user and password are separated\n       by ``:`` not by ``/``. Also you can omit ``HOST`` and ``PORT``\n       and provide a full DSN string or TNS name in ``NAME`` part.\n.. [5] Microsoft official `mssql-django <https://github.com/microsoft/mssql-django>`_ adapter.\n.. [6] Using the django-timescaledb Package which must be installed.\n\n\nContributing\n------------\n\nWe welcome contributions to this project. Projects can take two forms:\n\n1. Raising issues or helping others through the github issue tracker.\n2. Contributing code.\n\nRaising Issues or helping others:\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nWhen submitting an issue or helping other remember you are talking to humans who have feelings, jobs and lives of their\nown. Be nice, be kind, be polite. Remember english may not be someone first language, if you do not understand or\nsomething is not clear be polite and re-ask/ re-word.\n\nContributing code:\n^^^^^^^^^^^^^^^^^^\n\n* Before writing code be sure to check existing PR's and issues in the tracker.\n* Write code to the pylint spec.\n* Large or wide sweeping changes will take longer, and may face more scrutiny than smaller confined changes.\n* Code should be pass `black` and `flake8` validation.\n",
    "bugtrack_url": null,
    "license": "BSD",
    "summary": "Use Database URLs in your Django Application.",
    "version": "2.3.0",
    "project_urls": {
        "GitHub": "https://github.com/jazzband/dj-database-url/",
        "Homepage": "https://github.com/jazzband/dj-database-url",
        "Release log": "https://github.com/jazzband/dj-database-url/blob/master/CHANGELOG.md"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e591641a4e5c8903ed59f6cbcce571003bba9c5d2f731759c31db0ba83bb0bdb",
                "md5": "6c0ac402f1343663810cfcf58aae21d6",
                "sha256": "bb0d414ba0ac5cd62773ec7f86f8cc378a9dbb00a80884c2fc08cc570452521e"
            },
            "downloads": -1,
            "filename": "dj_database_url-2.3.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6c0ac402f1343663810cfcf58aae21d6",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 7793,
            "upload_time": "2024-10-23T10:05:41",
            "upload_time_iso_8601": "2024-10-23T10:05:41.254345Z",
            "url": "https://files.pythonhosted.org/packages/e5/91/641a4e5c8903ed59f6cbcce571003bba9c5d2f731759c31db0ba83bb0bdb/dj_database_url-2.3.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "989ffc9905758256af4f68a55da94ab78a13e7775074edfdcaddd757d4921686",
                "md5": "30bb7dc323216b2e0ac524e09873e3b0",
                "sha256": "ae52e8e634186b57e5a45e445da5dc407a819c2ceed8a53d1fac004cc5288787"
            },
            "downloads": -1,
            "filename": "dj_database_url-2.3.0.tar.gz",
            "has_sig": false,
            "md5_digest": "30bb7dc323216b2e0ac524e09873e3b0",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 10980,
            "upload_time": "2024-10-23T10:05:19",
            "upload_time_iso_8601": "2024-10-23T10:05:19.953961Z",
            "url": "https://files.pythonhosted.org/packages/98/9f/fc9905758256af4f68a55da94ab78a13e7775074edfdcaddd757d4921686/dj_database_url-2.3.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-23 10:05:19",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "jazzband",
    "github_project": "dj-database-url",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "coverage",
            "specs": []
        },
        {
            "name": "mypy",
            "specs": []
        },
        {
            "name": "pyright",
            "specs": []
        }
    ],
    "lcname": "dj-database-url"
}
        
Elapsed time: 0.84146s