Django: Local Postgresql and Heroku?

Asked

Viewed 466 times

0

I would like to use Postgresql locally and on Heroku. How I set up mine settings.py to know which configuration to use?

I’ve tried putting it as an environment variable, but I haven’t been able to. Today I have it:

#Conectar localmente no sqlite
default_dburl = 'sqlite:///' + os.path.join(BASE_DIR, 'db.sqlite3')
#Se não tiver sqlite, conecta no postgres
DATABASES = { 'default': config('DATABASE_URL', default=default_dburl, cast=dburl), }

1 answer

0

I have basically the same configuration and can make use of postgres locally and in Heroku. I install the same lib:

pip install dj-database-url

With him installed, there in my .env I have something like this:

DATABASE_URL=postgres://user:password@localhost:5432/database_name

Then I create the bank in the postgres locally. Thence in the settings.py I have:

from dj_database_url import parse as db_url
DATABASES = {
    'default': config(
        'DATABASE_URL',
        default='sqlite:///{}'.format(os.path.join(BASE_DIR, 'db.sqlite3')),
        cast=db_url),
}

When it comes time to deploy in Heroku, I add there the postgres add-on. Heroku already creates a database with a random name, but also adds in the environment variables a URL following that same scheme there .env.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.