A simple, customisable table audit system for PostgreSQL implemented using triggers.
This is modified from https://github.com/hasura/audit-trigger, remove unsupported operator that introduced in Postgres 10 in order to running in Postgres 9.
This is based off https://github.com/2ndQuadrant/audit-trigger with the following changes
- The row data is stored in
jsonb
. - Logs user information from hasura's graphql-engine (accessible by
current_setting('hasura.user')
).
Load audit.sql
into the database where you want to set up auditing. You can do this via psql or any other tool that lets you execute sql on the database.
psql -h <db-host> -p <db-port> -U <db-user> -d <db> -f audit.sql --single-transaction
Run the following sql to setup audit on a table
select audit.audit_table('author');
For a table in a different schema name as follows:
select audit.audit_table('shipping.delivery');
This sets up triggers on the given table which logs any change (insert/update/delete) into the table audit.logged_actions
.
select * from audit.logged_actions
The function audit.audit_table
takes the following arguments:
argument | description |
---|---|
target_table |
Table name, schema qualified if not on search_path |
audit_rows |
Record each row change, or only audit at a statement level |
audit_query_text |
Record the text of the client query that triggered the audit event? |
Do not log changes for every row
select audit.audit_table('author', false);
Log changes for every row but don't log the sql statement
select audit.audit_table('author', true, false);