简体   繁体   中英

Django + Heroku Database

When editing the settings.py file in my Django app, I have the following:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'mydb', 
        'USER': 'myuser',
        'PASSWORD': 'password',
        'HOST': '',
        'PORT': '',
    }
}

# Parse database configuration from $DATABASE_URL
import dj_database_url
DATABASES['default'] =  dj_database_url.config()

Can someone explain what exactly is the result?

On the one hand, I have set up a postgresql database - which until now has been sqlite and used just locally for development.

Now I am deploying to Heroku, I have imported dj_database_url which I think sets up the link between my database and Heroku's own postgresql setup.

So does it matter what I set as the database, if Heroku is going to convert it to postgresql regardless?

There's no point having both of these. The last line of that snippet overwrites the default database config you defined previously.

Edit

This isn't anything to do with whether or not you use manage.py runserver. The only point is that Heroku defines an environment variable, DATABASE_URL, which contains the database location and credentials, which is then parsed by dj_database_url. What you should be doing is defining the same thing locally: the dj_database_url docs have some examples of the format to use for different db backends.

The best way to set up the env variable is to put it in a file called .env in your development directory, and then always start your app via foreman start (Foreman should have been installed by the Heroku toolbelt). See the Heroku getting started docs for more.

But if this doesn't work for you you can always just do export DATABASE_URL=whatever in your shell before you run manage.py for the first time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM