How do I set DATABASE_URL?

I am working on my first Heroku / Django application. I just want to be sure that I am setting the variables DATABASE_URL

and correctly DATABASES

. 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 have only DATABASES['default'] = dj_database_url.config()

, and I am trying to use Django commands such as run server

or migrate

, I get the following error: NameError: name 'DATABASES' is not defined

. I installed DATABASE_URL

as doing this brings up an issue with this issue (after creating the database my_app

).

Everything seems to work fine when I code and test, but I've also seen half a dozen different ways to set database variables on the internet. If not, I would like to fix it now. What really confuses me is when I push my application to Heroku, how will the data be pushed to the internet when the database is / usr / local / var / postgres? Or will it not happen at all? Am I just too embarrassed / tired at this point?

+3


source to share


2 answers


It's a simple matter of logic. You cannot set a default key in the DATABASES dictionary before you define the dictionary yourself.



Whether the parameter has been set to a default

value dj_database_url

within the call or as a separate variable DATABASE_URL

does not matter, especially since it won't even be used in Heroku, since it will be overridden by environment variables.

+1


source


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 to combine database pools , add these bits as well. More details

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

      

+8


source







All Articles