All Products
Search
Document Center

ApsaraDB RDS:Generate audit logs with pgAudit

Last Updated:Jun 02, 2026

pgAudit provides session-level and object-level audit logging for ApsaraDB RDS for PostgreSQL, helping you meet public service, financial, and ISO compliance requirements. Use audit logs to analyze faults, track operations, and get information about data queries on your RDS instance.

Prerequisites

Make sure that you have:

How pgAudit works

pgAudit intercepts SQL statements and logs them through the standard PostgreSQL logging facility with structured metadata such as statement class, command tag, object type, and object name.

pgAudit supports two logging modes:

  • Session audit logging: Logs all statements of specified classes during a session. Configure which classes to log with the pgaudit.log parameter.

  • Object audit logging: Logs statements that affect specific database objects. Grant permissions to a designated audit role specified by the pgaudit.role parameter. This provides finer granularity than session audit logging.

Usage notes

  • pgAudit can generate a large volume of log data. The amount varies based on the extension configuration. Evaluate which objects and statement classes to audit before enabling auditing to avoid exhausting your instance storage.

  • After you rename an object, subsequent audit log entries use the new name.

Enable the pgAudit extension

Connect to your RDS instance and run:

CREATE EXTENSION pgaudit;

Configure session audit logging

Session audit logging records all statements of specified classes during a database session. Set pgaudit.log to specify which statement classes to audit.

Statement classes

Class Description
READ SELECT and COPY statements when the source is a relation or query.
WRITE INSERT, UPDATE, DELETE, TRUNCATE, and COPY statements when the destination is a relation.
FUNCTION Function calls and DO blocks.
ROLE Statements related to roles and privileges, such as GRANT, REVOKE, CREATE ROLE, ALTER ROLE, and DROP ROLE.
DDL All DDL statements that are not covered by the ROLE class.
MISC Miscellaneous commands such as DISCARD, FETCH, CHECKPOINT, and VACUUM.
MISC_SET Miscellaneous SET commands such as SET ROLE.
ALL Includes all statement classes.

Set the audit log class

Set pgaudit.log in the RDS console. Modify the parameters of an ApsaraDB RDS for PostgreSQL instance.

Specify multiple classes with a comma-separated list:

  • Log all write and DDL operations: set pgaudit.log to write, ddl.

  • Log all statement classes: set pgaudit.log to all.

  • Disable session audit logging: set pgaudit.log to none.

pgaudit.log requires superuser privileges. On ApsaraDB RDS for PostgreSQL, configure this parameter through the RDS console instead of a SQL SET statement.

Example: Log DDL and write operations

After setting pgaudit.log to ddl, write in the RDS console, run these SQL statements:

-- Create a table
CREATE TABLE employees (id int, name text, department text);

-- Insert a row
INSERT INTO employees VALUES (1, 'Alice', 'Engineering');

These statements produce the following audit log entries, visible in the error logs of your RDS instance:

AUDIT: SESSION,1,1,DDL,CREATE TABLE,TABLE,public.employees,"CREATE TABLE employees (id int, name text, department text);",<not logged>
AUDIT: SESSION,2,1,WRITE,INSERT,,,"INSERT INTO employees VALUES (1, 'Alice', 'Engineering');",<not logged>

Configure object audit logging

Object audit logging records statements that affect specific database objects, using the PostgreSQL permission system to determine which operations to log.

To configure object audit logging:

  1. Set pgaudit.role to the name of a designated audit role in the RDS console. For example, set pgaudit.role to auditor. Modify the parameters of an ApsaraDB RDS for PostgreSQL instance. > Note: pgaudit.role requires superuser privileges. On ApsaraDB RDS for PostgreSQL, configure this parameter through the RDS console instead of a SQL SET statement.

  2. Create the audit role:

       CREATE ROLE auditor;
  3. Grant permissions to the audit role on the objects to audit. For example, to audit SELECT and DELETE operations on a table named orders:

       GRANT SELECT, DELETE ON orders TO auditor;

After configuration, pgAudit logs any SELECT or DELETE statement on the orders table, regardless of which user runs it.

Example: Log read operations on a specific table

After setting pgaudit.role to auditor in the RDS console, run:

-- Grant SELECT on the target table to the audit role
GRANT SELECT ON employees TO auditor;

-- Any SELECT on the employees table is now logged
SELECT * FROM employees WHERE department = 'Engineering';

The SELECT statement produces this audit log entry:

AUDIT: OBJECT,1,1,READ,SELECT,TABLE,public.employees,"SELECT * FROM employees WHERE department = 'Engineering';",<not logged>

Session audit logging parameters

The following parameters control session audit logging. Configure them in the RDS console. Modify the parameters of an ApsaraDB RDS for PostgreSQL instance.

Parameter Type Default Description
pgaudit.log String none Statement classes to log, comma-separated. Valid values: READ, WRITE, FUNCTION, ROLE, DDL, MISC, MISC_SET, ALL, NONE.
pgaudit.log_catalog Boolean on Logs statements where all relations are in pg_catalog. Disable to reduce noise from tools such as psql and PgAdmin.
pgaudit.log_client Boolean off Sends log messages to the client process in addition to the server log. Available on PostgreSQL 11 and later.
pgaudit.log_level String log Log level for audit entries. Valid values: DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1, INFO, NOTICE, WARNING, LOG.
pgaudit.log_parameter Boolean off Includes parameters passed with the statement in the audit log, appended after the statement text in CSV format.
pgaudit.log_relation Boolean off Creates separate log entries for each relation (TABLE, VIEW, etc.) referenced in a SELECT or DML statement. Useful for object audit logging without using the pgAudit role.
pgaudit.log_rows Boolean off Includes the row count retrieved or affected by a statement. Available on PostgreSQL 12 and later.
pgaudit.log_statement Boolean on Includes the statement text and parameters in the log entry. Depending on requirements, the statement text may need to be excluded from the audit log.
pgaudit.log_statement_once Boolean off Logs statement text and parameters only with the first log entry for each statement/sub-statement combination. Reduces verbosity but may complicate correlation.
pgaudit.role String None Master role for object audit logging. Multiple audit roles can be defined by granting them to the master role.
Parameters such as pgaudit.log_client, pgaudit.log_rows, and pgaudit.log_statement may not appear in the RDS console. They use default values. To check their current values, run SHOW <parameter_name>; after connecting to your RDS instance.

Audit log output format

pgAudit generates log entries in the following format, visible in the error logs of your RDS instance.

AUDIT: SESSION,1,1,DDL,CREATE TABLE,TABLE,public.orders,"CREATE TABLE orders (id int, name text);",<not logged>

Each entry contains these comma-separated fields:

Field Description
AUDIT_TYPE SESSION or OBJECT, indicating the type of audit logging.
STATEMENT_ID Unique statement identifier. Increments for each statement in a session.
SUBSTATEMENT_ID Unique sub-statement identifier. Increments for each sub-statement within a statement.
CLASS Statement class, such as DDL, READ, or WRITE.
COMMAND Command tag, such as CREATE TABLE or SELECT.
OBJECT_TYPE Type of the object, such as TABLE, INDEX, or FUNCTION.
OBJECT_NAME Fully qualified name of the object, such as public.orders.
STATEMENT SQL statement that was run.
PARAMETER Statement parameters in CSV format. Displays <not logged> when pgaudit.log_parameter is set to off.

Best practices

  • Start with minimal logging: Begin with specific classes such as WRITE and DDL rather than ALL. Expand as needed.

  • Monitor storage usage: Audit logging generates additional data. Regularly check storage usage to prevent exhaustion.

  • Use object audit logging for fine-grained control: To audit specific tables or views, use object audit logging instead of session audit logging to reduce log volume.

  • Exclude catalog queries: Set pgaudit.log_catalog to on only if you need to audit catalog queries. Setting it to off reduces noise from administrative tools.

Disable the pgAudit extension

To stop generating audit logs and remove the extension, run:

DROP EXTENSION pgaudit;

References

For pgAudit documentation, see the pgAudit GitHub repository.