@@ -17,7 +17,27 @@ create extension pg_audit;
17
17
CREATE USER super SUPERUSER;
18
18
ALTER ROLE super SET pg_audit.log = 'Role';
19
19
ALTER 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.
20
34
\connect - super;
35
+ select load_pg_audit();
36
+ load_pg_audit
37
+ ---------------
38
+
39
+ (1 row)
40
+
21
41
--
22
42
-- Create auditor role
23
43
CREATE ROLE auditor;
@@ -33,6 +53,12 @@ NOTICE: AUDIT: SESSION,4,1,ROLE,ALTER ROLE,,,ALTER ROLE user1 SET pg_audit.log_
33
53
--
34
54
-- Create, select, drop (select will not be audited)
35
55
\connect - user1
56
+ select load_pg_audit();
57
+ load_pg_audit
58
+ ---------------
59
+
60
+ (1 row)
61
+
36
62
CREATE TABLE public.test (id INT);
37
63
NOTICE: AUDIT: SESSION,1,1,DDL,CREATE TABLE,TABLE,public.test,CREATE TABLE public.test (id INT);,<not logged>
38
64
SELECT * FROM test;
@@ -45,6 +71,12 @@ NOTICE: AUDIT: SESSION,2,1,DDL,DROP TABLE,TABLE,public.test,DROP TABLE test;,<n
45
71
--
46
72
-- Create second test user
47
73
\connect - super
74
+ select load_pg_audit();
75
+ load_pg_audit
76
+ ---------------
77
+
78
+ (1 row)
79
+
48
80
CREATE USER user2;
49
81
NOTICE: AUDIT: SESSION,1,1,ROLE,CREATE ROLE,,,CREATE USER user2;,<not logged>
50
82
ALTER 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
58
90
ALTER ROLE user2 SET pg_audit.log_statement_once = ON;
59
91
NOTICE: AUDIT: SESSION,6,1,ROLE,ALTER ROLE,,,ALTER ROLE user2 SET pg_audit.log_statement_once = ON;,<not logged>
60
92
\connect - user2
93
+ select load_pg_audit();
94
+ load_pg_audit
95
+ ---------------
96
+
97
+ (1 row)
98
+
61
99
CREATE TABLE test2 (id INT);
62
100
GRANT SELECT ON TABLE public.test2 TO auditor;
63
101
--
@@ -204,9 +242,21 @@ WARNING: AUDIT: OBJECT,6,1,WRITE,INSERT,TABLE,public.test2,<previously logged>,
204
242
--
205
243
-- Change permissions of user 2 so that only object logging will be done
206
244
\connect - super
245
+ select load_pg_audit();
246
+ load_pg_audit
247
+ ---------------
248
+
249
+ (1 row)
250
+
207
251
alter role user2 set pg_audit.log = 'NONE';
208
252
NOTICE: AUDIT: SESSION,1,1,ROLE,ALTER ROLE,,,alter role user2 set pg_audit.log = 'NONE';,<not logged>
209
253
\connect - user2
254
+ select load_pg_audit();
255
+ load_pg_audit
256
+ ---------------
257
+
258
+ (1 row)
259
+
210
260
--
211
261
-- Create test4 and add permissions
212
262
CREATE TABLE test4
@@ -279,9 +329,21 @@ DROP TABLE test4;
279
329
--
280
330
-- Change permissions of user 1 so that session logging will be done
281
331
\connect - super
332
+ select load_pg_audit();
333
+ load_pg_audit
334
+ ---------------
335
+
336
+ (1 row)
337
+
282
338
alter role user1 set pg_audit.log = 'DDL, READ';
283
339
NOTICE: AUDIT: SESSION,1,1,ROLE,ALTER ROLE,,,"alter role user1 set pg_audit.log = 'DDL, READ';",<not logged>
284
340
\connect - user1
341
+ select load_pg_audit();
342
+ load_pg_audit
343
+ ---------------
344
+
345
+ (1 row)
346
+
285
347
--
286
348
-- Create table is session logged
287
349
CREATE TABLE public.account
@@ -315,11 +377,23 @@ INSERT INTO account (id, name, password, description)
315
377
--
316
378
-- Change permissions of user 1 so that only object logging will be done
317
379
\connect - super
380
+ select load_pg_audit();
381
+ load_pg_audit
382
+ ---------------
383
+
384
+ (1 row)
385
+
318
386
alter role user1 set pg_audit.log = 'none';
319
387
NOTICE: AUDIT: SESSION,1,1,ROLE,ALTER ROLE,,,alter role user1 set pg_audit.log = 'none';,<not logged>
320
388
alter role user1 set pg_audit.role = 'auditor';
321
389
NOTICE: AUDIT: SESSION,2,1,ROLE,ALTER ROLE,,,alter role user1 set pg_audit.role = 'auditor';,<not logged>
322
390
\connect - user1
391
+ select load_pg_audit();
392
+ load_pg_audit
393
+ ---------------
394
+
395
+ (1 row)
396
+
323
397
--
324
398
-- ROLE class not set, so auditor grants not logged
325
399
GRANT SELECT (password),
@@ -362,11 +436,23 @@ NOTICE: AUDIT: OBJECT,2,1,WRITE,UPDATE,TABLE,public.account,"UPDATE account
362
436
--
363
437
-- Change permissions of user 1 so that session relation logging will be done
364
438
\connect - super
439
+ select load_pg_audit();
440
+ load_pg_audit
441
+ ---------------
442
+
443
+ (1 row)
444
+
365
445
alter role user1 set pg_audit.log_relation = on;
366
446
NOTICE: AUDIT: SESSION,1,1,ROLE,ALTER ROLE,,,alter role user1 set pg_audit.log_relation = on;,<not logged>
367
447
alter role user1 set pg_audit.log = 'read, WRITE';
368
448
NOTICE: AUDIT: SESSION,2,1,ROLE,ALTER ROLE,,,"alter role user1 set pg_audit.log = 'read, WRITE';",<not logged>
369
449
\connect - user1
450
+ select load_pg_audit();
451
+ load_pg_audit
452
+ ---------------
453
+
454
+ (1 row)
455
+
370
456
--
371
457
-- Not logged
372
458
create table ACCOUNT_ROLE_MAP
@@ -461,6 +547,12 @@ NOTICE: AUDIT: SESSION,5,1,WRITE,UPDATE,TABLE,public.account,"UPDATE account
461
547
--
462
548
-- Change back to superuser to do exhaustive tests
463
549
\connect - super
550
+ select load_pg_audit();
551
+ load_pg_audit
552
+ ---------------
553
+
554
+ (1 row)
555
+
464
556
SET pg_audit.log = 'ALL';
465
557
NOTICE: AUDIT: SESSION,1,1,MISC,SET,,,SET pg_audit.log = 'ALL';,<not logged>
466
558
SET pg_audit.log_level = 'notice';
0 commit comments