Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django multiple databases with router deployment on heroku

I am trying to configure multiple databases and a database router on my django project with heroku.

With one database it was easy to use

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

in my local settings its

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME':  'nameuser3',                  
        'USER': 'postgres',
        'PASSWORD': 'XXXXXXXX',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    },
    'admindb': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME':  'nameadmin4',                  
        'USER': 'postgres',
        'PASSWORD': 'xxxxxx',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }    
}

I also have database routers setup to read and write the particular databases.

def db_for_read(self, model, **hints):
    if model._meta.app_label == 'sgdb':
        return 'admindb'
    return 'default'

def db_for_write(self, model, **hints):
    if model._meta.app_label == 'sgdb':
        return 'admindb'
    return 'default'

def allow_relation(self, obj1, obj2, **hints):
    db1 = self.DATABASE_APPS_MAPPING.get(obj1._meta.app_label)
    db2 = self.DATABASE_APPS_MAPPING.get(obj2._meta.app_label)
    if db1 and db2:
        return db1 == db2
    return 'None'

def allow_migrate(self, db, app_label, model_name=None, **hints):
    return True

I googled but couldn't find anything conclusive on how to configure the database router to work with heroku databases. Any clues on how can this be done?


Full settings.py I uploaded to heroku -

import dj_database_url
import os

"""
Django settings for example project.

Generated by 'django-admin startproject' using Django 1.9.

For more information on this file, see
https://docs.djangoproject.com/en/1.9/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/ref/settings/
"""



# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
# BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'disqus',
    'django.contrib.sitemaps',
    'easy_maps',
    'hitcount',
    'accounts',
    'localflavor',
    'carts',
    'marketing',
    'crispy_forms',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.facebook'
]


CRISPY_TEMPLATE_PACK = 'bootstrap3'
# Sign IN settings
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_LOGIN_ON_EMAIL_CONFIRMATION = True
# Sign UP settings
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_EMAIL_VERIFICATION = 'optional'
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'
SOCIALACCOUNT_QUERY_EMAIL = True
SOCIALACCOUNT_PROVIDERS = {
    'facebook': {
        'SCOPE': ['email', 'publish_stream'],
        'METHOD': 'js_sdk'  # instead of 'oauth2'
    }
}

SITE_NAME='example.com'


#Newsletter
NEWSLETTER_CONFIRM_EMAIL = False

HITCOUNT_HITS_PER_IP_LIMIT = 0
HITCOUNT_KEEP_HIT_IN_DATABASE = { 'days': 30 }


DISQUS_WEBSITE_SHORTNAME = 'example'

EMAIL_BACKEND =  'django.core.mail.backends.smtp.EmailBackend' 
DEFAULT_FROM_EMAIL = '[email protected]'
EMAIL_HOST =  'smtp.gmail.com'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'example'
EMAIL_USE_TLS = True 
EMAIL_PORT = 587

SITE_ID = 2

DEBUG = False

if DEBUG: 
    SITE_URL = "http://127.0.0.1:8000"
if not DEBUG :
    SITE_URL = "http://www.example.com"


MIDDLEWARE_CLASSES = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'example.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'example.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases



# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

# Internationalization
# https://docs.djangoproject.com/en/1.9/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/

# STATIC_URL = '/static/'
# STATIC_ROOT = os.path.join((BASE_DIR), 'static_cdn')
# MEDIA_ROOT = os.path.join((BASE_DIR), 'media_cdn')
# MEDIA_URL = '/media/'

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join((BASE_DIR), 'static_cdn')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join((BASE_DIR), 'media_cdn')
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]

# db_from_env = dj_database_url.config(conn_max_age=500)
# db_from_env2 = dj_database_url.config(default ='postgres://address', conn_max_age=500)
# DATABASES['default'].update(db_from_env)
# DATABASES['admin'].update(db_from_env2)

DATABASES = {'default': dj_database_url.config(default='postgres://address') , 
            'admin': dj_database_url.config(default='postgres://urladdress')}

DATABASE_ROUTERS = ['exampleapp.routers.DatabaseAppsRouter',]



SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

ALLOWED_HOSTS = ['*']
like image 532
headcrabz Avatar asked Sep 15 '25 12:09

headcrabz


1 Answers

Ah finally Found it.

Firstly have to do . heroku pg:info . see what database urls are linked to your app. you should see multiple data bases for your app. Else create a new database on heroku and connect it with your app.

Then in the settings.py keep it the same way as local settings.py . dont use dj_database_url. that defaults to database_url. and voila the heroku is all set up for multiple dbs.

phew took so hours and hours to figure it out. thanks

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME':  'xxxxxxx',                  
        'USER': 'xxxxxxx',
        'PASSWORD': 'xxxxxxxxxxx',
        'HOST': 'xxxxxxxxxxxxxxx.compute-1.amazonaws.com',
        'PORT': '5432',
    },
    'admin': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME':  'xxxxxxxxxxxx',                  
        'USER': 'xxxxxxxxxxxxxxxxxxx',
        'PASSWORD': 'xxxxxxxxxxxxxxxxxxxx',
        'HOST': 'xxxxxxxxxxxxxxxxxx.compute-1.amazonaws.com',
        'PORT': '5432',
    }    
}
like image 153
headcrabz Avatar answered Sep 17 '25 01:09

headcrabz