How to Use Select Several PostgreSQL Database Schema for Generating Model in Django Application

Posted on

Introduction

In this article, the main content or the main subject is still has a specific relation with several other articles. Those articles are the article which is focusing on how to generate model, duplicate or backup model in Django application. Several of those articles are How to Backup or Duplicate Model Class Django Application in Microsoft Windows using inspectdb and also How to Use Another Database for Generating Model in Django Application. The first article in How to Backup or Duplicate Model Class Django Application in Microsoft Windows using inspectdb is using a default database definition in the Django project. On the other hand, the second one in  How to Use Another Database for Generating Model in Django Application is using another database definition.

But apparently, in the case of using a PostgreSQL database, there is something missing. By default, using that setting, it will only allow to access the ‘public’ schema. The setting is not useful if there is a need to be able to generate, backup or duplicate model from another schema. Below is the actual database definition of the PostgreSQL database which is only accessing the ‘public’ schema for generating, duplicating or backup up into a file consisting of Django models in the ‘settings.py’ file :

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

DATABASES = {
  # 'default': {
        # 'ENGINE': 'django.db.backends.sqlite3',
        # 'NAME': BASE_DIR / 'db.sqlite3',
  # }

    'default': {
       'ENGINE': 'django.db.backends.postgresql_psycopg2',
       'NAME': 'django_training',
       'USER': 'postgres',
       'PASSWORD': 'password',
       'HOST': 'localhost',
       'PORT': '5432'
    },
    'myapp' : {
       'ENGINE': 'django.db.backends.postgresql_psycopg2',
       'NAME': 'db_myapp',
       'USER': 'postgres',
       'PASSWORD': 'password',
       'HOST': 'localhost',
       'PORT': '5432'
    }
}

How to Use Select Several PostgreSQL Database Schema for Generating Model in Django Application

Actually, there is a solution for that. Especially to define several schemas available in the PostgreSQL database for further generating, duplicating or backup the model. Just add an additional setting to the PostgreSQL database defintion. Below is the revision for selecting not only the ‘public’ schema but also another schema :

'OPTIONS' : {
        'options': '-c search_path=admin,public'
},

Just add and list all of the schema which is going to be selected. In the above example, there is only two schemas including ‘public’ schema. So, the other schema is the ‘admin’ schema. As an overall configuration, the following is the database configuration setting for selecting specific schemas :

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

DATABASES = {
  # 'default': {
        # 'ENGINE': 'django.db.backends.sqlite3',
        # 'NAME': BASE_DIR / 'db.sqlite3',
  # }

    'default': {
       'ENGINE': 'django.db.backends.postgresql_psycopg2',
       'NAME': 'django_training',
       'USER': 'postgres',
       'PASSWORD': 'password',
       'HOST': 'localhost',
       'PORT': '5432'
    },
    'myapp' : {
       'ENGINE': 'django.db.backends.postgresql_psycopg2',
       'OPTIONS' : {
               'options': '-c search_path=admin,public'
       },
       'NAME': 'db_myapp',
       'USER': 'postgres',
       'PASSWORD': 'password',
       'HOST': 'localhost',
       'PORT': '5432'
    }
}

Leave a Reply