16

I'm using PostgreSQL 9.3 and Django 1.7.4 with psycopg2 2.5.4

The DBA, asked us to create a schema for our application instead of using public.

We defined the schema, and we had to add the

'OPTIONS': {
    'options': '-c search_path=custom-schema-name'
},

to the settings.

During testing, Django is creating the test database with the corresponding name, but we can't set the custom-schema name

I tried to find a way to set up the custom schema name (I've read the docs) but I can't find a way to force the creation of the schema name during testing.

The error that I obtain is

django.db.utils.ProgrammingError: no schema has been selected to create in

When I see the created database , it has the schema public created by default.

I partially solved this issue and added to the search path the schema name public

'OPTIONS': {
    'options': '-c search_path=custom-schema-name,public'
},

but I'd like to create the test database with a custom schema name.

Does anybody knows how to set the testing schema name ?

3
  • 1
    same issue here - do you found a solution, you might want share? :) Commented Jun 15, 2016 at 15:45
  • 1
    No, I've never found the solution. I've left the job and I totally forgot about this question here. It has been a year since I posted this, and yours is the only answer so far. Commented Jun 16, 2016 at 19:41
  • 2
    I'm also looking for a solution to have django use non-public schema. There appears to be a thread about this here: code.djangoproject.com/ticket/22673#no1 to fix in django, alternatively I have found this possible solution: github.com/bernardopires/django-tenant-schemas Commented Nov 15, 2017 at 9:24

1 Answer 1

22

I ended up with writing a custom test runner to solve this problem (using django 1.9.x):

myapp/test/runner.py

from types import MethodType
from django.test.runner import DiscoverRunner
from django.db import connections

def prepare_database(self):
    self.connect()
    self.connection.cursor().execute("""
    CREATE SCHEMA foobar_schema AUTHORIZATION your_user;
    GRANT ALL ON SCHEMA foobar_schema TO your_user;
    """)


class PostgresSchemaTestRunner(DiscoverRunner):

    def setup_databases(self, **kwargs):
        for connection_name in connections:
            connection = connections[connection_name]
            connection.prepare_database = MethodType(prepare_database, connection)
        return super().setup_databases(**kwargs)

settings.py

TEST_RUNNER = 'myapp.test.runner.PostgresSchemaTestRunner'
Sign up to request clarification or add additional context in comments.

3 Comments

Thx, this saved me a lot of debugging. Still works like charm for postgres 11.x, django 2.2.x and psycopg 2.8.x.
How to disable foreign key constraint when truncate table? is that possible to use teardown_databases() in runner?
@KodeKite disable triggers beforehand

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.