5

I have a project that works on Heroku, I don't have PostgreSQL installed on my local machine. I want to keep running the app on my local machine using sqlite3, but when I push it to Heroku it will convert to pg

All I am trying to do is to have an IF condition if this is development then run sqlite3 .. but if it's production run then following command.

DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': '',
    'HOST': '',
    'PORT': 5432,
    'USER': '',
    'PASSWORD': ''
  }
}

Heroku is working with dj_database_url

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

It basically similar to Rails when we define the gems for production and another gems for testing and development.

Oth
  • 53
  • 3
  • Try multiple settings files. See this question: http://stackoverflow.com/questions/1626326/how-to-manage-local-vs-production-settings-in-django – hanleyhansen Apr 15 '14 at 22:31

2 Answers2

3

You can create a local_settings.py file inside your project, and import it from your base setting file. This way you can have different settings for each environment.

This local_setting file should be included in your .gitignore

maxicecilia
  • 136
  • 1
  • 7
1

Do you use VirtualEnvs?

You can setup the settings.py like:

DATABASES = {
    'default': {
    'ENGINE': get_var('DB_ENGINE'),
    'NAME': get_var('DB_NAME'),
    'HOST': get_var('DB_HOST'),
    ...
    }
}

where get_var is reading the environment variables. These envvars are set by your virtualenv's postactivate file.

The production virtualenv postactivate sets the envvars DB_ENGINE, DB_NAME, DB_HOST with postgresql values

The dev virtualenv postactivate file sets env vars corresponding to the development DB.

zaphod
  • 2,045
  • 1
  • 14
  • 18
  • Are you saying that in heroku these environment vars _are_ set for us, or do we have to something ourselves to set those in production? – GreenAsJade May 04 '14 at 05:50
  • You have to set the environment variables in your production environment. – zaphod May 08 '14 at 02:22
  • interesting, where do we have to put the var name and value? all I know for now is how to create a virtualenv and modify the apps installed using pip – Oth Oct 10 '14 at 17:00