Review Cloud SQL PostgreSQL pgAudit configuration

Audit required database operations and verify that logs are received.

Description

pgAudit records selected SQL operations inside a Cloud SQL for PostgreSQL database. Instance administration is audited separately.

Potential impact

Missing SQL records can leave gaps in an investigation. Recording too many operations can expose query values and increase disk usage and logging costs.

Remediation

  1. Schedule a maintenance window. Setting cloudsql.enable_pgaudit to on restarts the instance.
  2. After the restart, reconnect with psql and create the pgAudit extension. Set pgaudit.log to the classes of SQL operations you need to audit.
  3. Enable the required Data Access audit logs in the project and find a test event in Cloud Logging. Check disk usage, limit read access and set retention.

Examples

Use a supported PostgreSQL version and supply the other required instance settings. You can enable the flag on the same engine version.

Before

hcl
resource "google_sql_database_instance" "example" {
  name             = "postgres-instance-with-flag"
  database_version = "POSTGRES_14"
  region           = "us-central1"

  settings {
    database_flags {
      name  = "cloudsql.enable_pgaudit"
      value = "off"
    }
  }
}

After

hcl
resource "google_sql_database_instance" "example" {
  name             = "postgres-instance-with-flag"
  database_version = "POSTGRES_15"
  region           = "us-central1"

  settings {
    database_flags {
      name  = "cloudsql.enable_pgaudit"
      value = "on"
    }
  }
}

The second excerpt turns on the instance flag. You still need to create the extension and choose which operations to record. Other server logging is configured separately.

References