PostgreSQL

This document describes how to configure your Google Kubernetes Engine deploymentso that you can use Google Cloud Managed Service for Prometheus to collect metrics fromPostgreSQL. This document shows you how to do the following:

  • Set up the exporter for PostgreSQL to report metrics.
  • Access a predefined dashboard in Cloud Monitoring to view the metrics.
  • Configure alerting rules to monitor the metrics.

These instructions apply only if you are usingmanaged collectionwith Managed Service for Prometheus.If you are using self-deployed collection, then see thesource repositoryfor the PostgreSQL exporterfor installation information.

These instructions are provided as an example and are expected to work inmost Kubernetes environments.If you are having trouble installing anapplication or exporter due to restrictive security or organizational policies,then we recommend you consult open-source documentation for support.

For information about PostgreSQL, seePostgreSQL.

Prerequisites

To collect metrics fromPostgreSQLby usingManaged Service for Prometheus and managed collection, your deployment mustmeet the following requirements:

  • Your cluster must be running Google Kubernetes Engine version 1.28.15-gke.2475000 or later.
  • You must be running Managed Service for Prometheus with managed collection enabled. For more information, see Get started with managed collection.

  • To use dashboards available in Cloud Monitoring for the PostgreSQL integration, you must usepostgres_exporter version v0.11.1 or later.

    For more information about available dashboards, seeView dashboards.

For information about creating a least-privileged user, seeRunning as non-superuser.

Install the PostgreSQL exporter

We recommend that you install the PostgreSQL exporter,postgres_exporter,as a sidecar to your PostgreSQL workload.For information about using sidecars, seeExtended applications on Kubernetes with multi-containerpods.

To installpostgres_exporter as a sidecar to PostgreSQL,modify your PostgreSQL configuration as shown in the followingexample:

#Copyright2022GoogleLLC##LicensedundertheApacheLicense,Version2.0(the"License");#youmaynotusethisfileexceptincompliancewiththeLicense.#YoumayobtainacopyoftheLicenseat##https://www.apache.org/licenses/LICENSE-2.0##Unlessrequiredbyapplicablelaworagreedtoinwriting,software#distributedundertheLicenseisdistributedonan"AS IS"BASIS,#WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.#SeetheLicenseforthespecificlanguagegoverningpermissionsand#limitationsundertheLicense.apiVersion:apps/v1kind:StatefulSetmetadata:name:postgresqlspec:serviceName:postgresqlselector:matchLabels:+app.kubernetes.io/name:postgresqltemplate:metadata:labels:+app.kubernetes.io/name:postgresqlspec:containers:-image:postgres:15.3name:postgresqlenv:-name:POSTGRES_USERvalue:root-name:POSTGRES_PASSWORDvalue:password-name:POSTGRES_DBvalue:dev+-name:exporter+image:quay.io/prometheuscommunity/postgres-exporter:v0.14.0+args:+---collector.stat_statements+env:+-name:DATA_SOURCE_NAME+value:postgresql://root:password@localhost:5432/dev?sslmode=disable+ports:+-containerPort:9187+name:prometheus

You must add any lines preceded by the+ symbol to yourconfiguration.

Update theDATA_SOURCE_NAME environment variable with credentials that work with your PostgreSQL instance.

To apply configuration changes from a local file, run the following command:

kubectl apply -nNAMESPACE_NAME -fFILE_NAME

You can alsouse Terraformto manage your configurations.

Define a PodMonitoring resource

For target discovery, the Managed Service for Prometheus Operatorrequires a PodMonitoring resource that corresponds tothe PostgreSQL exporter in the same namespace.

You can use the following PodMonitoring configuration:

# Copyright 2022 Google LLC## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at##     https://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.apiVersion:monitoring.googleapis.com/v1kind:PodMonitoringmetadata:name:postgresqllabels:app.kubernetes.io/name:postgresqlapp.kubernetes.io/part-of:google-cloud-managed-prometheusspec:endpoints:-port:prometheusscheme:httpinterval:30spath:/metricsselector:matchLabels:app.kubernetes.io/name:postgresql

Ensure that the label selectors and the port match the selectors and portused inInstall the PostgreSQL exporter.

To apply configuration changes from a local file, run the following command:

kubectl apply -nNAMESPACE_NAME -fFILE_NAME

You can alsouse Terraformto manage your configurations.

Define rules and alerts

You can use the followingRules configuration to definealerts on your PostgreSQL metrics:

# Copyright 2022 Google LLC## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at##     https://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.apiVersion:monitoring.googleapis.com/v1kind:Rulesmetadata:name:postgresql-ruleslabels:app.kubernetes.io/component:rulesapp.kubernetes.io/name:postgresql-rulesapp.kubernetes.io/part-of:google-cloud-managed-prometheusspec:groups:-name:postgresqlinterval:30srules:-alert:PostgreSQLDatabaseSizeTooLargeannotations:description:|-PostgreSQL database size too largeVALUE = {{ $value }}LABELS: {{ $labels }}summary:PostgreSQL database size too large (instance {{ $labels.instance }})expr:pg_database_size_bytes > 100000000000for:5mlabels:severity:warning

To apply configuration changes from a local file, run the following command:

kubectl apply -nNAMESPACE_NAME -fFILE_NAME

You can alsouse Terraformto manage your configurations.

For more information about applying rules to your cluster, seeManaged rule evaluation and alerting.

You can adjust the alert thresholds to suit your application.

Verify the configuration

You can use Metrics Explorer to verify that you correctly configuredthe PostgreSQL exporter. It might take one or two minutes forCloud Monitoring to ingest your metrics.

To verify the metrics are ingested, do the following:

  1. In the Google Cloud console, go to the Metrics explorer page:

    Go toMetrics explorer

    If you use the search bar to find this page, then select the result whose subheading isMonitoring.

  2. In the toolbar of thequery-builder pane, select the button whose name is either MQL or PromQL.
  3. Verify thatPromQL is selectedin theLanguage toggle. The language toggle is in the same toolbar thatlets you format your query.
  4. Enter and run the following query:
    up{job="postgresql", cluster="CLUSTER_NAME", namespace="NAMESPACE_NAME"}

View dashboards

The Cloud Monitoring integration includesthePostgreSQL Prometheus Overview dashboard.Dashboards are automatically installed when you configure the integration.You can also view static previews of dashboards without installing theintegration.

To view an installed dashboard, do the following:

  1. In the Google Cloud console, go to the Dashboards page:

    Go toDashboards

    If you use the search bar to find this page, then select the result whose subheading isMonitoring.

  2. Select theDashboard List tab.
  3. Choose theIntegrations category.
  4. Click the name of the dashboard, for example,PostgreSQL Prometheus Overview.

To view a static preview of the dashboard, do the following:

  1. In the Google Cloud console, go to the Integrations page:

    Go toIntegrations

    If you use the search bar to find this page, then select the result whose subheading isMonitoring.

  2. Click theKubernetes Engine deployment-platform filter.
  3. Locate the PostgreSQL integration and clickView Details.
  4. Select theDashboards tab.

Troubleshooting

For information about troubleshooting metric ingestion problems, seeProblems with collection from exporters inTroubleshooting ingestion-side problems.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-17 UTC.