Skip to content
This repository

Creating a trigger-based audit log for PostgreSQL

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .rspec
Octocat-spinner-32 .rvmrc
Octocat-spinner-32 Gemfile
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README.rdoc
Octocat-spinner-32 Rakefile
Octocat-spinner-32 init.rb
Octocat-spinner-32 pg_audit_log.gemspec
README.rdoc

pg_audit_log

DESCRIPTION

PostgreSQL only database-level audit logging of all databases changes using a completely transparent stored procedure and triggers. Comes with specs for your project and a rake task to generate the reverse SQL to undo changes logged}

All SQL INSERTs, UPDATEs, and DELETEs will be captured. Record columns that do not change do not generate an audit log entry.

Compatible with Rails 3.0.x, 3.1.x and 3.2.x

On a 2.93GHz i7 with postgresql 9.1 the audit log has an overhead of about 0.0035 seconds to each INSERT, UPDATE or DELETE

INSTALL

Enable plpgsql langauges in your postgresql instance

As a superuser in postgres make sure your database has plpgsql enabled:

CREATE OR REPLACE PROCEDURAL LANGUAGE plpgsql;

Rails 3

$ rails generate pg_audit_log:install

Installing the PostgreSQL function and triggers for your project

$ rake pg_audit_log:install

Using on your project

The PgAuditLog::Entry ActiveRecord model represents a single entry in the audit log table. Each entry represents a single change to a single field of a record in a table. So if you change 3 columns of a record, that will generate 3 corresponding PgAuditLog::Entry records.

You can see the SQL it injects on every query by running with LOG_AUDIT_SQL

Migrations

TODO

schema.rb and development_structure.sql

Since schema.rb cannot represent TRIGGERs or FUNCTIONs you will need to set your environment to generate SQL instead of ruby for your database schema and structure. In your application environment put the following:

config.active_record.schema_format = :sql

And you can generate this sql using:

$ rake db:structure:dump

Uninstalling

$ rake pg_audit_log:uninstall

REQUIREMENTS

  • ActiveRecord

LICENSE

MIT

Something went wrong with that request. Please try again.