Skip to main content

Postgres audit triggers for Django

Project description

Postgres audit database via triggers
====================================

This app sets up a postgres audit database via triggers.
See https://wiki.postgresql.org/wiki/Audit_trigger_91plus
and https://github.com/2ndQuadrant/audit-trigger/
for more information.


Installation
============
`pip install postgres_audit_triggers`


Usage
=====

- Add the `postgres_audit_triggers` app to `INSTALLED_APPS` *before* any apps that will be audited:

```
# settings.py
INSTALLED_APPS = {
'django.contrib.postgres',
'postgres_audit_triggers',
...
}
```

- Install the `postgres_audit_triggers` middleware:

```
# settings.py
MIDDLEWARE = [
...
'postgres_audit_triggers.middleware.AuditMiddleware',
]
```

This middleware will add metadata to the audit row. To send metadata, the client must send a
`POSTGRES_AUDIT_TRIGGERS_META` header in the request to your Django view. The data within
that header must be JSON serializable to a python dictionary.

- Run migrations: `python manage.py migrate postgres_audit_triggers`

- Add `audit_trigger = True` to the Model Meta options of the models that will be audited:

```
# models.py
class MyAuditedModel(models.Model):
...
class Meta:
audit_trigger = True
...
```

- Make migrations: `python manage.py makemigrations`
- Run migrations: `python manage.py migrate`

Triggers introduce performance overhead. In certain cases, you may need to disable triggers while
performing bulk operations. To turn off all triggers, a decorator is provided:

```
from postgres_audit_triggers.decorators import disable_triggers


@disable_triggers
def foo():
# auditing will not be triggered on any database operations performed here
Bar.objects.bulk_create(items)
```

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

postgres-audit-triggers-1.0.0.tar.gz (11.2 kB view details)

Uploaded Source

File details

Details for the file postgres-audit-triggers-1.0.0.tar.gz.

File metadata

File hashes

Hashes for postgres-audit-triggers-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b21a3f0e509ae8254b528bc8662bd380a5b3111bdf7a4e6a1d0060ec120ee7a8
MD5 9d54fc2d5e51a677d2ebad74711940a3
BLAKE2b-256 fc7caeba73bc524c902ab6c452d39232ab098347906c16e267f7848e21010813

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page