Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify PostgreSQL Schema in Django

I am trying to build some DB front-ends using Django since I like the general MVC structure as well as their template system and admin interface. However, I am having trouble with certain aspects of using a PostgreSQL back-end.

I've managed to figure out how to specify different schemas (and databases) using the database section of the settings.py file like so:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
    'MPS': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'OPTIONS': {
            'options': '-c search_path=django'
        },
       'NAME': 'sandbox_manzer',
       'HOST': '192.168.2.151',
       'PORT': '5434',
       'USER': '******',
       'PASSWORD': '**********************',
    }
}

However, when I try and script some basic tests, as detailed here, I run into a problem. My user can create databases just fine so my test_db gets built, but when the django app goes to build the models into tables I get an error stating that No schema is specified for the test_db. Any help in how I specify schema names for the test_db would be most appreciated. Especially since I intend to build out multiple apps for a single project using different schemas as logical separators.

Thanks!

like image 842
RyanM Avatar asked Jan 03 '17 20:01

RyanM


People also ask

How do I change the schema in PostgreSQL?

PostgreSQL ALTER SCHEMA statement overviewALTER SCHEMA schema_name RENAME TO new_name; In this syntax: First, specify the name of the schema that you want to rename after the ALTER SCHEMA keywords. Second, specify the new name of the schema after the RENAME TO keywords.

What is database schema in Django?

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.


1 Answers

I suppose you are familiar with the concept of PostgreSQL schemas.

Your app is looking for the django schema in your database (which presumably isn’t there) because you have specifically told it so.

       'options': '-c search_path=django'

For each application, create a suitably named schema and specify it in the search_path.

like image 56
Dario Avatar answered Sep 23 '22 04:09

Dario