https://github.com/asolovey/postgresql-audit-log
Simple, trigger-based audit log for PostgreSQL tables.
https://github.com/asolovey/postgresql-audit-log
dba plpgsql postgresql
Last synced: 19 days ago
JSON representation
Simple, trigger-based audit log for PostgreSQL tables.
- Host: GitHub
- URL: https://github.com/asolovey/postgresql-audit-log
- Owner: asolovey
- License: mit
- Created: 2017-05-18T05:04:27.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2023-04-24T06:02:17.000Z (almost 3 years ago)
- Last Synced: 2025-01-09T06:15:15.837Z (about 1 year ago)
- Topics: dba, plpgsql, postgresql
- Language: PLpgSQL
- Homepage:
- Size: 4.88 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Audit: audit.sql
Awesome Lists containing this project
README
# PostgreSQL Table Audit Log
Simple, trigger-based audit log for PostgreSQL tables.
Use `audit.sql` to add audit table `t_audit_log`, trigger function `f_audit`
and a few utility functions to your database. Create `AFTER INSERT OR UPDATE OR DELETE`
trigger on any table to keep the log for.
According to https://www.postgresql.org/docs/current/static/trigger-definition.html
If more than one trigger is defined for the same event on the same relation,
the triggers will be fired in alphabetical order by trigger name.
Therefore, use a name like "zz_audit" for audit trigger to ensure that it fires after other
triggers and records changes made by those triggers.
## Examples
### Simple case: default primary key, no excluded columns.
```sql
CREATE TABLE t_user (
id SERIAL PRIMARY KEY
,name TEXT NOT NULL
,email TEXT NOT NULL
,admin BOOLEAN NOT NULL DEFAULT FALSE
);
CREATE TRIGGER zz_audit AFTER INSERT OR UPDATE OR DELETE ON t_user
FOR EACH ROW EXECUTE PROCEDURE f_audit();
```
### No primary key (or it is unusable), so unique row identifier columns are listed explicitly
```sql
CREATE TABLE t_tag (
user_id INT CHECK( user_id != 0 )
,account_id INT CHECK( account_id != 0 )
,company_id INT CHECK( company_id != 0 )
,name TEXT NOT NULL
);
/* Cannot use nullable columns in primary key, so unique constraint instead: */
CREATE UNIQUE INDEX u_tag ON t_tag( COALESCE( user_id, 0 ), COALESCE( account_id, 0 ), COALESCE( company_id, 0 ) );
CREATE TRIGGER zz_audit AFTER INSERT OR UPDATE OR DELETE ON t_tag
FOR EACH ROW EXECUTE PROCEDURE f_audit('{"user_id", "account_id", "company_id"}');
```