Django integration for PostgreSQL Anonymizer extension
Project description
Django PostgreSQL Anonymizer
Professional-grade data anonymization for Django applications using PostgreSQL's native anonymizer extension.
Anonymize sensitive data in development, testing, and analytics environments. Django PostgreSQL Anonymizer provides seamless integration with the PostgreSQL Anonymizer extension, enabling you to anonymize data at the database level with zero performance overhead. Ideal for development workflows, safe data sharing, and reducing privacy risks.
โ ๏ธ Beta Status: This package is currently in beta. Core features are stable, but APIs may still change before 1.0. Test thoroughly before production use.
โจ Why Choose This Library?
- ๐ Zero Performance Overhead - Database-level anonymization means no application slowdown
- ๐ Role-Based Access - Database role switching with audit logging
- ๐ฏ Effortless Django Integration - Middleware, context managers, decorators, and admin interface
- ๐ Industry Presets - Pre-built anonymization rules for healthcare, finance, e-commerce domains
- โก Real-time Data Switching - Toggle between real and anonymized data instantly, no downtime
- ๐ก๏ธ Security-First Design - Built-in SQL injection prevention and function validation
- ๐ Multiple Access Patterns - Choose automatic (middleware), manual (context managers), or decorator-based approaches
- ๐งช Well-Tested - Comprehensive test coverage with type safety
๐ Quick Start
Install:
pip install django-postgres-anonymizer
Configure:
# settings.py
INSTALLED_APPS = ['django_postgres_anon']
POSTGRES_ANON = {
'ENABLED': True,
'MASKED_GROUPS': ['analysts', 'qa_team'],
}
Initialize:
python manage.py migrate
python manage.py anon_init
Use:
# Automatic (middleware) - users in masked groups see anonymized data
User.objects.all() # Automatically anonymized for analysts/qa_team
# Manual (context manager)
from django_postgres_anon.context_managers import anonymized_data
with anonymized_data():
users = User.objects.all() # Anonymized data
# Decorator
from django_postgres_anon.decorators import use_anonymized_data
@use_anonymized_data
def sensitive_report(request):
return render(request, 'report.html', {
'users': User.objects.all() # Automatically anonymized
})
๐ Documentation
๐ Full Documentation
- Installation Guide - PostgreSQL setup and package installation
- Quick Start - Get running in 10 minutes
- Configuration - 12-factor compliant settings
- User Guides - Middleware, context managers, decorators
- Examples - Real-world use cases
- API Reference - Complete API documentation
๐ฏ Real-World Use Cases
Development & Testing
- ๐ง Safe Development - Use realistic production-like data without privacy risks
- ๐งช QA & Testing - Test with anonymized datasets that mirror production
- ๐ Bug Reproduction - Debug with real data patterns safely
Data Sharing & Analytics
- ๐ Business Intelligence - Share anonymized data with internal analysts
- ๐ค Third-party Integration - Safely export data to vendors and partners
- ๐ Training & Demos - Create realistic demos without exposing sensitive data
Privacy & Compliance
- ๐ Privacy by Design - Reduce risk of data exposure in non-production environments
- ๐ Compliance Support - Tool to help with data protection requirements
- ๐ก๏ธ Data Minimization - Limit exposure of sensitive data to development teams
๐ค Why Not Just...?
"Why not use fake data generators like Faker?" Application-level anonymization is slow and risky. Database-level anonymization is instant, secure, and happens before data ever reaches your application code.
"Why not just delete sensitive data?" You lose referential integrity and realistic data patterns needed for proper testing and debugging. Anonymization preserves data structure and relationships.
"Why not use separate test fixtures?" Fixtures don't reflect real-world edge cases, data distributions, or production issues. Anonymized production data gives you the real picture without the risk.
"Why not query-by-query anonymization in views?" Manual anonymization is error-prone and easy to forget. This library provides automatic, middleware-based anonymization that just works.
๐๏ธ Architecture
graph LR
A[Django App] --> B[Middleware/Context Manager]
B --> C[PostgreSQL Role Switch]
C --> D[Anonymized Views]
D --> E[Masked Data]
Core Components:
- Middleware - Automatic anonymization for user groups
- Context Managers - Manual anonymized data access
- Decorators - View-level anonymization
- Admin Interface - Rule management and monitoring
- Management Commands - CLI operations and automation
๐ก๏ธ Security Features
- SQL Injection Prevention - Function validation and sanitization
- Role-based Access Control - Database-level security isolation
- Audit Logging - Complete operation tracking
- Zero Data Leakage - Original data never leaves the database
- Validated Functions - Whitelist-based anonymization function validation
๐ฆ Requirements
- Python 3.8+
- Django 3.2+
- PostgreSQL 12+ with Anonymizer extension
๐ง Cloud Platform Support
| Platform | Support | Notes |
|---|---|---|
| Self-hosted PostgreSQL | โ Full | Recommended for production |
| Docker | โ Full | Pre-built images available |
| AWS RDS | โ Limited | Extension requires superuser |
| Azure PostgreSQL | โ Limited | Extension not available |
| Google Cloud SQL | โ Limited | Extension not available |
| Heroku Postgres | โ Limited | Extension not available |
Note: Managed cloud services don't support the PostgreSQL Anonymizer extension. Use self-hosted PostgreSQL or Docker for full functionality.
๐งช Example Project
git clone https://github.com/CuriousLearner/django-postgres-anonymizer.git
cd django-postgres-anonymizer/example_project
pip install -r requirements.txt
python manage.py migrate
python manage.py anon_init
python manage.py runserver
Visit http://localhost:8000 to explore the interactive demo.
๐ค Contributing
We welcome contributions! See our Contributing Guide for details.
- ๐ Bug Reports - GitHub Issues
- ๐ก Feature Requests - GitHub Discussions
- ๐ Security Issues - sanyam@sanyamkhurana.com
๐ License
BSD-3-Clause License. See LICENSE for details.
๐ Acknowledgments
- PostgreSQL Anonymizer - Core anonymization engine
- Django community - Framework excellence
- Contributors and early adopters - Valuable feedback
โญ Star this project if you find it useful!
๐ Read the Docs | ๐ Report Issues | ๐ฌ Discussions
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_postgres_anonymizer-0.1.0b1.tar.gz.
File metadata
- Download URL: django_postgres_anonymizer-0.1.0b1.tar.gz
- Upload date:
- Size: 37.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
647b87b495bca52adb54f0df66869f271d7320a77b961b8551d090ddaa110db6
|
|
| MD5 |
883a25f927e5149bb6c505d14546ce7b
|
|
| BLAKE2b-256 |
80242e0ac1b8e57eb63dec122e42211935e6d3d1c11336d2bb2264639ad7bb83
|
File details
Details for the file django_postgres_anonymizer-0.1.0b1-py3-none-any.whl.
File metadata
- Download URL: django_postgres_anonymizer-0.1.0b1-py3-none-any.whl
- Upload date:
- Size: 35.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
411d605346bf2efb52b9c1029e843746b56689192a0306f1fc163b8f0177305a
|
|
| MD5 |
ce065cf8207d73099946505d52d61b43
|
|
| BLAKE2b-256 |
ba551401e4d331e56192df5205b25ddedf5ced9652371ad333559503869623e5
|