Understanding DJ - Database - Url - App Development
Understanding DJ - Database - Url - App Development
Understanding dj_database_url
May 1, 2019
• Jeremy Zhou
Understanding dj_database_url
This blog post is meant for people who are already in the middle of setting up their Django
project! If this is not you, I would recommend that you check out these three tutorials first. For
this blog post, I assume that you are already familiar with the way database configuration works
in settings.py .
In the course of following a beginner tutorial for hooking Django Postgres to Heroku, you may
have come across this interesting piece of code:
import dj_database_url
DATABASES = {
Why dj_database_url ?
https://nzufelt.github.io/csc630_s19_blog/2019/05/01/jeremy-1.html 1/4
9/27/21, 10:52 AM Understanding dj_database_url | CSC630 Full Stack App Development
When you succeed in setting up Django so that it connects with the local Postgres database,
your code in settings.py probably looks something like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'database',
'USER': 'user',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '5432',
As a recap, this just tells Django the host and port to connect to Postgres, the database it should
look for, and what user it should log in as when accessing it.
This code seems awfully hardcoded, though, and Django is supposed to be a framework that
avoids hardcoding. And this is precisely the problem: what happens if you need to run the code
somewhere besides your local machine? You would have to change the configuration manually.
This can get very ugly, for example, if you’re working with a group who all have different copies
of the code.
What is the solution? Let’s take the configuration out of the project and into the system. This way,
it’ll be whatever it needs to be for that system. Where would it go then? Well, that’s what
environment variables are for!
>>> import os
>>> os.getenv('LANG')
'en_US.UTF-8'
Whether you’ve heard of environment variables or not, you have almost definitely experienced
at least one of their multitudinous uses. The environment variable that is probably most well-
known is PATH . Whenever you run anything from the command line, such as python , the
computer looks through all of the directories listed in PATH for an executable file matching the
https://nzufelt.github.io/csc630_s19_blog/2019/05/01/jeremy-1.html 2/4
9/27/21, 10:52 AM Understanding dj_database_url | CSC630 Full Stack App Development
name python . Thus, if people install new things in nonstandard locations, all they have to do to
let the command line know is edit the PATH environment variable.
This is very useful - imagine if we had no power to change where the command line looked for
executable files! Or if we did, imagine if we had to change this specification separately for every
single app that we download! No one likes to hardcode - unless you’re doing USACO and you’re
down to your last half hour - so environment variables are great news.
See here for more information regarding environment variables and how to set them.
DATABASES = {
DATABASE_URL = postgres://user:password@localhost:5432/database
The way you do this depends on your operating system. See here (same link as above).
'ENGINE': 'django.db.backends.postgres',
'NAME': 'database',
'USER': 'user',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '5432',
and stuff it into the DATABASES['default'] variable, just like what we had before.
https://nzufelt.github.io/csc630_s19_blog/2019/05/01/jeremy-1.html 3/4
9/27/21, 10:52 AM Understanding dj_database_url | CSC630 Full Stack App Development
Now, when you transfer the project to a different environment, it will run just fine (given that
you configured DATABASE_URL properly)! For instance, getting Django Postgres working on
Heroku is immediate, since Heroku auto-generates the value for DATABASE_URL .
DATABASE_URL = postgres://atiezzmwujaoas:81s113t3pilgwoteedrkiz2aa7mg6v2xckpe5deh
Hardcoding this would not have been enjoyable. (This is just a randomly generated example, but
you get the idea.)
Well, that’s that for one of the many mysteries of Django. It’s only one of many, but one is one
nonetheless.
Image sources
https://www.djangoproject.com/m/img/logos/django-logo-positive.png
</span>
CSC630 Full Stack App nzufelt A blog created for the Spring 2019 CSC630
Development course covering Full Stack App
Development.
https://nzufelt.github.io/csc630_s19_blog/2019/05/01/jeremy-1.html 4/4