Monday, October 14, 2013

Django + Heroku - Local and Heroku database environement settings

I deployed a Django app in Heroku following these instructions:

https://devcenter.heroku.com/articles/getting-started-with-django

I could push source code to Heroku and the app ran successfully in that environment.

But, when I run "./manage syncdb" command locally, the console told me that I had an improperly DATABASE setting.

I figured out that the setting line of Heroku is the problem:

DATABASES['default'] =  dj_database_url.config()


It overrode the default database setting of Django which was right above this line. Only Heroku will understand and use a specific setting for its environment.

So, I used the following method to help Django to figure out the DB engine:

DATABASES = {}

import dj_database_url
import socket
if socket.gethostname() not in ['trinh-pc']:
    DATABASES['default'] =  dj_database_url.config()
else:
    DATABASES['default'] =  dj_database_url.config(default='postgres://myuser:mypwd@localhost/mydb')


'trinh-pc' is my machine's hostname.

I also deleted the default DATABASE setting line of Django.