31

I'm working on my first-ever Heroku/Django app. I just want to be sure I'm setting my DATABASE_URL and DATABASES variables correctly. Here's what's in my code:

import dj_database_url

DATABASE_URL = 'postgresql:///my_app'

# Parse database configuration from $DATABASE_URL
DATABASES = {
    'default': dj_database_url.config(default=DATABASE_URL)
}

When I just have DATABASES['default'] = dj_database_url.config() and I try to use Django commands like run server or migrate I get the following error: NameError: name 'DATABASES' is not defined. I set the DATABASE_URL since doing so appears to solve this issue (after I create the my_app database).

Everything appears to be working fine as I code and test, but I've also seen a half-dozen different ways to set the database variables on the internet. If this isn't correct, I'd like to fix it now. The thing that really confuses me is, when I push my app to Heroku, how will the data get pushed to the web, when the database is /usr/local/var/postgres? Or will this not happen at all? Am I just too confused/tired at this point?

2
  • DATABASES['default'] = dj_database_url.config() returns NameError because DATABASES dictionary is not present there. DATABASES = {} DATABASES['default'] = dj_database_url.config() should work Commented May 5, 2015 at 6:18
  • I already had a Postgresql database connected while tunning in localhost, but when I hosted on Heroku it created its own database, I need to change that and link my already existing database. How to do that, Can anyone help me with this?? Thank you. Commented Jun 8, 2021 at 2:19

3 Answers 3

26

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'
Sign up to request clarification or add additional context in comments.

1 Comment

So when I just have DATABASES['default'] = dj_database_url.config() and I try to use Django commands like runserver or migrate I get the following error: NameError: name 'DATABASES' is not defined. I set the DATABASE_URL since doing so appears to solve this issue.
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 Comments

Right. I forgot that I deleted the original declaration of DATABASES. Thanks for pointing that out.
@daniel, how to specify test database using dj_database_url ?
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.