@@ -17,7 +17,27 @@ create extension pg_audit;
1717CREATE USER super SUPERUSER;
1818ALTER ROLE super SET pg_audit.log = 'Role';
1919ALTER ROLE super SET pg_audit.log_level = 'notice';
20+ CREATE FUNCTION load_pg_audit( )
21+ RETURNS VOID
22+ LANGUAGE plpgsql
23+ SECURITY DEFINER
24+ AS $function$
25+ declare
26+ begin
27+ LOAD 'pg_audit';
28+ end;
29+ $function$;
30+ -- After each connect, we need to load pg_audit, as if it was
31+ -- being loaded from shared_preload_libraries. Otherwise, the hooks
32+ -- won't be set up and called correctly, leading to lots of ugly
33+ -- errors.
2034\connect - super;
35+ select load_pg_audit();
36+ load_pg_audit
37+ ---------------
38+
39+ (1 row)
40+
2141--
2242-- Create auditor role
2343CREATE ROLE auditor;
@@ -33,6 +53,12 @@ NOTICE: AUDIT: SESSION,4,1,ROLE,ALTER ROLE,,,ALTER ROLE user1 SET pg_audit.log_
3353--
3454-- Create, select, drop (select will not be audited)
3555\connect - user1
56+ select load_pg_audit();
57+ load_pg_audit
58+ ---------------
59+
60+ (1 row)
61+
3662CREATE TABLE public.test (id INT);
3763NOTICE: AUDIT: SESSION,1,1,DDL,CREATE TABLE,TABLE,public.test,CREATE TABLE public.test (id INT);,<not logged>
3864SELECT * FROM test;
@@ -45,6 +71,12 @@ NOTICE: AUDIT: SESSION,2,1,DDL,DROP TABLE,TABLE,public.test,DROP TABLE test;,<n
4571--
4672-- Create second test user
4773\connect - super
74+ select load_pg_audit();
75+ load_pg_audit
76+ ---------------
77+
78+ (1 row)
79+
4880CREATE USER user2;
4981NOTICE: AUDIT: SESSION,1,1,ROLE,CREATE ROLE,,,CREATE USER user2;,<not logged>
5082ALTER ROLE user2 SET pg_audit.log = 'Read, writE';
@@ -58,6 +90,12 @@ NOTICE: AUDIT: SESSION,5,1,ROLE,ALTER ROLE,,,ALTER ROLE user2 SET pg_audit.role
5890ALTER ROLE user2 SET pg_audit.log_statement_once = ON;
5991NOTICE: AUDIT: SESSION,6,1,ROLE,ALTER ROLE,,,ALTER ROLE user2 SET pg_audit.log_statement_once = ON;,<not logged>
6092\connect - user2
93+ select load_pg_audit();
94+ load_pg_audit
95+ ---------------
96+
97+ (1 row)
98+
6199CREATE TABLE test2 (id INT);
62100GRANT SELECT ON TABLE public.test2 TO auditor;
63101--
@@ -204,9 +242,21 @@ WARNING: AUDIT: OBJECT,6,1,WRITE,INSERT,TABLE,public.test2,<previously logged>,
204242--
205243-- Change permissions of user 2 so that only object logging will be done
206244\connect - super
245+ select load_pg_audit();
246+ load_pg_audit
247+ ---------------
248+
249+ (1 row)
250+
207251alter role user2 set pg_audit.log = 'NONE';
208252NOTICE: AUDIT: SESSION,1,1,ROLE,ALTER ROLE,,,alter role user2 set pg_audit.log = 'NONE';,<not logged>
209253\connect - user2
254+ select load_pg_audit();
255+ load_pg_audit
256+ ---------------
257+
258+ (1 row)
259+
210260--
211261-- Create test4 and add permissions
212262CREATE TABLE test4
@@ -279,9 +329,21 @@ DROP TABLE test4;
279329--
280330-- Change permissions of user 1 so that session logging will be done
281331\connect - super
332+ select load_pg_audit();
333+ load_pg_audit
334+ ---------------
335+
336+ (1 row)
337+
282338alter role user1 set pg_audit.log = 'DDL, READ';
283339NOTICE: AUDIT: SESSION,1,1,ROLE,ALTER ROLE,,,"alter role user1 set pg_audit.log = 'DDL, READ';",<not logged>
284340\connect - user1
341+ select load_pg_audit();
342+ load_pg_audit
343+ ---------------
344+
345+ (1 row)
346+
285347--
286348-- Create table is session logged
287349CREATE TABLE public.account
@@ -315,11 +377,23 @@ INSERT INTO account (id, name, password, description)
315377--
316378-- Change permissions of user 1 so that only object logging will be done
317379\connect - super
380+ select load_pg_audit();
381+ load_pg_audit
382+ ---------------
383+
384+ (1 row)
385+
318386alter role user1 set pg_audit.log = 'none';
319387NOTICE: AUDIT: SESSION,1,1,ROLE,ALTER ROLE,,,alter role user1 set pg_audit.log = 'none';,<not logged>
320388alter role user1 set pg_audit.role = 'auditor';
321389NOTICE: AUDIT: SESSION,2,1,ROLE,ALTER ROLE,,,alter role user1 set pg_audit.role = 'auditor';,<not logged>
322390\connect - user1
391+ select load_pg_audit();
392+ load_pg_audit
393+ ---------------
394+
395+ (1 row)
396+
323397--
324398-- ROLE class not set, so auditor grants not logged
325399GRANT SELECT (password),
@@ -362,11 +436,23 @@ NOTICE: AUDIT: OBJECT,2,1,WRITE,UPDATE,TABLE,public.account,"UPDATE account
362436--
363437-- Change permissions of user 1 so that session relation logging will be done
364438\connect - super
439+ select load_pg_audit();
440+ load_pg_audit
441+ ---------------
442+
443+ (1 row)
444+
365445alter role user1 set pg_audit.log_relation = on;
366446NOTICE: AUDIT: SESSION,1,1,ROLE,ALTER ROLE,,,alter role user1 set pg_audit.log_relation = on;,<not logged>
367447alter role user1 set pg_audit.log = 'read, WRITE';
368448NOTICE: AUDIT: SESSION,2,1,ROLE,ALTER ROLE,,,"alter role user1 set pg_audit.log = 'read, WRITE';",<not logged>
369449\connect - user1
450+ select load_pg_audit();
451+ load_pg_audit
452+ ---------------
453+
454+ (1 row)
455+
370456--
371457-- Not logged
372458create table ACCOUNT_ROLE_MAP
@@ -461,6 +547,12 @@ NOTICE: AUDIT: SESSION,5,1,WRITE,UPDATE,TABLE,public.account,"UPDATE account
461547--
462548-- Change back to superuser to do exhaustive tests
463549\connect - super
550+ select load_pg_audit();
551+ load_pg_audit
552+ ---------------
553+
554+ (1 row)
555+
464556SET pg_audit.log = 'ALL';
465557NOTICE: AUDIT: SESSION,1,1,MISC,SET,,,SET pg_audit.log = 'ALL';,<not logged>
466558SET pg_audit.log_level = 'notice';
0 commit comments