sharing django generated tables across multiple databases

Hi,
Is there any way to share django generated tables(auth_user, django_session, django_migrations etc) across multiple hosts?
For example, Here i want to use the ‘default’ database host django generated tables in ‘first’ database without migrate command.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'HOST': 'localhost',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PORT': 5432,
    },
    'first': {
        'ENGINE': 'django.db.backends.postgresql',
        'HOST': 'localhost01',
        'NAME': 'postgres01',
        'USER': 'release',
        'PORT': 5432,
    }
   
}

Yes, you can do this with a database router (see an example in docs) by defining db_for_read(), db_for_write, e.g.

class DjangoRouter:
    route_app_labels = {'auth', 'sessions'}

    def db_for_read(self, model, **hints):
        if model._meta.app_label in self.route_app_labels:
            return 'default'
        return None

    def db_for_write(self, model, **hints):
        if model._meta.app_label in self.route_app_labels:
            return 'default'
        return None

django_migrations has entries for migrations applied to a specific database, so you should have it on both default and first.

Thank you @felixxm for your help. I have a follow up question on this. I am using legacy database and i want to use django generated tables(django_content_type, django_migrations, django_admin_log) which are in default are to be shared to first without creating these tables in first.

I’m not sure if I understand you question :thinking: from Django’s perspective you don’t need to do anything special. Databases are just the source of data and you can freely switch (via QuerySet.using()) between them. If you are talking about cross-db relations they are not supported.

As for django_migrations, it has entries for migrations applied to a specific database, so you cannot share it. If you don’t want to run migrations on first you don’t need it on this db.