2

So I made a change locally and then pushed to github and heroku, then when I accessed my live heroku project I noticed that the database was oddly reverting to the objects that I had during my local development.

This very well could be because I didn't set up my heroku postgresql db correctly (this is my first time setting up a heroku postgresql with django).

Here is a snippet from my Settings.py, please assume the information in brackets:

ON_HEROKU = os.environ.get('ON_HEROKU')
HEROKU_SERVER = os.environ.get('HEROKU_SERVER')


if ON_HEROKU:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': '<the name is here>',
            'USER': '<user is here>',
            'PASSWORD': '<password is here>',
            'HOST': '<host is here>',                      
            'PORT': '<port is here>',   
        }
    }
else:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
            'USER': '',
            'PASSWORD': '',
            'HOST': '',                      
            'PORT': '',  
        }
    }

I ran a syncdb, and it seems to do nothing. :

(venv)$ heroku run python manage.py syncdb --account personal
Running `python manage.py syncdb` attached to terminal... up, run.7965
Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

For the record, the first time I ran syncdb, it seemed to never set up a new user, rather it just tried to install more tables. I don't know why, but this seems like a red flag.

I have also run the following commands to check if the postgres db is there:

(venv)$ heroku addons --account personal | grep POSTGRES
heroku-postgresql:hobby-dev  HEROKU_POSTGRESQL_COBALT

Even though everything seems to be okay, my project is using SQLite db on live. I must be missing something. Any advice appreciated. I can show more if needed.

1 Answer 1

3

Heroku doesn't provide an ON_HEROKU environment variable. Unless you've set that yourself, this won't work.

However you should not be hard-coding the production database values in your settings anyway. As the Heroku docs explain, those values are provided by environment variables, and the dj-database-url library should be used to convert that into the correct settings automatically. So you can either use the presence or absence of the DATABASE_URL env var to switch between the hard-coded sqlite settings and the dynamic production ones, or even better set a DATABASE_URL in your local dev environment too, pointing to your sqlite file.

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

1 Comment

I have been reading about the method of not hardcoding it, but I think I am getting confused with how to build a DATABASE_URL. Do you think you could link to an example, the heroku docs aren't really enough. As for the 'ON_HEROKU' variable, I have seen this used in other stacks, I didn't know it wouldn't work.

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.