├── .gitignore ├── LICENSE ├── README.rst ├── django_postgres_readonly ├── __init__.py └── base.py └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | dist 3 | django_postgres_readonly.egg-info 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016, Opbeat Inc. 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, 5 | are permitted provided that the following conditions are met: 6 | 7 | 1. Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | 2. Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 12 | 13 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 14 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 17 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | django-postgres-readonly 2 | ======================== 3 | 4 | A readonly database backend for Django and PostgreSQL. It does this by setting 5 | the connection to "readonly". 6 | 7 | 8 | Usage 9 | ----- 10 | 11 | To configure a read-only database connection, add an entry to your ``DATABASES`` 12 | setting that uses `django_postgres_readonly` as the engine:: 13 | 14 | DATABASES = { 15 | 'default': { 16 | 'NAME': 'my_django_db', 17 | 'ENGINE': 'django.db.backends.postgresql', 18 | 'USER': 'my_db_user', 19 | 'PASSWORD': 'my_password' 20 | }, 21 | 'readonly': { 22 | 'NAME': 'my_django_db', 23 | 'ENGINE': 'django_postgres_readonly', 24 | 'USER': 'my_db_user', 25 | 'PASSWORD': 'my_password' 26 | } 27 | } 28 | 29 | 30 | Caveats 31 | ------- 32 | 33 | This backend is not meant to provide security against willful bad actors. Its 34 | main reason of existence is to protect you against your own mistakes. 35 | 36 | If you need secure way for read-only connections, you should probably 37 | look into creating a separate user and grant it ``SELECT`` rights only. 38 | -------------------------------------------------------------------------------- /django_postgres_readonly/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/opbeat/django-postgres-readonly/a6bc00f020f9f033f03fbe8282aea06ea11931bb/django_postgres_readonly/__init__.py -------------------------------------------------------------------------------- /django_postgres_readonly/base.py: -------------------------------------------------------------------------------- 1 | from django.db.backends.postgresql_psycopg2 import base 2 | 3 | 4 | class DatabaseWrapper(base.DatabaseWrapper): 5 | def get_new_connection(self, conn_params): 6 | conn = super(DatabaseWrapper, self).get_new_connection(conn_params) 7 | conn.set_session(readonly=True) 8 | return conn 9 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup, find_packages 2 | from codecs import open 3 | from os import path 4 | 5 | here = path.abspath(path.dirname(__file__)) 6 | 7 | with open(path.join(here, 'README.rst'), encoding='utf-8') as f: 8 | long_description = f.read() 9 | 10 | setup( 11 | name='django-postgres-readonly', 12 | 13 | version='1.0.0', 14 | 15 | description='A backend for Django and Postgres that sets up a readonly connection', 16 | long_description=long_description, 17 | 18 | url='https://github.com/opbeat/django-postgres-readonly', 19 | 20 | author='Opbeat', 21 | author_email='benjamin@opbeat.com', 22 | 23 | license='BSD', 24 | 25 | classifiers=[ 26 | 'Development Status :: 5 - Production/Stable', 27 | 28 | 'License :: OSI Approved :: BSD License', 29 | 30 | 'Framework :: Django', 31 | 32 | 'Programming Language :: Python :: 2', 33 | 'Programming Language :: Python :: 2.7', 34 | 'Programming Language :: Python :: 3', 35 | 'Programming Language :: Python :: 3.3', 36 | 'Programming Language :: Python :: 3.4', 37 | 'Programming Language :: Python :: 3.5', 38 | ], 39 | 40 | keywords='django postgres', 41 | packages=find_packages(exclude=['tests']), 42 | 43 | install_requires=['Django', 'psycopg2'], 44 | 45 | extras_require={ 46 | 'test': ['coverage'], 47 | }, 48 | ) --------------------------------------------------------------------------------