A schema in a SQL database is a list of logical data structures. It's commonly used for security-related management and permissions.
The feature is supported by PostgreSQL
and very well by Django.
Let's see how to configure schema in Django for a PostgreSQL
database.
Creating the database
First, ensure that the database is created.
CREATE DATABASE coredb;
CREATE USER core WITH PASSWORD '12345678';
GRANT ALL PRIVILEGES ON DATABASE coredb TO core;
And let's create a schema.
CREATE SCHEMA AUTHORIZATION core;
CREATE SCHEMA IF NOT EXISTS coreschema AUTHORIZATION core;
We've created coreschema
in the coredb
database. And only the core
user can access the schema for the moment.
Let's see how to use it with Django.
Django models configuration
Naturally, make sure that the Django project is configured with Django. It'll look like this.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'coredb',
'USER': 'core',
'PASSWORD': '12345678',
'HOST': 'localhost',
'PORT': 5432
}
}
And when working with a model, use the Meta
class to tell Django the name of the database table to use for the model.
class Fruit(AbstractModel):
name = models.CharField(max_length=100)
class Meta:
db_table = '"coreschema". "fruit"'
And run the migrations commands. And the table will be created under coreschema
.
Want to add something or have some comments? Let's discuss this in the comments section.
Article posted using bloggu.io. Try it for free.