Restarting can take several seconds depending on the size of your Database Instance.
Setting up and using the pgaudit extension
The pgaudit
extension is available with Scaleway Managed Databases for PostgreSQL. The extension is a powerful monitoring and logging tool that allows you to keep track of the actions happening in your databases, and record them.
The main purpose of pgaudit
is to allow PostgreSQL users to produce audit logs often required to comply with government, financial, and/or ISO certifications.
pgaudit
allows you to log different statements executed in your Database Instance, or specific databases. You can keep track of who accessed your databases, what actions were performed, and when, under a structured format.
You can configure the extension to log only what is relevant to your audits.
Before you start
To complete the actions presented below, you must have:
- A Scaleway account logged into the console
- Owner status or IAM permissions allowing you to perform actions in the intended Organization
- A Database Instance running a PostgreSQL engine
Enabling and installing pgaudit
-
Click PostgreSQL and MySQL under Managed Databases on the side menu of the Scaleway console. A list of your Database Instances displays.
-
Click the Database Instance name or «See more Icon» > More info to access the Instance information page.
-
Click the Advanced settings tab.
-
Click «Edit Icon».
-
Click + Add parameters.
-
Select
rdb.enable_pgaudit
in the drop-down menu and click the toggle to set it toon
. -
Click «Validate Icon» to validate. Your Database Instance restarts.
Important -
Run the following command to install the
pgaudit
extension.CREATE EXTENSION pgauditIf the operation is successful, no output is returned.
Configuring pgaudit
pgaudit
provides two different types of logging:
- Session audit logging - logs the operations, such as queries and updates, executed by a user during a session within a database or the entire Database Instance
- Object audit logging - logs actions that involve specific data objects, like tables or files.
To use each one, you must configure the pgaudit.log
and pgaudit.role
settings, respectively.
Both settings can be set up at the Database Instance level, meaning that a global configuration can be applied to all databases within the Instance. You can configure them in the advanced settings of your Database Instance. You can use both settings concurrently.
pgaudit.log
can also be configured for specific databases within your Instance.
Setting up pgaudit.log
The pgaudit.log
setting determines which classes of statements will be recorded through session audit logging.
The possible values include:
Class | Action |
---|---|
READ | Logs SELECT and COPY commands when the source is a relation or a query. |
WRITE | Logs INSERT , UPDATE , DELETE , TRUNCATE , and COPY commands when the destination is a relation. |
FUNCTION | Logs function calls and DO blocks. |
ROLE | Logs statements related to roles and privileges, such as GRANT , REVOKE , CREATE , ALTER , and DROP ROLE . |
DDL | Covers all Data Definition Language (DDL) statements not included in the ROLE class. |
MISC | Logs miscellaneous commands like DISCARD , FETCH , CHECKPOINT , VACUUM , SET . |
MISC_SET | Logs miscellaneous SET commands. |
ALL | Includes all of the above classes for comprehensive logging. |
- Go to the Advanced settings of your Database Instance in the Scaleway console.
- Click «Edit Icon».
- Click + Add parameters.
- Select
pgaudit.log
in the drop-down. - Enter one or more of the statement class values listed above.
Note
The values must be written in uppercase. If entering more than one class, separate them with a comma and no spaces. For example:
READ,WRITE
. - Click «Validate Icon» to validate.
Note
The configuration takes a few seconds to be applied. During this time the Database Instance connection remains uninterrupted. However, you must wait until the new configuration is applied to edit your advanced settings again.
You can edit the pgaudit.log
values in the advanced settings anytime.
-
Connect to your database as an admin user.
-
Run the following command to set
pgaudit.log
for the database. Replace<database>
with the name of your database, and<class>
with one or more of the statement class values listed above. When listing more than one class, separate them with a comma and no spaces. For example:WRITE,FUNCTION
.database=> ALTER DATABASE <database> SET pgaudit.log = '<class>'If the operation is successful no output is returned.
-
Reset the connection to your database to apply the configuration:
database=> \q$ psql -h <ip-address> -p <port> -d <database-name> -U <username>TipRun
\drds
to see a list of your settings. You should get an output like the following:List of settingsRole | Database | Settings------+----------+----------------------------------------------| postgres | pgaudit.log=WRITE,FUNCTION,ROLE,DDL,MISC_SET
Setting up pgaudit.role
Object audit logging is configured through the roles system. The pgaudit.role
setting allows you to specify a principal audit role (also known as “master role”).
Various audit roles can be established, delegating responsibility for distinct aspects of audit logging to different groups. These roles must subsequently be assigned to the principal audit role to facilitate audit logging.
The primary function of the principal audit role is to oversee and manage the audit logging process. The auditing process is generally handled by an independent individual or company, which will use the audit logs to perform their audit.
Audit logging for an object (such as a TABLE
or VIEW
) takes place when the principal audit role has the necessary permissions to log the actions carried out on the object, or when it inherits these permissions from another role.
Currently, only the SELECT
, INSERT
, UPDATE
, and DELETE
permissions are supported.
There is no default principal role. You must create a new role, or specify an existing one as the principal audit.
When specifying a principal role for object audit logging, we recommend first revoking all its privileges, and then granting specific audit permissions, to improve auditing.
To set up and grant permissions to the principal audit role follow these instructions:
-
Connect to your database as an admin user.
-
Run the following command to create a new user. If no output is returned, the operation was successful.
NoteIf you want to specify an existing role as the principal audit, you can skip this step.
CREATE ROLE auditor;You can replace
auditor
with your name of choice.TipRun
\du
to see a list of your database roles and check that the new role was created. -
Set the role you created as
pgaudit.role
SET pgaudit.role = 'auditor';NoteAlternatively, you can follow these steps to configure
pgaudit.role
from the Scaleway console:- Go to the Advanced settings of your Database Instance in the Scaleway console.
- Click «Edit Icon».
- Click + Add parameters.
- Select
pgaudit.role
in the drop-down and enter the name of the role you created to be the principal. - Click «Validate Icon» to validate.
NoteThe configuration takes a few seconds to be applied. During this time the Database Instance connection remains uninterrupted. However, you must wait until the new configuration is applied to edit your advanced settings again.
You can edit the
pgaudit.role
values in the advanced settings anytime. -
Run the following command in your Database Instance to grant the principal audit role permissions:
GRANT PERMISSION1, PERMISSION2on <object>to <principal-role>;Replace
<PERMISSION1>
AND<PERMISSION2>
with one or more of the supported permissions,<object>
with the name of the data object you want to audit, andauditor
with the<principal-role>
with the name of the principal role.
If the operation was successful, you see GRANT
as an output.
Accessing and handling logs
Your logs are accessible via the Scaleway Observability dashboard, Cockpit.
With Cockpit’s free plan, your logs remain available for a week. If you want to extend the volume capacity and the duration of storage, you can upgrade to a paid plan. Refer to the Cockpit FAQ for more information about plans and billing.
- Follow the Cockpit Quickstart to activate the feature and retrieve your Grafana credentials.
- Configure the Grafana agent.
- Connect to Grafana with the credentials you created.
- Click RDB PostgreSQL Overview on the left-side menu.
- Scroll to the Logs section and click to expand.
- View the logs.
Technical Information
-
All management actions performed via API or the console, such as database creations and deletions, are logged, no matter the configuration. However, the associated read statements are not logged. For example, select statements used to check that actions have been correctly executed are not logged.
-
When objects are renamed, they will start to be logged under their new name. For more information, refer to the official pgaudit repository on Github
-
Audit logs are displayed in a specific format that you can also find referenced in the official pgaudit repository on Github
Optimizing disk usage
-
Audit logs are flushed and consume disk space. The log ingestion rate is 4 MB per second.
To avoid log generation from exceeding ingestion, and the subsequent uncontrolled growth of disk usage, make sure you closely monitor the disk usage of the Database Instance.
ImportantIf the disk space becomes full, some audit logs may be lost, and the database can become unavailable.
-
Before activating any audits, make sure you define exactly which statements are mandatory.
Some statements might not be important for auditing use cases, such as
SELECT
. When you generate high-volume logs, they can take up significant disk space.TipWhen it comes to
SELECT
andDML
statements, we recommend opting for an object audit logging configuration rather than a session audit one. Refer to the official pgaudit Github repository for more information.
Deactivating pgaudit
-
Connect to your database as an admin user.
-
Run the following command to deactivate the
pgaudit
extension in your Database Instance.DROP EXTENSION pgaudit -
Go to the Advanced settings of your Database Instance in the Scaleway console.
-
Click «Edit Icon».
-
Click + Add parameters.
-
Select
rdb.enable_pgaudit
in the drop-down and click the toggle to set it tooff
. -
Click «Validate Icon» to validate. Your Database Instance restarts.
NoteIf you reactivate the extension, your previous configuration is applied.
Setup examples
Session audit logging for ALL
To set up session audit logging of all statements in all databases and roles, set pgaudit.log
to ALL
in the advanced settings tab of your Database Instance dashboard in the Scaleway console.
Session audit logging for only DML statements
To set up session audit logging only of Data Manipulation Language (DML) statements for all databases and roles, set pgaudit.log
to WRITE
in the advanced settings tab of your Database Instance dashboard in the Scaleway console.
Session audit logging for ALL statements in a specific database
To set up session audit logging of all statements in only one database, you must:
- Set
pgaudit.log
toNONE
in the advanced settings tab of your Database Instance dashboard in the Scaleway console. - Connect to your database as an admin user and run the following command:
ReplaceALTER DATABASE mydb SET pgaudit.log = 'ALL';
mydb
with the name of your database.
Session audit logging and object audit logging
To set up:
- session audit logging of all statements except
READ
andWRITE
for all databases and roles, and - object audit logging of
SELECT
,INSERT
,UPDATE
, andDELETE
statements only on a specific table in a database.
-
Set
pgaudit.log
toFUNCTION,ROLE,DDL,MISC,MISC_SET
, and thepgaudit.role
asauditor
in the advanced settings tab of your Database Instance dashboard in the Scaleway console. You can replaceauditor
with the name of your principal audit role.NoteThere is no default principal role. You must create a new role, or specify an existing one as the principal audit.
-
Connect to your database as an admin user and run the following command:
GRANT SELECT, INSERT, UPDATE, DELETEon mydb.mytableto auditor;Replace
mydb
with the name of your database andmytable
with the name of the table.