---
title: "Collect pgaudit Logs"
description: "Enable pgaudit on Managed PostgreSQL with the API, audit statement classes or chosen tables, and read the records in Managed OpenSearch."
url: https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/collect-pgaudit-logs/
product: "PostgreSQL"
section: product
last_updated: 2026-11-10
---
> For AI agents: the documentation index is at https://community.exoscale.com/llms.txt. Every page is available as markdown at `<page URL>index.md` or with `Accept: text/markdown`.

# 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](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/send-logs-to-opensearch/index.md),
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:

```bash
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:

```bash
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:

```sql
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:

| Class | Statements audited |
| --- | --- |
| `read` | `SELECT` and `COPY` from a relation |
| `write` | `INSERT`, `UPDATE`, `DELETE`, `TRUNCATE` and `COPY` into a relation |
| `function` | Function calls and `DO` blocks |
| `role` | `GRANT`, `REVOKE`, `CREATE ROLE`, `ALTER ROLE` and `DROP ROLE` |
| `ddl` | DDL not covered by `role` |
| `misc` | Other commands, for example `SET`, `VACUUM` or `DISCARD` |
| `misc_set` | `SET ROLE` and similar commands |
| `all` | Every 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](#audit-one-role-or-one-database).

### Settings

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

| Key | Type | When not set | Effect |
| --- | --- | --- | --- |
| `feature_enabled` | boolean | `false` | Turns auditing on or off for the whole service |
| `log` | array | `none` | Statement classes audited by session auditing |
| `log_catalog` | boolean | `true` | Also audit statements that only touch `pg_catalog`, such as the queries `psql` runs for `\d` |
| `log_parameter` | boolean | `false` | Add the bind parameters of the statement to the record |
| `log_parameter_max_size` | integer | `0` | Replace parameters longer than this many bytes with `<long param suppressed>`; `0` keeps them all |
| `log_relation` | boolean | `false` | Write one record per relation referenced by a `SELECT` or DML statement |
| `log_rows` | boolean | `false` | Add the number of rows retrieved or affected |
| `log_statement` | boolean | `true` | Include the statement text |
| `log_statement_once` | boolean | `false` | Include the statement text only in the first record of a statement |
| `log_max_string_length` | integer | `-1` | Crop statements and parameters longer than this many characters, up to `102400`; `-1` keeps them whole |
| `log_nested_statements` | boolean | `true` | Audit statements run inside a function or another statement |
| `log_level` | string | `log` | One of `debug1` to `debug5`, `info`, `notice`, `warning`, `log` |
| `log_client` | boolean | `false` | Accepted, but audit records are not sent to the client session |
| `role` | string | unset | Role 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:

```bash
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:

```bash
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:

```sql
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:

```sql
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:

```sql
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`:

```bash
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`:

```json
{
  "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](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/manage-connection-pools/index.md),
`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:

```text
AUDIT: AUDIT_TYPE,STATEMENT_ID,SUBSTATEMENT_ID,CLASS,COMMAND,OBJECT_TYPE,OBJECT_NAME,STATEMENT,PARAMETER
```

| Field | Content |
| --- | --- |
| `AUDIT_TYPE` | `SESSION` for class-based auditing, `OBJECT` for object auditing |
| `STATEMENT_ID` | Number of the statement within the session |
| `SUBSTATEMENT_ID` | Number of the sub-statement, for example the grants that a `CREATE EXTENSION` runs |
| `CLASS` | The class that matched, for example `DDL`, `ROLE`, `READ` or `WRITE` |
| `COMMAND` | The command, for example `CREATE TABLE` or `INSERT` |
| `OBJECT_TYPE`, `OBJECT_NAME` | Filled for `OBJECT` records, and for DDL records once the `pgaudit` extension exists in the database |
| `STATEMENT` | The statement text, quoted when it contains a comma |
| `PARAMETER` | The 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:

```text
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:

```sql
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:

```sql
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

- [Send logs to Managed OpenSearch](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/send-logs-to-opensearch/index.md)
- [Monitor a Managed PostgreSQL service](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/monitor-your-service/index.md)
- [Manage users and databases](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/manage-users-and-databases/index.md)
- [Log formats and log retrieval](https://community.exoscale.com/product/dbaas/service-specific/postgresql/reference/log-formats/index.md)
- [pgaudit documentation](https://github.com/pgaudit/pgaudit) ↗

