Skip to content

pgAudit for row-level auditing on PostgreSQL

Use the pgAudit extension to log who did what to PostgreSQL — session and object audit, row-level filters, performance trade-offs, and downstream pipelines.

pgAudit is the supported way to produce SQL-level audit trails in Postgres — useful when you need to answer “who SELECTed from this table on Monday morning” for an auditor, a regulator, or a post-incident investigation. It writes structured records to the regular Postgres log, ready for shipping into SIEM. This article covers the install, the session vs object audit modes, the row-level audit pattern, and the operational cost it adds.

How to verify

Confirm the extension is loaded and check what’s being audited:

sudo apt list --installed 2>/dev/null | grep pgaudit
psql -c "SHOW shared_preload_libraries;"
psql -c "SHOW pgaudit.log;"
psql -c "SHOW pgaudit.log_catalog;"
psql -c "SELECT extversion FROM pg_extension WHERE extname='pgaudit';"
sudo grep AUDIT /var/log/postgresql/postgresql-*.log | head -20

A correctly configured pgAudit prefixes lines with AUDIT: and includes the session id, command, object name, and statement text.

What’s happening

pgAudit hooks into Postgres’ statement executor. It runs in two modes:

  • Session audit records statements run in audited sessions, classified by category (READ, WRITE, DDL, FUNCTION, MISC, etc.). You pick which categories to log via pgaudit.log = 'read, write, ddl'.
  • Object audit records access to specific objects you’ve marked. Define a role (typically audit), GRANT it on the tables/views you want logged, and pgAudit logs every access to those objects regardless of who runs the query.

Object audit is the right primitive when only a few tables need an audit trail (PII tables, payment records, employee directory). Session audit is the right primitive when you need to record everything an admin user does. They can be combined.

The records go to the regular Postgres log — pgAudit doesn’t ship to a separate sink. You front it with a log forwarder (Filebeat, Vector, Fluent Bit) and route to SIEM. The downstream is what most “audit log” requirements actually need.

The procedure

  1. Install the package and add it to preload:

    sudo apt install postgresql-16-pgaudit
    sudo -u postgres psql -c "ALTER SYSTEM SET shared_preload_libraries = 'pgaudit,pg_stat_statements';"
    sudo systemctl restart postgresql@16-main
    psql -d app -c "CREATE EXTENSION pgaudit;"
  2. For session audit, configure what to log:

    ALTER SYSTEM SET pgaudit.log = 'write, ddl, role';
    ALTER SYSTEM SET pgaudit.log_catalog = off;
    ALTER SYSTEM SET pgaudit.log_client = off;
    ALTER SYSTEM SET pgaudit.log_level = 'log';
    ALTER SYSTEM SET pgaudit.log_parameter = on;
    ALTER SYSTEM SET pgaudit.log_statement_once = on;
    SELECT pg_reload_conf();

    pgaudit.log_catalog = off skips queries against system catalogs — drops noise from monitoring queries and ORMs that introspect the schema. pgaudit.log_parameter = on includes bound parameter values; turn it off if your parameters could contain regulated PII you can’t log.

  3. For object audit, create an audit role and grant on what matters:

    CREATE ROLE audit;
    ALTER SYSTEM SET pgaudit.role = 'audit';
    SELECT pg_reload_conf();
    
    GRANT SELECT, INSERT, UPDATE, DELETE
      ON customers, customer_payments, employee_directory
      TO audit;

    Every read or write to those tables is now logged — regardless of which user did it. You can scope further: GRANT SELECT (ssn, dob) ON customers TO audit; audits only those columns.

  4. For row-level audit, use triggers that write to a dedicated audit_log table:

    CREATE TABLE audit_log (
      id          bigserial PRIMARY KEY,
      ts          timestamptz NOT NULL DEFAULT now(),
      usr         text NOT NULL DEFAULT current_user,
      action      text NOT NULL,
      table_name  text NOT NULL,
      row_pk      text NOT NULL,
      old_row     jsonb,
      new_row     jsonb
    );
    
    CREATE OR REPLACE FUNCTION audit_trigger() RETURNS trigger AS $$
    BEGIN
      INSERT INTO audit_log (action, table_name, row_pk, old_row, new_row)
      VALUES (TG_OP, TG_TABLE_NAME,
              COALESCE(NEW.id::text, OLD.id::text),
              to_jsonb(OLD), to_jsonb(NEW));
      RETURN COALESCE(NEW, OLD);
    END;
    $$ LANGUAGE plpgsql;
    
    CREATE TRIGGER customers_audit
      AFTER INSERT OR UPDATE OR DELETE ON customers
      FOR EACH ROW EXECUTE FUNCTION audit_trigger();

    This is heavier than pgAudit but gives you the row-by-row diff that compliance frameworks frequently want.

  5. Ship the logs. Filebeat/Vector reads the Postgres log file, filters AUDIT: lines (and/or the audit_log table via a pg_recvlogical channel), and forwards to SIEM. Sign the records or write to append-only storage if your framework requires tamper-evidence.

  6. Tune retention. Audit logs grow forever if you let them; we set 90 days hot and 7 years archived for regulated workloads, with pg_partman-rolled audit_log partitions detached monthly.

Common pitfalls

  • pgaudit.log = 'all' on a busy host can multiply log volume 10x. Start narrow and widen — 'write, ddl, role' is a good baseline.
  • Object audit silently does nothing if pgaudit.role isn’t set or if the role doesn’t exist. Verify by running a known query and grepping for AUDIT.
  • The trigger-based audit_log table can become the largest table in the database — partition it from day one.
  • Logging bound parameters can violate PCI or HIPAA — turn off pgaudit.log_parameter for production where regulated data is in literals.
  • Audit logs that live in the same database as the audited data don’t satisfy strict frameworks — ship to an external sink.

In the engagements we run, pgAudit + a trigger-based row audit + downstream shipping to SIEM is the pattern for any regulated workload (financial, healthcare, government). The operational cost is real — about 10-30% extra write volume on busy tables — and we plan IOPS accordingly. For deeper isolation by tenant, pair this with row-level security. For the operating model behind a regulated Postgres footprint, see managed operations.