D
D
DUDE2020-07-24 22:59:28
Django
DUDE, 2020-07-24 22:59:28

How to connect multiple databases?

Hello everyone, this is the story.

I have two databases: db_1, db_2. I want to make sure that all jungian default tables are stored in db_1 and certain models are stored in db_2. However, db_2 has two users. The first is the default postgres and the second is test_user which has read-only permission on all tables in db_2.

1. How do I organize all this history in settings.DATABASES? Is this entry correct? If not, how to do it right?

DATABASES = {
    'default': {
        'ENGINE': "django.db.backends.postgresql_psycopg2",
        'NAME': "db_1",
        "USER": "postgres",
        "HOST": "localhost",
        "PASSWORD": "postgres_password",
        "PORT": ""
    },
    'secondary_postgress': {
        'ENGINE': "django.db.backends.postgresql_psycopg2",
        'NAME': "db_2",
        "USER": "postgres",
        "HOST": "localhost",
        "PASSWORD": "postgres_password",
        "PORT": ""
    },
    'secondary_test_user': {
        'ENGINE': "django.db.backends.postgresql_psycopg2",
        'NAME': "db_2",
        "USER": "test_user",
        "HOST": "localhost",
        "PASSWORD": "test_user_password",
        "PORT": ""
    }
}


2. How can I make sure that certain models are stored exclusively in db_2? There is a lot of information on the Internet, as far as I understand, you need to connect a router for the database and rewrite the save(using="db_2") method of the model. But I did not quite understand how everything should work correctly, since the information is mostly not complete.

3. What is the right way to do migration for both databases in this case?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-07-25
@bacon

as information, basically, is not complete.
All "integral" information in the dock https://docs.djangoproject.com/en/3.0/topics/db/mu... Default behavior is prescribed in routers, and using is needed when you need to use something other than "default" base. Where to do the migration is also prescribed in the routers.
And what for to you two different connections to base db_2? What do you want to achieve with this?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question