Django: PostgreSQL local and in Heroku?

0

I'd like to use PostgreSQL locally and in Heroku. How do I set my settings.py to know which setting to use?

I have tried to put it as an environment variable, but I still can not. Today I have this:

#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), }
    
asked by anonymous 05.12.2018 / 21:14

1 answer

0

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

pip install dj-database-url

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

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

Then I create the database in postgres locally. Hence in%% of% 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 the postgres add-on there. The heroku already creates a randomly named bank, but also adds a URL to the environment variables following the same scheme there as settings.py .

    
09.12.2018 / 15:51