ALTER TABLE ... EXPERIMENTAL_AUDIT command to enable the SENSITIVE_ACCESS logging channel per table.
This page provides an example of SQL audit logging in CockroachDB, including:
- How to turn audit logging on and off.
- Where the audit log files live.
- What the audit log files look like.
File format
Audit log messages, like all log messages, consist of two sections:- A payload that contains notable events structured in JSON. These can include information such as the application name, full text of the query (which may contain PII), user account that triggered the event, number of rows produced (e.g., for
SELECT) or processed (e.g., forINSERTorUPDATE), status of the query, and more. For more information on the possible event types logged to theSENSITIVE_ACCESSchannel, see Notable Event Types. - An envelope that contains event metadata (e.g., severity, date, timestamp, channel). Depending on the log format you specify when configuring logs, the envelope can be formatted either as JSON or as a flat prefix to the message.
File storage location
By default, audit logs are prefixedcockroach-sql-audit and are stored in the same directory as the other logs generated by CockroachDB.
To store the audit log files in a specific directory, configure the SENSITIVE_ACCESS channel with a custom dir path.
Performance considerations
To ensure non-repudiation in audit logs, we recommend enablingauditable for the SENSITIVE_ACCESS channel. CockroachDB will then synchronously log all of the activity of every user on a cluster in a way that is durable to system failures. Note that every query that causes a logging event must access the disk of the node on which audit logging is enabled. As a result, enabling auditable on a logging channel negatively impacts performance, and we recommend using this setting for security purposes only.
For debugging and troubleshooting on production clusters, the most performant way to log all queries is to enable the SQL_EXEC logging channel. For details, see Logging Use Cases.
Example
Step 1. Create sample tables
Use the statements below to create:- A
customerstable which contains PII such as name, address, etc. - An
orderstable with a foreign key intocustomers, which does not expose any PII
customers table.
Step 2. Turn on auditing for the customers table
We turn on auditing for a table using the EXPERIMENTAL_AUDIT subcommand of ALTER TABLE.
customers table into the SENSITIVE_ACCESS logging channel.
To turn on auditing for more than one table, issue a separate
ALTER statement for each table.Step 3. Populate the customers table
Now that we have auditing turned on, let’s add some customer data:
Step 4. Check the audit log
By default, events in theSENSITIVE_ACCESS channel are output to a log file that is prefixed cockroach-sql-audit and stored in the same directory as the other logs generated by CockroachDB.
To store the audit log files in a specific directory, configure the SENSITIVE_ACCESS channel with a custom dir path. Like the other log files, it’s rotated according to the max-file-size setting.
When we look at the audit log for this example, we see the following lines showing every command we’ve run so far, as expected.
The above example shows the default
crdb-v2 log format. This can be changed to another format (e.g., JSON). For details, see Configure Logs.Step 5. Populate the orders table
Unlike the customers table, orders doesn’t have any PII, just a Product ID and a delivery status.
Let’s populate the orders table with some placeholder data using CREATE SEQUENCE:
SELECT returned multiple results, but it doesn’t in this case.
Step 6. Check the audit log again
Because we used aSELECT against the customers table to generate the placeholder data for orders, those queries will also show up in the audit log.
Note that two log entries are created for each query: one entry for the SELECT subquery, and one entry for the foreign key check on customer_id. Since the customers table is read twice with each query, the TableName and TxnCounter values will be duplicated across entries:
crdb-v2 log format. This can be changed to another format (e.g., JSON). For details, see Configure Logs.
For descriptions of all SQL audit event types and their fields, see Notable Event Types.

