[Django]-Point manage.py to a specific PostegreSQL schema

2👍

Here’s what worked in the end:

I wrote a new manage.py command that was a wrapper for the commands I wanted to be schema-aware (syncdb, migrate, changepassword), and select the schema manually by:

from django.db import connection
cursor = connection.cursor()
cursor.execute('SET search_path TO ' + my_little_schema)
👤Goro

4👍

You can assign a default schema to a database user or – to be precise – you assign a schema search path to a user. If that path only contains a single schema, you effectively set a default schema for the user.

Any unqualified object name (table, view, function, …) is then looked for along the path. Unqualified CREATE statements will create the object in the first schema of the search path.

So all you need to do is to change the user that Djange uses to connect to the database:

alter user django_user set search_path to the_one_schema;
commit;

After that, e.g. a create table foo will create the table in the schema the_one_schema.

You need to log off and then log on again to make the change “active”.

Leave a comment