Package: dj-database-url
This simple Django utility allows you to utilize the12factorinspiredDATABASE_URLenvironment variable to configure your Django application.Thedj_database_url.configmethod returns a Django database connection
dictionary, populated with all the data specified in your URL. There is
also aconn_max_ageargument to easily enable Django’s connection pool.If you’d rather not use an environment variable, you can pass a URL in directly
instead todj_database_url.parse.Supported DatabasesSupport currently exists for PostgreSQL, PostGIS, MySQL, MySQL (GIS),
Oracle, Oracle (GIS), Redshift, CockroachDB, Timescale, Timescale (GIS) and SQLite.InstallationInstallation is simple:$pipinstalldj-database-urlUsageIfDATABASESis already defined:Configure your database insettings.pyfromDATABASE_URL:importdj_database_urlDATABASES['default']=dj_database_url.config(conn_max_age=600,conn_health_checks=True,)Provide a default:DATABASES['default']=dj_database_url.config(default='postgres://...',conn_max_age=600,conn_health_checks=True,)Parse an arbitrary Database URL:DATABASES['default']=dj_database_url.parse('postgres://...',conn_max_age=600,conn_health_checks=True,)IfDATABASESis not defined:Configure your database insettings.pyfromDATABASE_URL:importdj_database_urlDATABASES={'default':dj_database_url.config(conn_max_age=600,conn_health_checks=True,),}You can provide a default, used if theDATABASE_URLsetting is not defined:DATABASES={'default':dj_database_url.config(default='postgres://...',conn_max_age=600,conn_health_checks=True,)}Parse an arbitrary Database URL:DATABASES={'default':dj_database_url.parse('postgres://...',conn_max_age=600,conn_health_checks=True,)}conn_max_agesets theCONN_MAX_AGEsetting, 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 of0. Setting it is recommended for performance.conn_health_checkssets theCONN_HEALTH_CHECKSsetting(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 ofFalse. Enabling it is recommended if you set a non-zeroconn_max_age.Strings passed todj_database_urlmust be valid URLs; in
particular, special characters must be url-encoded. The following url will raise
aValueError:postgres://user:p#ssword!@localhost/foobarand should instead be passed as:postgres://user:p%23ssword!@localhost/foobarTESTsettings can be configured using thetest_optionsattribute:DATABASES['default'] = dj_database_url.config(default='postgres://...', test_options={'NAME': 'mytestdatabase'})URL schemaEngineDjango BackendURLPostgreSQLdjango.db.backends.postgresql[1]postgres://USER:PASSWORD@HOST:PORT/NAME[2]PostGISdjango.contrib.gis.db.backends.postgispostgis://USER:PASSWORD@HOST:PORT/NAMEMSSQLsql_server.pyodbcmssql://USER:PASSWORD@HOST:PORT/NAMEMSSQL[5]mssqlmssqlms://USER:PASSWORD@HOST:PORT/NAMEMySQLdjango.db.backends.mysqlmysql://USER:PASSWORD@HOST:PORT/NAME[2]MySQL (GIS)django.contrib.gis.db.backends.mysqlmysqlgis://USER:PASSWORD@HOST:PORT/NAMESQLitedjango.db.backends.sqlite3sqlite:///PATH[3]SpatiaLitedjango.contrib.gis.db.backends.spatialitespatialite:///PATH[3]Oracledjango.db.backends.oracleoracle://USER:PASSWORD@HOST:PORT/NAME[4]Oracle (GIS)django.contrib.gis.db.backends.oracleoraclegis://USER:PASSWORD@HOST:PORT/NAMERedshiftdjango_redshift_backendredshift://USER:PASSWORD@HOST:PORT/NAMECockroachDBdjango_cockroachdbcockroach://USER:PASSWORD@HOST:PORT/NAMETimescale[6]timescale.db.backends.postgresqltimescale://USER:PASSWORD@HOST:PORT/NAMETimescale (GIS)[6]timescale.db.backend.postgistimescalegis://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](1,2)With PostgreSQL or CloudSQL, you can also use unix domain socket paths withpercent encoding:postgres://%2Fvar%2Flib%2Fpostgresql/dbnamemysql://uf07k1i6d8ia0v@%2fcloudsql%2fproject%3alocation%3ainstance/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 omitHOSTandPORTand provide a full DSN string or TNS name inNAMEpart.[5]Microsoft officialmssql-djangoadapter.[6](1,2)Using the django-timescaledb Package which must be installed.ContributingWe welcome contributions to this project. Projects can take two forms:Raising issues or helping others through the github issue tracker.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 passblackandflake8validation.