[Django]-How should I set my DATABASE_URL?

24👍

This is documented on Heroku Devecenter

# Parse database configuration from $DATABASE_URL
import dj_database_url
# DATABASES['default'] =  dj_database_url.config()
#updated
DATABASES = {'default': dj_database_url.config(default='postgres://user:pass@localhost/dbname')}

If you need Database connection pooling add this bits too. More details

# Enable Connection Pooling
DATABASES['default']['ENGINE'] = 'django_postgrespool'

4👍

This is a simple matter of logic. You can’t set the “default” key of the DATABASES dictionary before you have defined the dictionary itself.

Whether or not you set the default parameter to dj_database_url inside the call or as a separate DATABASE_URL variable is irrelevant, especially as that won’t even be used on Heroku as it will be overridden by environment variables.

2👍

This allows you to use any database settings during development,
but at production (on Heroku), DATABASES['default'].update(db_from_env) changes the database settings to the one created by Heroku.

import dj_database_url


DATABASES = {
    '"default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": os.path.join(BASE_DIR, "db.sqlite3"),
    }
}


db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)

You could use any database settings, but the last two lines allow heroku to create its own database for you.

Leave a comment