Oth
Oth

Reputation: 53

How to define two databases in django one for Production and one for Development

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.

Upvotes: 5

Views: 1085

Answers (2)

zaphod
zaphod

Reputation: 2095

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.

Upvotes: 1

maxicecilia
maxicecilia

Reputation: 136

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

Upvotes: 3

Related Questions