Skip to content

CuriousLearner/django-postgres-anonymizer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

29 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Django PostgreSQL Anonymizer

Tests Coverage License Downloads Python Maintained PyPI version PRs Welcome Python versions Django versions GitHub stars Security: bandit Code style: ruff

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

🎯 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]
Loading

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

🚧 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.

πŸ“„ 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

About

Django integration for PostgreSQL Anonymizer extension

Resources

License

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published