pythondjangopostgresqlheroku

How should I set my DATABASE_URL?


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?


Solution

  • 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'