Use Database URLs in your Django Application.

Related tags

Djangodj-database-url
Overview

DJ-Database-URL

https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fjacobian%2Fdj-database-url%2Fbadge&style=for-the-badge

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:[email protected]:PORT/NAME [2]
PostGIS django.contrib.gis.db.backends.postgis postgis://USER:[email protected]:PORT/NAME
MSSQL sql_server.pyodbc mssql://USER:[email protected]:PORT/NAME
MySQL django.db.backends.mysql mysql://USER:[email protected]:PORT/NAME
MySQL (GIS) django.contrib.gis.db.backends.mysql mysqlgis://USER:[email protected]: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:[email protected]:PORT/NAME [4]
Oracle (GIS) django.contrib.gis.db.backends.oracle oraclegis://USER:[email protected]:PORT/NAME
Redshift django_redshift_backend redshift://USER:[email protected]: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, you can also use unix domain socket paths with percent encoding: postgres://%2Fvar%2Flib%2Fpostgresql/dbname.
[3] (1, 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.
[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.
Comments
  • 📣  looking for maintainers

    📣 looking for maintainers

    I'd adopted this codebase with every intention of being a good maintainer, but that's clearly not happening. There's a backlog of issues I'm unable to get to, and it's clearly leading to some frustration. So, I'm taking the nudge of Volunteer Responsibility Amnesty Day and looking for new maintainers.

    If you'd like to pick up maintaining this repo, leave a comment here and let me know. I'll check back in a week or so and do the logistics.

    I am mildly wary of supply-chain attacks, so I do have a few requirements to try to protect our users:

    1. If more than one person step up I'd like y'all to coordinate creating an organization to transfer this repo to; if it's just a single person I can transfer it to your personal github or a repo, either way.
    2. Anyone who's going to be a maintainer needs at least a bit of history here on github -- I won't be ok transferring this to a new account.
    3. I'd like you to commit to cutting a new release within a month or so, as a way of notifying users that the ownership has changed. I'll grant you access to PyPI to do the release, but I'll also keep access; I'll had that last bit over once this new release goes out.

    Sorry I haven't been a good steward; I'm sure the next folks will do better!

    opened by jacobian 16
  • Implement Jazzband guidelines for dj-database-url

    Implement Jazzband guidelines for dj-database-url

    This issue tracks the implementation of the Jazzband guidelines for the project dj-database-url

    It was initiated by @jacobian who was automatically assigned in addition to the Jazzband roadies.

    See the TODO list below for the generally required tasks, but feel free to update it in case the project requires it.

    Feel free to ping a Jazzband roadie if you have any question.

    TODOs

    • [x] Fix all links in the docs (and README file etc) from old to new repo
    • [x] Add the Jazzband badge to the README file
    • [x] Add the Jazzband contributing guideline to the CONTRIBUTING.md or CONTRIBUTING.rst file
    • [x] Check if continuous testing works with GitHub Actions
    • [x] Check if test coverage is tracked with Codecov
    • [x] Add jazzband account to PyPI project as maintainer role (e.g. URL: https://pypi.org/manage/project/dj-database-url/collaboration/)
    • [ ] Add jazzband-bot as maintainer to the Read the Docs project (e.g. URL: https://readthedocs.org/dashboard/dj-database-url/users/)
    • [ ] Add incoming GitHub webhook integration to Read the Docs project (e.g. URL: https://readthedocs.org/dashboard/dj-database-url/integrations/)
    • [x] Fix project URL in GitHub project description
    • [x] Review project if other services are used and port them to Jazzband
    • [x] Decide who is project lead for the project (if at all)
    • [x] Set up CI for Jazzband project releases if needed and open ticket if yes

    Project details

    Description Use Database URLs in your Django Application.
    Homepage https://pypi.python.org/pypi/dj-database-url
    Stargazers 1238
    Open issues 34
    Forks 185
    Default branch master
    Is a fork False
    Has Wiki True
    Has Pages False
    opened by jazzband-bot 12
  • Handle special characters in database URL

    Handle special characters in database URL

    If the password contains special characters such as ? or /, the following exception is raised (here I tried to use "foo?bar" as a password):

    Traceback (most recent call last):
      File "./manage.py", line 19, in <module>
        execute_from_command_line(sys.argv)
      File "/var/www/myproject/dev/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
        utility.execute()
      File "/var/www/myproject/dev/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 376, in execute
        sys.stdout.write(self.main_help_text() + '\n')
      File "/var/www/myproject/dev/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 240, in main_help_text
        for name, app in six.iteritems(get_commands()):
      File "/var/www/myproject/dev/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 107, in get_commands
        apps = settings.INSTALLED_APPS
      File "/var/www/myproject/dev/env/local/lib/python2.7/site-packages/django/conf/__init__.py", line 54, in __getattr__
        self._setup(name)
      File "/var/www/myproject/dev/env/local/lib/python2.7/site-packages/django/conf/__init__.py", line 49, in _setup
        self._wrapped = Settings(settings_module)
      File "/var/www/myproject/dev/env/local/lib/python2.7/site-packages/django/conf/__init__.py", line 128, in __init__
        mod = importlib.import_module(self.SETTINGS_MODULE)
      File "/var/www/myproject/dev/env/local/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
        __import__(name)
      File "/var/www/myproject/dev/myproject/myproject/settings/base.py", line 175, in <module>
        'default': dj_database_url.parse(get_env_variable('DATABASE_URL'))
      File "/var/www/myproject/dev/env/local/lib/python2.7/site-packages/dj_database_url.py", line 84, in parse
        'PORT': url.port or '',
      File "/usr/lib/python2.7/urlparse.py", line 110, in port
        port = int(port, 10)
    ValueError: invalid literal for int() with base 10: 'foo'
    

    I tried to escape the special characters with backslashes but then it got interpreted as "foo". I also tried to url-encode them, ie. replacing "?" by %63 but then the password returned by dj-database-url was "foo%63bar".

    The issue can be easily reproduced with the following URL: postgres://foo:[email protected]/foobar.

    Maybe I'm just missing something but if that's the case I think it would be worth documenting it somewhere.

    opened by sephii 12
  • See about moving this to an official Django project

    See about moving this to an official Django project

    Given the widespread use of dj-database-url, it seems a good idea for it to become Official Project, per DEP 7. Maintenance burden seems minimal but important.

    Read DEP 7 for full details, but roughly there are 4 steps:

    1. Pre-proposal discussion
    2. Form a team
    3. Formal proposal
    4. Django Technical Board Vote

    Let's use this thread to tackle (1) and (2). This means we need to

    work out if there is rough agreement that the project is a good thing for Django to adopt, particularly focusing on any alternative approaches to the same problem and the relative merits of them, including code design, scalability, alignment with existing Django design and philosophy, and having an available development and maintenance team.

    What do folks thing?

    Also: I'll be the shepherd for this proposal, but for it to go forward we'd need a larger maintenance team than just me. If there's anyone -- Django committer or not -- who would be willing to commit to long-term maintenance, please ✋ raise your hand here.

    [Backstory, and why this is living on my personal github right now. @kennethreitz was looking for a new maintainer, and reached out to me to see if Django might be interested. I thought perhaps so, and offered to take over maintainership in the meantime while we work through the DEP 7 process. If it turns out that an Official Project is the wrong home for this, I'll either maintain it long-term or transfer it again.]

    question 
    opened by jacobian 10
  • Django 3.0+ Uses deprecated postgresql_psycopg2 package

    Django 3.0+ Uses deprecated postgresql_psycopg2 package

    I'm getting this error

    Traceback (most recent call last):
      File "/Users/jesse.kleve/src/playgrounds/django/dori/api/venv/lib/python3.7/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
        self.connect()
      File "/Users/jesse.kleve/src/playgrounds/django/dori/api/venv/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner
        return func(*args, **kwargs)
      File "/Users/jesse.kleve/src/playgrounds/django/dori/api/venv/lib/python3.7/site-packages/django/db/backends/base/base.py", line 197, in connect
        self.connection = self.get_new_connection(conn_params)
      File "/Users/jesse.kleve/src/playgrounds/django/dori/api/venv/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner
        return func(*args, **kwargs)
      File "/Users/jesse.kleve/src/playgrounds/django/dori/api/venv/lib/python3.7/site-packages/django/db/backends/postgresql/base.py", line 185, in get_new_connection
        connection = Database.connect(**conn_params)
      File "/Users/jesse.kleve/src/playgrounds/django/dori/api/venv/lib/python3.7/site-packages/psycopg2/__init__.py", line 125, in connect
        dsn = _ext.make_dsn(dsn, **kwargs)
      File "/Users/jesse.kleve/src/playgrounds/django/dori/api/venv/lib/python3.7/site-packages/psycopg2/extensions.py", line 174, in make_dsn
        parse_dsn(dsn)
    psycopg2.ProgrammingError: invalid dsn: invalid connection option "init_command"
    

    It seems to be using the wrong backend. It's using 'django.db.backends.postgresql_psycopg2' with DATABASE_URL='postgres://root:[email protected]:5432/django'.

    This is with Django version 3.0

    opened by jkleve 8
  • Modernize dj-database-url

    Modernize dj-database-url

    I think this PR addresses a number of outstanding issues, namely:

    • The ability to register custom backends/schemas (and simultaneously only support "blessed" Django backends by default)
    • The ability to pass arbitrary OPTIONS into config and parse
    • Cleaner customization of backend-specific options
    • Modernize the testing matrix
    • Blacken (and isort) the codebase

    As I don't want to impose any more maintenance burden on you, consider this PR also as an offer to maintain this library. My original plan was to simply fork it and release under a new name, but figured it was worth offering. The current test suite is almost entirely unchanged (and passing - https://github.com/imsweb/django-dburl/actions/runs/1373144981) - it only drops pre-2.0 Django support, and registers the previously-supported backends:

    https://github.com/imsweb/django-dburl/blob/modernize/test_dj_database_url.py#L12-L23

    Happy to discuss further. I have a few projects at work that use this, so I'm invested in keeping it up to date, either by maintaining this library directly or maintaining my own fork under a new name.

    opened by dcwatson 7
  • PyPi dj-database-url points to different/old github codebase.

    PyPi dj-database-url points to different/old github codebase.

    I have just realised that the PyPi dj-database-url is pointing to a forked version of this under a different name (https://github.com/kennethreitz/dj-database-url , 0.5.0 from 2018, and no recent activity), but this repo looks like it has more recnt active than that and more up to date with new Django versions.

    Are you aware of this?

    I am interested in using this. Hense, is there a 'stable/production' version of this available via Pip install?

    Will your next release replace the above or be under a different name?

    Kind Regards, Hitesh Patel

    docs 
    opened by hcpatel 6
  • fix #96 deprecated postgres backend strings

    fix #96 deprecated postgres backend strings

    The database backend "django.db.backends.postgresql_psycopg2" has been deprecated in Django 2.0 in favor of "django.db.backends.postgresql".

    https://docs.djangoproject.com/en/2.0/releases/2.0/#id1

    opened by jtdoepke 6
  • URL-decode whole hostname when appropriate

    URL-decode whole hostname when appropriate

    Adds support for other special characters in UNIX socket path names, like the colons in Cloud SQL paths.

    See also: https://github.com/kennethreitz/dj-database-url/issues/66

    opened by jdp 6
  • Added support for percent-encoded Postgres paths.

    Added support for percent-encoded Postgres paths.

    This allows the user to specify unix domain sockets as host.

    Example: postgresql://%2Fvar%2Flib%2Fpostgresql/dbname

    Reference: http://www.postgresql.org/docs/9.2/interactive/libpq-connect.html#AEN38162

    opened by dbrgn 6
  • Exception while using the connection string with postgres

    Exception while using the connection string with postgres

    Hi, I am getting the following exception when using the connection string.

    engine = SCHEMES[url.scheme] if engine is None else engine KeyError: 'postgresql+psycopg2'

    Also my connection string starts with postgresql+psycopg2

    Can you please suggest me how to fix it? Thanks!

    opened by zugspitze 5
  • Support MongoDB with djongo backend

    Support MongoDB with djongo backend

    opened by Nagico 1
  • Throw warning/exception if DATABASE_URL isn't set

    Throw warning/exception if DATABASE_URL isn't set

    Fixes https://github.com/jazzband/dj-database-url/issues/114

    Along the way, to make the test cleaner, it fixes all the explicit "set os.environ" calls with mock.patch work instead.

    opened by palfrey 3
  • Generic backend registration and arbitrary django settings support

    Generic backend registration and arbitrary django settings support

    Add support for custom backend registration with optional config post-processing. Add support for passing arbitrary django database settings as kwargs. Add feature description to README and make README more clear. Extract tests with deprecated options to separate suite to simplify further removal.

    opened by alexanderGerbik 5
  • Should we raise a KeyError if the env var is missing and default is None?

    Should we raise a KeyError if the env var is missing and default is None?

    Shouldn't this function raise a KeyError if s is None?

    def config(env=DEFAULT_ENV, default=None, engine=None, conn_max_age=0, ssl_require=False):
        """Returns configured DATABASE dictionary from DATABASE_URL."""
    
        config = {}
    
        s = os.environ.get(env, default)
    
        if s:
            config = parse(s, engine, conn_max_age, ssl_require)
    
        return config
    

    If the user has not properly configured his environment settings (most likely), and not provided a default fallback value either (also likely), the config will return an empty dictionary, implicitly silencing the issue... (speaking of experience here...) https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-python

    question 
    opened by dschenck 6
  • heroku+django+mysql: sslmode not supported.

    heroku+django+mysql: sslmode not supported.

    Based on docs: there's no 'sslmode' attribute. The following code gives me the exception: 'sslmode' is an invalid keyword argument. This issue is possibly related to other backends like sqlite as well.

    Workaround: On settings.py after: django_heroku.settings(locals()) add: del DATABASES['default']['OPTIONS']['sslmode']

    feature needs pr easy pickings 
    opened by lwaite 13
Releases(v1.2.0)
  • v1.2.0(Dec 13, 2022)

    What's Changed

    • allow TEST settings to be passed into a db config by @Brodan in https://github.com/jazzband/dj-database-url/pull/116
    • Support urlencoding/decoding for hostname by @Dresdn in https://github.com/jazzband/dj-database-url/pull/181
    • [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in https://github.com/jazzband/dj-database-url/pull/194
    • Fixes #195 - config missin conn_health_check parameter

    New Contributors

    • @Brodan made their first contribution in https://github.com/jazzband/dj-database-url/pull/116
    • @Dresdn made their first contribution in https://github.com/jazzband/dj-database-url/pull/181

    Full Changelog: https://github.com/jazzband/dj-database-url/compare/v1.1.0...v1.2.0

    Source code(tar.gz)
    Source code(zip)
  • v1.1.0(Dec 12, 2022)

    What's Changed

    • Added support for Timescale and Timescale (GIS) by @lino in https://github.com/jazzband/dj-database-url/pull/174
    • [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in https://github.com/jazzband/dj-database-url/pull/175
    • [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in https://github.com/jazzband/dj-database-url/pull/177
    • Fix linter violation by @alexanderGerbik in https://github.com/jazzband/dj-database-url/pull/179
    • [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in https://github.com/jazzband/dj-database-url/pull/182
    • Support Django 4.1 and remove compatibility code by @adamchainz in https://github.com/jazzband/dj-database-url/pull/183
    • [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in https://github.com/jazzband/dj-database-url/pull/188
    • Use urlsplit() instead of urlparse() by @adamchainz in https://github.com/jazzband/dj-database-url/pull/184
    • CHANGELOG: Fix date format by @cfra in https://github.com/jazzband/dj-database-url/pull/192
    • add support to python 3.11 by @jairhenrique in https://github.com/jazzband/dj-database-url/pull/189
    • [pre-commit.ci] pre-commit autoupdate by @pre-commit-ci in https://github.com/jazzband/dj-database-url/pull/193
    • Improve by @jairhenrique in https://github.com/jazzband/dj-database-url/pull/190
    • Add ability to set CONN_HEALTH_CHECKS by @adamchainz in https://github.com/jazzband/dj-database-url/pull/185

    New Contributors

    • @lino made their first contribution in https://github.com/jazzband/dj-database-url/pull/174
    • @alexanderGerbik made their first contribution in https://github.com/jazzband/dj-database-url/pull/179
    • @adamchainz made their first contribution in https://github.com/jazzband/dj-database-url/pull/183
    • @cfra made their first contribution in https://github.com/jazzband/dj-database-url/pull/192
    • @jairhenrique made their first contribution in https://github.com/jazzband/dj-database-url/pull/189

    Full Changelog: https://github.com/jazzband/dj-database-url/compare/v1.0.0...v1.1.0

    Source code(tar.gz)
    Source code(zip)
  • v1.0.0(Jul 29, 2022)

    Initial release since the project was migrated to Jazzband.

    We are bumping to version 1.0.0 for a couple of reasons, though none of them are truely breaking API changes.

    • 1.0.0 dignifies a long awaited release of dj-database-url, the first since 2018.
    • We are dropping support of python2.7 🪦
    • Supported python versions >=3.7

    Thank you for helping to push this project forwards.

    Source code(tar.gz)
    Source code(zip)
A handy tool for generating Django-based backend projects without coding. On the other hand, it is a code generator of the Django framework.

Django Sage Painless The django-sage-painless is a valuable package based on Django Web Framework & Django Rest Framework for high-level and rapid web

sageteam 51 Sep 15, 2022
A fresh approach to autocomplete implementations, specially for Django. Status: v3 stable, 2.x.x stable, 1.x.x deprecated. Please DO regularely ping us with your link at #yourlabs IRC channel

Features Python 2.7, 3.4, Django 2.0+ support (Django 1.11 (LTS), is supported until django-autocomplete-light-3.2.10), Django (multiple) choice suppo

YourLabs 1.7k Jan 01, 2023
A Django GraphQL (Graphene) base template

backend A Django GraphQL (Graphene) base template Make sure your IDE/Editor has Black and EditorConfig plugins installed; and configure it lint file a

Reckonsys 4 May 25, 2022
https://django-storages.readthedocs.io/

Installation Installing from PyPI is as easy as doing: pip install django-storages If you'd prefer to install from source (maybe there is a bugfix in

Josh Schneier 2.3k Jan 06, 2023
This Django app will be used to host Source.Python plugins, sub-plugins, and custom packages.

Source.Python Project Manager This Django app will be used to host Source.Python plugins, sub-plugins, and custom packages. Want to help develop this

2 Sep 24, 2022
Bootstrap 3 integration with Django.

django-bootstrap3 Bootstrap 3 integration for Django. Goal The goal of this project is to seamlessly blend Django and Bootstrap 3. Want to use Bootstr

Zostera B.V. 2.3k Jan 03, 2023
An orgizational tool to keep track of tasks/projects and the time spent on them.

Django-Task-Manager Task Tracker using Python Django About The Project This project is an orgizational tool to keep track of tasks/projects and the ti

Nick Newton 1 Dec 21, 2021
Dynamic Django settings.

Constance - Dynamic Django settings A Django app for storing dynamic settings in pluggable backends (Redis and Django model backend built in) with an

Jazzband 1.5k Jan 07, 2023
Learn Python and the Django Framework by building a e-commerce website

The Django-Ecommerce is an open-source project initiative and tutorial series built with Python and the Django Framework.

Very Academy 275 Jan 08, 2023
A clone of https://virgool.io written in django

Virgool clone A clone of virgool blog written in django Installation first rename the .env.sample to .env and fill it. with docker docker-compose up -

Danial Selmipoor 7 Dec 23, 2022
Django Course Project - TextCorrector

Django-TextUtils Django Course Project A tool for analyzing text data in Django backend. It is a project where you can do some of the things with you

1 Oct 29, 2021
Py-instant-search-redis - Source code example for how to build an instant search with redis in python

py-instant-search-redis Source code example for how to build an instant search (

Giap Le 4 Feb 17, 2022
Opinionated boilerplate for starting a Django project together with React front-end library and TailwindCSS CSS framework.

Opinionated boilerplate for starting a Django project together with React front-end library and TailwindCSS CSS framework.

João Vítor Carli 10 Jan 08, 2023
Use minify-html, the extremely fast HTML + JS + CSS minifier, with Django.

django-minify-html Use minify-html, the extremely fast HTML + JS + CSS minifier, with Django. Requirements Python 3.8 to 3.10 supported. Django 2.2 to

Adam Johnson 60 Dec 28, 2022
Simple XML-RPC and JSON-RPC server for modern Django

django-modern-rpc Build an XML-RPC and/or JSON-RPC server as part of your Django project. Major Django and Python versions are supported Main features

Antoine Lorence 82 Dec 04, 2022
Django Federated Login provides an authentication bridge between Django projects and OpenID-enabled identity providers.

Django Federated Login Django Federated Login provides an authentication bridge between Django projects and OpenID-enabled identity providers. The bri

Bouke Haarsma 18 Dec 29, 2020
Modular search for Django

Haystack author: Daniel Lindsley date: 2013/07/28 Haystack provides modular search for Django. It features a unified, familiar API that allows you to

Daniel Lindsley 4 Dec 23, 2022
django-idom allows Django to integrate with IDOM

django-idom allows Django to integrate with IDOM, a package inspired by ReactJS for creating responsive web interfaces in pure Python.

113 Jan 04, 2023
Basic implementation of Razorpay payment gateway 💳 with Django

Razorpay Payment Integration in Django 💥 In this project Razorpay payment gateway 💳 is integrated with Django by breaking down the whole process int

ScaleReal 12 Dec 12, 2022
A blog app powered by python-django

Django_BlogApp This is a blog app powered by python-django Features Add and delete blog post View someone else blog Can add comment to that blog And o

Manish Jalui 1 Sep 12, 2022