2

I've added a Postgres database to my Heroku app, and am trying to connect my Django app to it. However, my app always connects to the local Postgres database instead.

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'app_db',
        'USER': 'admin',
        'PASSWORD': '',
        'HOST': 'localhost',
        'PORT': '',
    }
}

db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)

I've set 'DATABASE_URL' in the .env file to be the url for the Postgres database on my Heroku app, but it doesn't seem to update the database. How do I force my app to connect to the Heroku database rather than the local one?

4
  • Can you confirm that your code actually sees DATABASE_URL environment variable with something like import os; print os.environ['DATABASE_URL']? Just put this before db_from_env variable, restart the server and check output. Commented Apr 17, 2017 at 20:41
  • It throws a KeyError; seems like it isn't actually seeing it. I thought dj_database_url.config() was supposed to read the .env file and grab the DATABASE_URL value there? It does seem to work if I do export 'DATABASE_URL'=postgres:blahblah, but I can't figure out why it won't work with just having it in .env Commented Apr 17, 2017 at 20:46
  • if you really want to store your env variables in the .env file, consider using https://github.com/theskumar/python-dotenv maybe. Commented Apr 17, 2017 at 20:50
  • dj_database_url.config() doesn't read a file, instead it checks the environment variable value Commented Apr 17, 2017 at 20:51

2 Answers 2

5

You can set up your settings to read from your local DB when in development and use your Heroku DB in production. First of all, as you may already know, you need dj_database_url.

You can make a seperate settings file called local_settings.py and in there include your normal db config (e.g. the default Django db config). And in your settings.py:

DATABASES = {
    'default': dj_database_url.config()
}

And at the bottom of your settings.py:

# Tries to import local settings, if on dev, 
# import everything in local_Settings, which overrides the dj_database_url
# If on deploy, local_settings won't be found so just ignore the ImportError
try:
    from .local_settings import *
except ImportError:
    pass

So your local_settings.py should be in your development server, not in Heroku app (you can ignore it adding it to .gitignore). I hope this is understandable.

Sign up to request clarification or add additional context in comments.

Comments

2

Connect to Live heroku DB from local Django code without having to manually set the DATABASE_URL env variable:

Put following code in your settings.py file

import os, subprocess, dj_database_url

bashCommand = “heroku config:get DATABASE_URL -a app_name” #Use your app_name

output = subprocess.check_output([‘bash’,’-c’, bashCommand]).decode(“utf-8”) # executing the bash command and converting byte to string

DATABASES[‘default’] = dj_database_url.config(default=output,conn_max_age=600, ssl_require=True) #making connection to heroku DB without having to set DATABASE_URL env variable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.