PGAudit
log_statement = all
setting in postgresql.conf
.dnf
.postgres
. While PGAudit offers session and object auditing, this article will focus on session auditing.shared_preload_libraries
parameter in postgresql.conf
.Additionally, the log_line_prefix
parameter should be customized to add more valuable information.
Once the cluster restarts, the logs indicate that the PGAudit extension is successfully loaded. PGAudit is a cluster-wide extension and only needs to be installed once. The CREATE EXTENSION
command activates it.
PGAudit settings can be configured at various levels within PostgreSQL’s object hierarchy:
- Globally (
postgresql.conf
,ALTER SYSTEM ... SET
) - Database level (
ALTER DATABASE ... SET
) - Role level (
ALTER ROLE ... SET
).
Note: PostgreSQL’s role system presents two limitations:
- Settings are not inherited through role inheritance.
- SET ROLE does not affect a user’s PGAudit settings.
The most important parameter is pgaudit.log
, which enables audit logging based on the selected level of detail:
none
: No audit loggingread
:SELECT
andCOPY
when the source is a relation or a query.write
:INSERT
,UPDATE
,DELETE
,TRUNCATE
, andCOPY
when the destination is a relation.function
: Function calls and DO blocks.role
: Statements related to roles and privileges:GRANT
,REVOKE
,CREATE/ALTER/DROP ROLE
.ddl
: All DDL that is not included in the ROLE class.misc
: Miscellaneous commands, e.g.DISCARD
,FETCH
,CHECKPOINT
,VACUUM
,SET
.misc_set
: Miscellaneous SET commands, e.g. SET ROLE.all
: Include all of the above.
These values can be combined as a comma-separated list, and certain subsets can be excluded by prefacing the level with a -
sign.
In most scenarios, pgaudit.log
is the primary setting to configure. Let’s examine an example using the postgres user
.
Audit logging is configured to track all write
and ddl
SQL commands for the postgres
user session. Then, a table t1
is created, and rows are inserted.
The log output provides essential details such as:
- Command execution time
- User who executed the command
- Database in which the command was executed
- Application name
- Client connection (either Unix-domain socket or IP-based)
- Session-based audit log entry
- Command type
- Full SQL statement
The audit log format is detailed here.
This simple example demonstrates the advantages of using the PGAudit extension to monitor user activities. I recommend using the ALTER ROLE
command to explicitly set the pgaudit.log
level for a role. Of course, many other logging options are available to obtain a comprehensive view of the activities within a cluster.
That’s all for now. Stay tuned for the second entry in this blog series.

Dirk Aumüller
Dirk Aumueller arbeitet als Associate Partner für die Proventa AG. Sein technologischer Schwerpunkt liegt bei Datenbankarchitekturen mit PostgreSQL sowie Data Management Lösungen mit Pentaho. Zusätzlich zu seinen Datenbanktransformations-Projekteinsätzen ist er regelmäßig als PostgreSQL Trainer unterwegs und betreut Studenten bei ihren Abschlussarbeiten. Seine fachlichen Erfahrungen erstrecken sich über die Branchen Telco und Financial Services.