Audit logging in Azure Database for PostgreSQL - Flexible Server

APPLIES TO: Azure Database for PostgreSQL - Flexible Server

Audit logging of database activities in Azure Database for PostgreSQL flexible server is available through the PostgreSQL Audit extension: pgaudit. pgaudit provides detailed session and/or object audit logging.

If you want Azure resource-level logs for operations like compute and storage scaling, see the Azure Activity Log.

Usage considerations

By default, pgaudit log statements and your regular log statements are emitted using Postgres's standard logging facility. In the Azure Database for PostgreSQL flexible server, you can configure all logs to be sent to the Azure Monitor Log store for later analytics in Log Analytics. If you enable Azure Monitor resource logging, your logs are automatically sent (in JSON format) to Azure Storage, Event Hubs, and/or Azure Monitor logs, depending on your choice.

To learn how to set up logging to Azure Storage, Event Hubs, or Azure Monitor logs, visit the resource logs section of the server logs article.

Installing pgaudit

Before you can install pgaudit extension in Azure Database for PostgreSQL flexible server, you need to allowlist pgaudit extension for use.

Using the Azure portal:

  1. Select your Azure Database for PostgreSQL flexible server instance.
  2. On the sidebar, select Server Parameters.
  3. Search for the azure.extensions parameter.
  4. Select pgaudit as the extension you wish to allowlist.

Screenshot of allowlist in Azure Database for PostgreSQL.

Using Azure CLI:

You can allow-list extensions via CLI parameter set command.

az postgres flexible-server parameter set --resource-group <your resource group> --server-name <your server name> --subscription <your subscription id> --name azure.extensions --value `pgaudit`

To install pgaudit, you must include it in the server's shared preload libraries. A change to Postgres's shared_preload_libraries parameter requires a server restart to take effect. You can change parameters using the Azure portal, Azure CLI, or REST API.

Using the Azure portal:

  1. Select your Azure Database for PostgreSQL flexible server instance.

  2. On the sidebar, select Server Parameters.

  3. Search for the shared_preload_libraries parameter.

  4. Select pgaudit.

    Screenshot showing Azure Database for PostgreSQL flexible server enabling shared_preload_libraries for `pgaudit`.

  5. You can check that pgauditis loaded in shared_preload_libraries by executing the following query in psql:

    show shared_preload_libraries;
    

    You should see pgauditin the query result that will return shared_preload_libraries.

  6. Connect to your server using a client (like psql) and enable the pgaudit extension.

    CREATE EXTENSION `pgaudit`;
    

Tip

If you see an error, confirm that you restarted your server after saving shared_preload_libraries.

pgaudit settings

pgaudit allows you to configure session or object audit logging. Session audit logging emits detailed logs of executed statements. Object audit logging is audit scoped to specific relations. You can choose to set up one or both types of logging.

Once you have enabled pgaudit, you can configure its parameters to start logging.
To configure pgaudit you can follow below instructions.
Using the Azure portal:

  1. Select your Azure Database for the PostgreSQL server.

  2. On the sidebar, select Server Parameters.

  3. Search for the pgaudit parameters.

  4. Pick the appropriate settings parameter to edit. For example to start logging set pgaudit.log to WRITE

  5. Select Save button to save changes

The pgaudit documentation provides the definition of each parameter. Test the parameters first and confirm that you're getting the expected behavior.

Setting pgaudit.log_client to ON will redirect logs to a client process (like psql) instead of being written to the file. This setting should generally be left disabled.

pgaudit.log_level is only enabled when pgaudit.log_client is on.

In Azure Database for PostgreSQL flexible server, pgaudit.log can't be set using a - (minus) sign shortcut as described in the pgaudit documentation. All required statement classes (READ, WRITE, etc.) should be individually specified.

If you set the log_statement parameter to DDL or ALL and run a CREATE ROLE/USER ... WITH PASSWORD ... ; or ALTER ROLE/USER ... WITH PASSWORD ... ;, command, then PostgreSQL creates an entry in the PostgreSQL logs, where password is logged in clear text, which might cause a potential security risk. This is the expected behavior per the PostgreSQL engine design.

You can, however, use the pgaudit extension and set the pgaudit.log=DDL parameter in the server parameters page, which doesn't record any CREATE/ALTER ROLE statement in Postgres Log, unlike Postgres log_statement=DDL setting. If you need to log these statements, you can add pgaudit.log ='ROLE' in addition, which redacts the password from logs while logging CREATE/ALTER ROLE.

Audit log format

Each audit entry is indicated by AUDIT: near the beginning of the log line. The format of the rest of the entry is detailed in the pgaudit documentation.

Getting started

To start quickly, set pgaudit.log to WRITE, and open your server logs to review the output.

Viewing audit logs

The way you access the logs depends on which endpoint you choose. See the logs storage account article for Azure Storage. See the stream Azure logs article for Event Hubs.

For Azure Monitor Logs, logs are sent to the workspace you selected. The Postgres logs use the AzureDiagnostics collection mode, so they can be queried from the AzureDiagnostics table. The fields in the table are described below. Learn more about querying and alerting in the Azure Monitor Logs query overview.

You can use this query to get started. You can configure alerts based on queries.

Search for all pgaudit entries in Postgres logs for a particular server on the last day

AzureDiagnostics
| where Resource =~ "myservername"
| where Category == "PostgreSQLLogs"
| where TimeGenerated > ago(1d)
| where Message contains "AUDIT:"