0

I have a database created from non-django app and have defined the database connection info like below

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': os.environ.get('POSTGRES_DB'),
        'USER': os.environ.get('POSTGRES_USER'),
        'PASSWORD': os.environ.get('POSTGRES_PASSWORD'),
        'HOST': os.environ.get('POSTGRES_HOST'),
        'PORT': os.environ.get('POSTGRES_PORT'),
    }
}

Unfortunately, when I migrate the django admin related tables, they all go into the default database.

I know that it is possible to declare multiple databases and separate read and write actions to different databases; however, what I would like to do is to have all the default django admin related tables to be created into another database. Say I declare a second database like below, how do I make sure that django admin related datas get migrated to the second database and also read from that when I login to django admin?

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': os.environ.get('POSTGRES_DB'),
        'USER': os.environ.get('POSTGRES_USER'),
        'PASSWORD': os.environ.get('POSTGRES_PASSWORD'),
        'HOST': os.environ.get('POSTGRES_HOST'),
        'PORT': os.environ.get('POSTGRES_PORT'),
    },
    'admin':{
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': os.environ.get('ADMIN_POSTGRES_DB'),
        'USER': os.environ.get('ADMIN_POSTGRES_USER'),
        'PASSWORD': os.environ.get('ADMIN_POSTGRES_PASSWORD'),
        'HOST': os.environ.get('ADMIN_POSTGRES_HOST'),
        'PORT': os.environ.get('ADMIN_POSTGRES_PORT'),
    }
}
forJ
  • 4,309
  • 6
  • 34
  • 60
  • 1
    I was struggling with multiple databases too. Maybe you would like to check my [question](https://stackoverflow.com/questions/58608319/use-multiple-databases-in-django-with-only-one-table-django-migrations). I answered it myself, I think it covers your problem too. – cezar Feb 06 '20 at 08:40

1 Answers1

0

first you must leave default dictionary as empty

then define your database for admin and non admin

for examample do this

DATABASES = {
    'default':{},
    'nonAdmin': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': os.environ.get('POSTGRES_DB'),
        'USER': os.environ.get('POSTGRES_USER'),
        'PASSWORD': os.environ.get('POSTGRES_PASSWORD'),
        'HOST': os.environ.get('POSTGRES_HOST'),
        'PORT': os.environ.get('POSTGRES_PORT'),
    },
    'admin':{
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': os.environ.get('ADMIN_POSTGRES_DB'),
        'USER': os.environ.get('ADMIN_POSTGRES_USER'),
        'PASSWORD': os.environ.get('ADMIN_POSTGRES_PASSWORD'),
        'HOST': os.environ.get('ADMIN_POSTGRES_HOST'),
        'PORT': os.environ.get('ADMIN_POSTGRES_PORT'),
    }
}

then after that you can migrate database using

./manage.py migrate --database=admin

or

./manage.py migrate --database=nonAdmin

If you don’t want every application to be synchronized onto a particular database, you can define a database router that implements a policy constraining the availability of particular models.

by using this method allow_migrate(db, app_label, model_name=None, **hints)

you can reference this document https://docs.djangoproject.com/en/3.0/topics/db/multi-db/#topics-db-multi-db-routing

MecaTheclau
  • 188
  • 1
  • 3
  • 14
  • So without specifying any additional configurations, django would just know during runtime which database I have migrated admin tables to? – forJ Feb 06 '20 at 08:44
  • with database router you specify which database to used when migrating database or when selecting you can check the link provided above for more – MecaTheclau Feb 06 '20 at 08:55