Skip to content

Instantly share code, notes, and snippets.

@AndreasDickow
Last active December 5, 2019 09:58
Show Gist options
  • Save AndreasDickow/f277191dd948b5f76260af027f843acf to your computer and use it in GitHub Desktop.
Save AndreasDickow/f277191dd948b5f76260af027f843acf to your computer and use it in GitHub Desktop.
Set default schema for Postgresql DB in django
# with this trick it is possible to install django on schema access restricted databases
# within project root create folders like
# -app
# -pg
# |
# - __init__.py
# - base.py
from django.db.backends.postgresql_psycopg2.base import DatabaseWrapper
class DatabaseWrapper(DatabaseWrapper):
def __init__(self, *args, **kwargs):
super(DatabaseWrapper, self).__init__(*args, **kwargs)
def _cursor(self, *args, **kwargs):
cursor = super(DatabaseWrapper, self)._cursor(*args, **kwargs)
if not kwargs:
cursor.execute('SET search_path = <your-db-schema>')
return cursor
DATABASES = {
'default': {
'ENGINE': 'app.pg',
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment