Skip to content

Instantly share code, notes, and snippets.

@jbothma
Last active April 25, 2025 17:42
Show Gist options
  • Save jbothma/8a9a30399c2091d89763bff0a1952da4 to your computer and use it in GitHub Desktop.
Save jbothma/8a9a30399c2091d89763bff0a1952da4 to your computer and use it in GitHub Desktop.
Database Setup for django project using DJ-Database-URL

This is a short guide for common database setup for a django app using DJ-Database-URL. This is focused on setting up a development environment. Production will be similar but instead of using the default DB location and credentials, you should use the location of the actual DB server and you really ought to be using a secret password.

Find the DB name and credentials

The simplest is to use the default database connection parameters, but you can override them in your development environment.

Using the defaults

Look in the project settings file. For a project called publicpeople, you'd probably find the database settings in publicpeople/settings.py around a set of config code like the following:

import dj_database_url
db_config = dj_database_url.config(default='postgres://publicpeople@localhost/publicpeople')
db_config['ATOMIC_REQUESTS'] = True
DATABASES = {
    'default': db_config,
}

In this case, the database connection details are the URL in the default argument to the dj_database_url.config function call.

That URL means:

  • username: publicpeople
  • password: none
  • database hostname: localhost
  • database name: publicpeople

If the URL has a colon and something before the @, the password is that something, e.g. in postgres://publicpeople:somepassword@localhost/publicpeople the password is somepassword

Using custom DB config

If you want to use your own custom DB credentials (e.g. in production, or because you want to use a different database name in development), run all django commands (once you've created the database and user) that need the database with the environment variable DATABASE_URL, e.g.

DATABASE_URL=postgres://username:somepassword@172.0.0.2/dbname python manage.py runserver

or

export DATABASE_URL=postgres://username:somepassword@172.0.0.2/dbname
python python manage.py migrate

Create the role and database

You can create roles and databases within psql (postgres command line client) or using postgres command line programs.

Within psql

Connect to the database as a postgres admin user, e.g.

sudo su postgres
[sudo] password for fakeuser: 
[postgres@fakehost public-people]$ psql
psql (10.3)
Type "help" for help.

postgres=# 

Then create the role

postgres=# create role publicpeople with login password 'somepassword' ;

Allow superusername to make the role owne the new db

postgres=# grant publicpeople to superusername

And finally create the database

postgres=# create database publicpeople with owner publicpeople;

Using postgres command line programs

See createuser and createdb at https://www.codementor.io/engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb#a2-the-createuser-utility

@gidsey
Copy link

gidsey commented Jul 30, 2021

Hi @jbothma thanks for sharing this guide. I wondered if you had any experince using dj_database_url with multiple databases? I'm looking to set up something similar to this

    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'contentdb',
        'USER': 'postgres',
        'PASSWORD': 'admin1234',
        'HOST': 'localhost',
        'PORT': '5432'
    },
    'usersdb': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'usersdb',
        'USER': 'postgres',
        'PASSWORD': 'admin1234',
        'HOST': 'localhost',
        'PORT': '5432'
    }
}```

But unsure how to set a second `DATABASE_URL` with dj_database_url. Any ideas appreciated!

@jbothma
Copy link
Author

jbothma commented Jul 31, 2021 via email

@gidsey
Copy link

gidsey commented Aug 2, 2021

Thanks @jbothma - I'll check out django environ, it looks to be exactly what I need.

@AndrzejOlejniczak
Copy link

Hi @jbothma. My app uses postgis engine ( 'ENGINE': 'django.contrib.gis.db.backends.postgis'). How do I retain this information in this: db_config = dj_database_url.config(default='postgres://publicpeople@localhost/publicpeople') ?

@Akwaboah
Copy link

As describe above, call the db_config instance and override the ENGINE like below

db_config = dj_database_url.config(default='postgres://publicpeople@localhost/publicpeople')
db_config['ENGINE']='django.contrib.gis.db.backends.postgis'

the db_config key which is the ENGINE has now been re-assign

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment