Skip to content

Collect pgaudit Logs

Turn on pgaudit on a Managed PostgreSQL service, choose what it audits, and read the audit records it produces.

Where the Audit Records Go

pgaudit writes one record per audited statement. On Managed PostgreSQL these records are not part of the stream that exo dbaas logs returns: the command shows the server log, but never the AUDIT: lines. They reach a logs integration to Managed OpenSearch, where each record is a document of its own.

Set up the logs integration before you rely on auditing. Without it, the records are written but you have no way to read them.

Enable pgaudit

pgaudit is configured in the pgaudit-settings object of the service. exo dbaas update has no flag for it, so send the object with the raw API command:

echo '{"pgaudit-settings":{"feature_enabled":true,"log":["ddl","role"]}}' | \
  exo api update-dbaas-service-pg SERVICE_NAME -z ZONE

Parameters:

  • SERVICE_NAME: name of the Managed PostgreSQL service.
  • ZONE: zone the service runs in, for example ch-gva-2.

The service reloads its configuration and audits new statements within seconds, in every database, without a restart. Read the configuration back with:

exo api get-dbaas-service-pg SERVICE_NAME -z ZONE -q '"pgaudit-settings"'

Note

On a service where pgaudit has never been configured, the pgaudit-settings field is absent and this query fails. Run it after the first update.

Each update is merged into the stored object: keys you leave out keep their value. Setting feature_enabled back to false stops auditing (pgaudit.log then reads none) and keeps the other keys, so turning it on again restores the same rules.

The pgaudit Extension

Session auditing works without CREATE EXTENSION pgaudit: the extension does not appear in pg_extension after you enable the feature. avnadmin can create it in a database, and doing so fills the object type and object name fields of DDL records, which stay empty otherwise:

CREATE EXTENSION pgaudit;

DROP EXTENSION pgaudit removes it again without stopping auditing.

Choose What to Audit

Statement Classes

The log array takes the statement classes defined by pgaudit:

ClassStatements audited
readSELECT and COPY from a relation
writeINSERT, UPDATE, DELETE, TRUNCATE and COPY into a relation
functionFunction calls and DO blocks
roleGRANT, REVOKE, CREATE ROLE, ALTER ROLE and DROP ROLE
ddlDDL not covered by role
miscOther commands, for example SET, VACUUM or DISCARD
misc_setSET ROLE and similar commands
allEvery class above

["ddl","role"] records schema and privilege changes, including the grants that CREATE EXTENSION issues. The password in a CREATE USER ... WITH PASSWORD statement is replaced with <REDACTED> in the record.

Caution

read, write or all produce one record for nearly every statement the application sends. Enable them for a defined period, or only for the roles that need them, as shown in Audit One Role or One Database.

Settings

All keys of pgaudit-settings, with the value in force when a key is not set:

KeyTypeWhen not setEffect
feature_enabledbooleanfalseTurns auditing on or off for the whole service
logarraynoneStatement classes audited by session auditing
log_catalogbooleantrueAlso audit statements that only touch pg_catalog, such as the queries psql runs for \d
log_parameterbooleanfalseAdd the bind parameters of the statement to the record
log_parameter_max_sizeinteger0Replace parameters longer than this many bytes with <long param suppressed>; 0 keeps them all
log_relationbooleanfalseWrite one record per relation referenced by a SELECT or DML statement
log_rowsbooleanfalseAdd the number of rows retrieved or affected
log_statementbooleantrueInclude the statement text
log_statement_oncebooleanfalseInclude the statement text only in the first record of a statement
log_max_string_lengthinteger-1Crop statements and parameters longer than this many characters, up to 102400; -1 keeps them whole
log_nested_statementsbooleantrueAudit statements run inside a function or another statement
log_levelstringlogOne of debug1 to debug5, info, notice, warning, log
log_clientbooleanfalseAccepted, but audit records are not sent to the client session
rolestringunsetRole used for object auditing, see below

log_catalog: false removes the catalog lookups that psql, ORMs and monitoring tools issue, which is most of the noise on a quiet service.

Warning

log_parameter copies bind parameters into the audit records, and with them any personal data or secret the application sends. Keep it off unless the audit requirement demands it, and treat the OpenSearch indices with the same care as the database.

A configuration that records schema, privilege and data changes without catalog noise:

echo '{"pgaudit-settings":{"feature_enabled":true,"log":["ddl","role","write"],"log_catalog":false,"log_max_string_length":2048}}' | \
  exo api update-dbaas-service-pg SERVICE_NAME -z ZONE

Audit Chosen Tables

Object auditing records access to the tables a marker role has privileges on, and nothing else. Set the role in the service settings:

echo '{"pgaudit-settings":{"feature_enabled":true,"role":"auditor"}}' | \
  exo api update-dbaas-service-pg SERVICE_NAME -z ZONE

Then, connected as avnadmin, create the role and grant it the operations to audit:

CREATE ROLE auditor NOLOGIN;
GRANT SELECT, INSERT, UPDATE, DELETE ON public.accounts TO auditor;
GRANT SELECT ON public.payment_methods TO auditor;

Every read or write on public.accounts and every read on public.payment_methods now produces an OBJECT record, whoever runs it. Nobody connects as auditor, so the grants give no one new access. Take a table out of scope by revoking the privilege:

REVOKE ALL ON public.payment_methods FROM auditor;

Note

Once set, role cannot be cleared: the API rejects an empty string and null. To stop object auditing, revoke the privileges of the marker role.

Audit One Role or One Database

avnadmin can override pgaudit.log for a role, a database or the current session. This audits a migration job or one application account without raising the volume for the whole service:

ALTER ROLE ROLE_NAME SET pgaudit.log = 'read, write';

Parameters:

  • ROLE_NAME: the database role whose new sessions are audited.

The same works with ALTER DATABASE DATABASE_NAME SET pgaudit.log = '...' and, for the current session, with SET pgaudit.log = '...'. Remove the override with ALTER ROLE ROLE_NAME RESET pgaudit.log.

Read the Audit Records

In the OpenSearch indices of the logs integration, find the audit records with a query on MESSAGE:

curl -s "OPENSEARCH_URI/INDEX_PREFIX-*/_search?pretty" \
  -H 'Content-Type: application/json' \
  -d '{
    "size": 20,
    "sort": [{"timestamp": "desc"}],
    "_source": ["timestamp", "PG_USER", "PG_DATABASE", "PG_REMOTE_HOST", "MESSAGE"],
    "query": {"match_phrase": {"MESSAGE": "AUDIT:"}}
  }'

Parameters:

  • OPENSEARCH_URI: the URI returned by exo dbaas show OPENSEARCH_SERVICE -z ZONE --uri.
  • INDEX_PREFIX: the index prefix set on the logs integration.

An audit document carries the session context in fields of its own, so its MESSAGE has no log_line_prefix:

{
  "HOSTNAME": "my-postgresql-1",
  "SYSTEMD_UNIT": "postgresql-18.service",
  "AIVEN_AUDIT_FROM": "pg",
  "PG_DATABASE": "defaultdb",
  "PG_USER": "avnadmin",
  "PG_REMOTE_HOST": "203.0.113.10",
  "PG_REMOTE_PORT": "46923",
  "PG_APPLICATION_NAME": "psql",
  "PG_BACKEND_TYPE": "client backend",
  "PG_BACKEND_PID": "1658",
  "PG_XID": "991",
  "MESSAGE": "AUDIT: SESSION,2,1,DDL,DROP TABLE,TABLE,public.audit_smoke_test,DROP TABLE audit_smoke_test,<not logged>",
  "timestamp": "2026-11-10T13:54:44.564472",
  "service_name": "my-postgresql"
}

The PG_* fields are sent even when the integration limits the fields with selected_log_fields. For connections made through a connection pool, PG_REMOTE_HOST is 127.0.0.1, the address of the pooler, not of the client.

Record Layout

MESSAGE holds AUDIT: followed by comma-separated fields:

AUDIT: AUDIT_TYPE,STATEMENT_ID,SUBSTATEMENT_ID,CLASS,COMMAND,OBJECT_TYPE,OBJECT_NAME,STATEMENT,PARAMETER
FieldContent
AUDIT_TYPESESSION for class-based auditing, OBJECT for object auditing
STATEMENT_IDNumber of the statement within the session
SUBSTATEMENT_IDNumber of the sub-statement, for example the grants that a CREATE EXTENSION runs
CLASSThe class that matched, for example DDL, ROLE, READ or WRITE
COMMANDThe command, for example CREATE TABLE or INSERT
OBJECT_TYPE, OBJECT_NAMEFilled for OBJECT records, and for DDL records once the pgaudit extension exists in the database
STATEMENTThe statement text, quoted when it contains a comma
PARAMETERThe bind parameters, or <not logged> while log_parameter is false

A statement can produce both kinds of record. An INSERT on an audited table with write in log gives one SESSION and one OBJECT record with the same statement ID:

AUDIT: SESSION,5,1,WRITE,INSERT,,,"INSERT INTO accounts VALUES (2,'b')",<not logged>
AUDIT: OBJECT,5,1,WRITE,INSERT,TABLE,public.accounts,"INSERT INTO accounts VALUES (2,'b')",<not logged>

Verify That Auditing Is Active

Connected as avnadmin, list the parameters in force:

SELECT name, setting FROM pg_settings WHERE name LIKE 'pgaudit%' ORDER BY name;

pgaudit.log must show the classes you set, not none. Then run a statement of an audited class:

CREATE TABLE audit_smoke_test (id int);
DROP TABLE audit_smoke_test;

Search for audit_smoke_test in OpenSearch with the query above. The records are searchable within seconds.

Related Pages

Last updated on