Git Product home page Git Product logo

audit-trigger's Introduction

A simple, customisable table audit system for PostgreSQL implemented using triggers.

This is based off https://github.com/2ndQuadrant/audit-trigger with the following changes

  1. The row data is stored in jsonb.
  2. Logs user information from hasura's graphql-engine (accessible by current_setting('hasura.user')).

Installation

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

Setting up triggers

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

Available options

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?
ignored_cols Columns to exclude from update diffs, ignore updates that change only ignored cols.

Examples

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);

Log changes for every row, log the sql statement, but don't log the data of the columns email and phone_number

select audit.audit_table('author', true, true, '{email,phone_number}');

audit-trigger's People

Contributors

3nids avatar adtc avatar asenyshyn avatar jeffreyhmcclelland avatar memoz avatar paulovieira avatar ringerc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

audit-trigger's Issues

Fails with Postgres 12

{
    "internal": [
        {
            "definition": {
                "schema": "audit",
                "name": "if_modified_func"
            },
            "reason": "in function \"audit.if_modified_func\": the function \"audit.if_modified_func\" cannot be tracked for the following reasons:\n  • the function does not return a \"COMPOSITE\" type\n  • the function does not return a SETOF\n  • the function does not return a SETOF table\n  • function of type \"VOLATILE\" is not supported now\n",
            "type": "function"
        }
    ],
    "path": "$.args[2].args",
    "error": "in function \"audit.if_modified_func\": the function \"audit.if_modified_func\" cannot be tracked for the following reasons:\n  • the function does not return a \"COMPOSITE\" type\n  • the function does not return a SETOF\n  • the function does not return a SETOF table\n  • function of type \"VOLATILE\" is not supported now\n",
    "code": "constraint-violation"
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.