Review Cloud SQL PostgreSQL disconnection logging

Record required session termination history and session duration.

Description

PostgreSQL’s log_disconnections records when a session ends and how long it lasted.

Potential impact

Missing end times make unusual session lengths harder to investigate. Connection and error logs can help explain why a session ended. SQL activity requires separate logging.

Remediation

  • If you need session history, set log_disconnections to on. Open and close a test session, then find its end time and duration in the logs.
  • Review log volume and set retention and read access for these records.

Examples

These PostgreSQL 15 excerpts show part of an instance configuration. Supply a supported machine type and the other required settings.

Before

hcl
resource "google_sql_database_instance" "db" {
  name             = "postgres-instance"
  database_version = "POSTGRES_15"
  region           = "us-central1"

  settings {
    database_flags {
      name  = "log_disconnections"
      value = "off"
    }
  }
}

After

hcl
resource "google_sql_database_instance" "db" {
  name             = "postgres-instance"
  database_version = "POSTGRES_15"
  region           = "us-central1"

  settings {
    tier = "db-f1-micro"

    database_flags {
      name  = "log_disconnections"
      value = "on"
    }
  }
}

Changing off to on adds a log entry when a session ends. Session access controls are separate.

References