Set up Google CloudSQL PostgreSQL
Set up Database Observability with Grafana Cloud to collect telemetry from Google CloudSQL PostgreSQL instances using Grafana Alloy. You configure your CloudSQL instance and Alloy to forward telemetry to Grafana Cloud.
What you’ll achieve
In this article, you:
- Configure CloudSQL PostgreSQL database flags for monitoring.
- Create monitoring users with required privileges.
- Configure Alloy with the Database Observability components.
- Forward telemetry to Grafana Cloud.
Before you begin
Review these requirements:
- CloudSQL PostgreSQL 14.0 or later.
- Access to modify CloudSQL instance database flags.
- Grafana Alloy deployed and accessible to your CloudSQL instance.
- Network connectivity between Alloy and your CloudSQL instance (via private IP, public IP with authorized networks, or Cloud SQL Auth Proxy).
For general PostgreSQL setup concepts, refer to Set up PostgreSQL.
Note
Alloy should connect directly to the database host. Avoid connecting Alloy to the database through a load balancer or connection pooler such as PgBouncer as it would limit Alloy’s ability to collect accurate telemetry.
Configure database flags
Enable pg_stat_statements and configure query tracking by adding database flags to your CloudSQL PostgreSQL instance. These flags require an instance restart to take effect.
Required database flags
Use the Google Cloud console
- Open the Cloud Console and navigate to SQL.
- Select your CloudSQL PostgreSQL instance.
- Click Edit.
- Expand Flags section.
- Click Add a database flag for each flag listed above.
- Set the flag name and value as specified in the table.
- Click Save to apply the changes.
- The instance restarts automatically to apply the new flags.
For detailed console instructions, refer to Configure database flags in the Google Cloud documentation.
Use Terraform
Using Terraform with google_sql_database_instance:
resource "google_sql_database_instance" "postgres" {
name = "<INSTANCE_NAME>"
database_version = "POSTGRES_16"
region = "<REGION>"
settings {
database_flags {
name = "pg_stat_statements.track"
value = "all"
}
database_flags {
name = "track_activity_query_size"
value = "4096"
}
}
}Replace the placeholders:
INSTANCE_NAME: Your CloudSQL instance name.REGION: GCP region where the instance is deployed.
Alternatively, configure flags using the gcloud CLI:
gcloud sql instances patch <INSTANCE_NAME> \
--pg_stat_statements.track=all,track_activity_query_size=4096Note
CloudSQL requires an instance restart after changing database flags. The restart happens automatically when you apply the changes.
After the instance restarts, enable the pg_stat_statements extension in each database you want to monitor:
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;Verify the extension is installed:
SELECT * FROM pg_stat_statements LIMIT 1;Create a monitoring user and grant required privileges
Connect to your Cloud SQL for PostgreSQL instance as an administrator and create the monitoring user:
Create the db-o11y user and grant base privileges:
CREATE USER "db-o11y" WITH PASSWORD '<DB_O11Y_PASSWORD>';
GRANT pg_monitor TO "db-o11y";
GRANT pg_read_all_stats TO "db-o11y";Replace <DB_O11Y_PASSWORD> with a secure password for the db-o11y PostgreSQL user.
Verify that the user has the correct privileges to query pg_stat_statements:
-- run with the `db-o11y` user
SELECT * FROM pg_stat_statements LIMIT 1;Disable tracking of monitoring user queries
Prevent tracking of queries executed by the monitoring user itself:
ALTER ROLE "db-o11y" SET pg_stat_statements.track = 'none';Grant object privileges for detailed data
To allow collecting schema details and table information, connect to each logical database and grant access to each schema.
For example, for a payments database:
-- switch to the 'payments' database
\c payments
-- grant permissions in the 'public' schema
GRANT USAGE ON SCHEMA public TO "db-o11y";
GRANT SELECT ON ALL TABLES IN SCHEMA public TO "db-o11y";
-- grant permissions in the 'tests' schema
GRANT USAGE ON SCHEMA tests TO "db-o11y";
GRANT SELECT ON ALL TABLES IN SCHEMA tests TO "db-o11y";Alternatively, if you’re unsure which specific schemas need access, use the predefined role to grant USAGE and SELECT access to all objects:
GRANT pg_read_all_data TO "db-o11y";Verify database flag settings
Verify that the parameter settings were applied correctly after restarting:
SHOW pg_stat_statements.track;Expected result: Value is all.
SHOW track_activity_query_size;Expected result: Value is 4096.
Run and configure Alloy
Run Alloy and add the Database Observability configuration for your CloudSQL instance.
Run the latest Alloy version
Run Alloy version 1.13.0 or later with the --stability.level=public-preview flag for the database_observability.postgres component. Find the latest stable version on Docker Hub.
Add the CloudSQL PostgreSQL configuration blocks
Add these blocks to Alloy for CloudSQL PostgreSQL. Replace <DB_NAME>. Create a local.file with the Data Source Name string, for example, "postgresql://<DB_USER>:<DB_PASSWORD>@<INSTANCE_IP>:<DB_PORT>/<DB_DATABASE>?sslmode=require":
local.file "postgres_secret_<DB_NAME>" {
filename = "/var/lib/alloy/postgres_secret_<DB_NAME>"
is_secret = true
}
prometheus.exporter.postgres "postgres_<DB_NAME>" {
data_source_names = [local.file.postgres_secret_<DB_NAME>.content]
enabled_collectors = ["stat_statements"]
autodiscovery {
enabled = true
// Exclude the cloudsqladmin database
database_denylist = ["cloudsqladmin"]
}
}
database_observability.postgres "postgres_<DB_NAME>" {
data_source_name = local.file.postgres_secret_<DB_NAME>.content
forward_to = [loki.relabel.database_observability_postgres_<DB_NAME>.receiver]
targets = prometheus.exporter.postgres.postgres_<DB_NAME>.targets
enable_collectors = ["query_details", "query_samples", "schema_details", "explain_plans"]
exclude_databases = ["cloudsqladmin"]
}
loki.relabel "database_observability_postgres_<DB_NAME>" {
forward_to = [loki.write.logs_service.receiver]
rule {
target_label = "instance"
replacement = "<INSTANCE_LABEL>"
}
}
discovery.relabel "database_observability_postgres_<DB_NAME>" {
targets = database_observability.postgres.postgres_<DB_NAME>.targets
rule {
target_label = "job"
replacement = "integrations/db-o11y"
}
rule {
target_label = "instance"
replacement = "<INSTANCE_LABEL>"
}
}
prometheus.scrape "database_observability_postgres_<DB_NAME>" {
targets = discovery.relabel.database_observability_postgres_<DB_NAME>.output
forward_to = [prometheus.remote_write.metrics_service.receiver]
}Replace the placeholders:
DB_NAME: Database name Alloy uses in component identifiers (appears in component names and secret filenames).GCP_PROJECT_ID: Google Cloud project ID where your CloudSQL instance runs.CLOUDSQL_INSTANCE_NAME: CloudSQL instance name.GCP_REGION: GCP region where the instance is deployed.INSTANCE_LABEL: Value that sets theinstancelabel on logs and metrics (optional).- Secret file content example:
"postgresql://DB_USER:DB_PASSWORD@INSTANCE_IP:DB_PORT/DB_DATABASE?sslmode=require".DB_USER: Database user Alloy uses to connect (for example,db-o11y).DB_PASSWORD: Password for the database user.INSTANCE_IP: CloudSQL instance IP address (private or public).DB_PORT: Database port number (default:5432).DB_DATABASE: Logical database name in the DSN (recommend: usepostgres).
Add Prometheus and Loki write configuration
Add the Prometheus remote write and Loki write configuration. From Grafana Cloud, open your stack to get the URLs and generate API tokens:
prometheus.remote_write "metrics_service" {
endpoint {
url = sys.env("GCLOUD_HOSTED_METRICS_URL")
basic_auth {
password = sys.env("GCLOUD_RW_API_KEY")
username = sys.env("GCLOUD_HOSTED_METRICS_ID")
}
}
}
loki.write "logs_service" {
endpoint {
url = sys.env("GCLOUD_HOSTED_LOGS_URL")
basic_auth {
password = sys.env("GCLOUD_RW_API_KEY")
username = sys.env("GCLOUD_HOSTED_LOGS_ID")
}
}
}Replace the placeholders:
GCLOUD_HOSTED_METRICS_URL: Your Grafana Cloud Prometheus remote write URL.GCLOUD_HOSTED_METRICS_ID: Your Grafana Cloud Prometheus instance ID (username).GCLOUD_HOSTED_LOGS_URL: Your Grafana Cloud Loki write URL.GCLOUD_HOSTED_LOGS_ID: Your Grafana Cloud Loki instance ID (username).GCLOUD_RW_API_KEY: Grafana Cloud API token with write permissions.
Run and configure Alloy with the Grafana Kubernetes Monitoring Helm chart
Extend your values.yaml when you use the k8s-monitoring Helm chart and set databaseObservability.enabled to true within the PostgreSQL integration.
integrations:
collector: alloy-singleton
postgresql:
instances:
- name: <INSTANCE_NAME>
exporter:
dataSource:
host: <INSTANCE_IP>
port: 5432
database: postgres
sslmode: require
auth:
usernameKey: username
passwordKey: password
collectors:
statStatements: true
databaseObservability:
enabled: true
extraConfig: |
exclude_databases = ["cloudsqladmin"]
collectors:
queryDetails:
enabled: true
querySamples:
enabled: true
schemaDetails:
enabled: true
explainPlans:
enabled: true
secret:
create: false
name: <SECRET_NAME>
namespace: <NAMESPACE>
logs:
enabled: true
labelSelectors:
app.kubernetes.io/instance: <INSTANCE_NAME>Replace the placeholders:
INSTANCE_NAME: Name for this database instance in Kubernetes.INSTANCE_IP: CloudSQL instance IP address.GCP_PROJECT_ID: Google Cloud project ID where your CloudSQL instance runs.CLOUDSQL_INSTANCE_NAME: CloudSQL instance name.GCP_REGION: GCP region where the instance is deployed.SECRET_NAME: Name of the Kubernetes secret containing database credentials.NAMESPACE: Kubernetes namespace where the secret exists.
To see the full set of values, check out the k8s-monitoring Helm chart documentation or the example configuration.
Optional: Configure GCP Secret Manager and Kubernetes
If you use GCP Secret Manager with External Secrets Operator to manage database credentials, configure them as follows.
Secret path convention
Store monitoring credentials in GCP Secret Manager with a name following this convention:
cloudsql-<INSTANCE_NAME>-monitoringPostgreSQL secret format
Store the secret as JSON with the following format:
{
"username": "db-o11y",
"password": "<DB_O11Y_PASSWORD>",
"host": "<INSTANCE_IP>",
"port": 5432,
"database": "postgres"
}Replace the placeholders:
DB_O11Y_PASSWORD: Password for thedb-o11yPostgreSQL user.INSTANCE_IP: CloudSQL instance IP address.
Create the secret via gcloud CLI
echo '{"username":"db-o11y","password":"<DB_O11Y_PASSWORD>","host":"<INSTANCE_IP>","port":5432,"database":"postgres"}' | \
gcloud secrets create cloudsql-<INSTANCE_NAME>-monitoring --data-file=-Kubernetes External Secrets configuration
Use the External Secrets Operator to sync the GCP secret into Kubernetes:
---
apiVersion: external-secrets.io/v1beta1
kind: SecretStore
metadata:
name: <INSTANCE_NAME>-db-monitoring-secretstore
spec:
provider:
gcpsm:
projectID: <GCP_PROJECT_ID>
---
apiVersion: external-secrets.io/v1beta1
kind: ExternalSecret
metadata:
name: <INSTANCE_NAME>-db-monitoring-secret
spec:
refreshInterval: 1h
secretStoreRef:
kind: SecretStore
name: <INSTANCE_NAME>-db-monitoring-secretstore
dataFrom:
- extract:
key: cloudsql-<INSTANCE_NAME>-monitoringReplace the placeholders:
INSTANCE_NAME: CloudSQL instance name.GCP_PROJECT_ID: Google Cloud project ID.
Next steps
For an overview of key concepts, refer to Introduction to Database Observability.
For troubleshooting during setup, refer to Troubleshoot.



