Grafana Cloud

Set up self-managed PostgreSQL

Set up Database Observability with Grafana Cloud to collect telemetry from PostgreSQL using Grafana Alloy. You configure your database and Alloy to forward telemetry to Grafana Cloud.

If you already use the PostgreSQL integration, Database Observability extends it with query-level telemetry collected by the database_observability.postgres Alloy component.

What you’ll achieve

In this article, you:

  • Configure PostgreSQL for monitoring.
  • Configure Grafana Alloy with the Database Observability components.
  • Forward telemetry to Grafana Cloud.
  • Verify that telemetry appears in Database Observability.

Setup steps

Setting up Database Observability for self-managed PostgreSQL has three steps:

  1. Set up your database: Prepare PostgreSQL so Alloy can collect from it.
  2. Configure Grafana Alloy: Configure how Alloy collects telemetry and sends it to Grafana Cloud. Self-managed PostgreSQL supports a few methods to choose from.
  3. Verify telemetry in Grafana Cloud: Check telemetry status and confirm that query metrics appear in Database Observability.

Before you begin

To complete this setup, you need:

  • A self-managed PostgreSQL 14.0 or later database.
  • Permission to modify your PostgreSQL configuration.
  • Permission to restart PostgreSQL if configuration changes require it.
  • A PostgreSQL admin user that can create users and grant privileges.
  • A planned Grafana Alloy deployment location with network access to the PostgreSQL host.

Estimated setup time: 20-40 minutes, excluding any required maintenance window for restarting 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.

Set up your database

In this step, you’ll prepare PostgreSQL for monitoring by enabling pg_stat_statements, creating a monitoring user, and granting the permissions Database Observability needs.

Complete this before configuring Alloy. Without it, Alloy can connect to your database, but it won’t be able to collect the telemetry required for Database Observability.

Configure PostgreSQL settings

Enable pg_stat_statements and configure query tracking in your PostgreSQL configuration. If you change a startup-only setting, restart PostgreSQL before continuing.

Required settings

SettingValueNotes
shared_preload_librariespg_stat_statementsRequires restart when changed
compute_query_idonRequires restart when changed
pg_stat_statements.trackallReload or restart, depending on deployment
track_activity_query_size4096Requires restart when changed

Update PostgreSQL configuration

Add or update these settings in postgresql.conf:

conf
shared_preload_libraries = 'pg_stat_statements'
compute_query_id = on
pg_stat_statements.track = all
track_activity_query_size = 4096

Enable the pg_stat_statements extension

Create the extension in each database you want to monitor:

SQL
-- repeat across all logical databases
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;

Create a monitoring user and grant required privileges

Create the db-o11y user and grant base privileges:

SQL
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.

Alloy must connect as this same user. If you use a name other than db-o11y, replace it in every later step, including the disable-tracking SQL.

Verify that the user has the correct privileges to query pg_stat_statements:

SQL
-- 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:

SQL
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:

SQL
-- 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:

SQL
GRANT pg_read_all_data TO "db-o11y";

Verify PostgreSQL settings

Verify that pg_stat_statements is loaded:

SQL
SHOW shared_preload_libraries;

Expected result: Includes pg_stat_statements.

Verify the extension is installed in each database you want to monitor:

SQL
-- Run in each database you want to monitor.
SELECT extname FROM pg_extension WHERE extname = 'pg_stat_statements';

Expected result: Returns pg_stat_statements.

Verify query tracking settings:

SQL
SHOW compute_query_id;
SHOW pg_stat_statements.track;
SHOW track_activity_query_size;

Expected results:

  • compute_query_id is on.
  • pg_stat_statements.track is all.
  • track_activity_query_size is 4096 or 4kB.

Database setup checkpoint

Continue to Alloy configuration only after these conditions are true:

  • pg_stat_statements is in shared_preload_libraries and the extension is created (SELECT extname FROM pg_extension WHERE extname = 'pg_stat_statements'; returns pg_stat_statements).
  • compute_query_id is on, pg_stat_statements.track is all, and track_activity_query_size is 4096 or 4kB.
  • The db-o11y monitoring user has the required monitoring and object privileges.
  • The db-o11y monitoring user can connect from the network where Alloy will run.
  • Any configuration changes that required a restart have been applied and the PostgreSQL restart is complete.

After these checks pass, PostgreSQL is ready for Database Observability. Next, configure Alloy so it can collect telemetry from the database and send it to Grafana Cloud.

Configure Grafana Alloy

After you set up your database, choose how to configure Alloy.

Pick one:

  1. Configuration page (recommended): Database Observability generates the Alloy configuration for you. Choose Fleet Management or Manual configuration, then select Standalone Alloy or Kubernetes Monitoring Helm chart. Best for most teams.
  2. Kubernetes Monitoring Helm chart: Set databaseObservability.enabled in your values.yaml yourself.
  3. Custom configuration file (advanced): Write the Alloy configuration yourself. Best for full control, custom components or relabeling, or environments the other paths don’t cover.

Make sure you’re on a supported Alloy version

Alloy 1.17.0 or later is required for Database Observability. Find the latest stable version on Docker Hub. To update, refer to the Alloy release notes.

Note

Grafana Alloy is an open source collector that sends your data to Grafana Cloud. Database Observability needs it to collect metrics and query telemetry from your database.

If you don’t have it installed, go to Connections > Collector Setup in Grafana Cloud, open the Install tab, and choose Alloy. For more detail, refer to Get started with Fleet Management and Grafana Alloy.

Start here for most deployments. The Configuration page (Configuration > Setup) generates the configuration for you.

Choose how to manage the configuration:

  • Fleet Management: Grafana Cloud applies and manages the configuration on enrolled Alloy collectors. Refer to Introduction to Fleet Management.
  • Manual configuration: Generate the configuration and deploy it yourself with your own tools.

Then choose where Alloy runs: Standalone Alloy or Kubernetes Monitoring Helm chart. Both platforms support Fleet Management and manual configuration. For manual configuration, the wizard generates an Alloy configuration file for standalone Alloy or Helm values for Kubernetes.

To start the guided setup flow:

  1. Open Database Observability in Grafana Cloud.
  2. Go to Configuration.
  3. Open Setup.
  4. Click Add database.
  5. Select PostgreSQL as the database engine and Self-managed or other as the hosting option.
  6. Choose Fleet Management or Manual configuration, then select Standalone Alloy or Kubernetes Monitoring Helm chart.

For an overview of setup methods and what appears in the Setup tab, refer to Configure Alloy from the Configuration page.

Option 2: Configure the Grafana Kubernetes Monitoring Helm chart manually

Use this method to write the Kubernetes Monitoring Helm chart values yourself instead of generating them from the Configuration page.

Extend your values.yaml and set databaseObservability.enabled to true within the PostgreSQL integration.

YAML
integrations:
  collector: alloy-singleton
  postgresql:
    instances:
      - name: <INSTANCE_NAME>
        jobLabel: integrations/db-o11y
        exporter:
          dataSource:
            host: <DB_HOST>
            port: 5432
            database: postgres
            sslmode: require
            auth:
              usernameKey: <DB_USERNAME_SECRET_KEY>
              passwordKey: <DB_PASSWORD_SECRET_KEY>
          autoDiscovery:
            enabled: true
          collectors:
            statStatements:
              enabled: true
              excludeUsers: ["db-o11y"]
        databaseObservability:
          enabled: true
        secret:
          create: false
          name: <INSTANCE_NAME>
          namespace: postgresql
        logs:
          enabled: true
          labelSelectors:
            app.kubernetes.io/instance: <INSTANCE_NAME>

Replace the placeholders:

  • INSTANCE_NAME: Name for this database instance in Kubernetes.
  • DB_HOST: Hostname or IP address of the database.
  • DB_USERNAME_SECRET_KEY: Kubernetes secret key that holds the database username, for example, username. The value must be the monitoring user you created and granted privileges to, for example, db-o11y. That username must match excludeUsers.
  • DB_PASSWORD_SECRET_KEY: Kubernetes secret key containing database password.

To see the full set of values, refer to the k8s-monitoring Helm chart documentation or the example configuration.

Option 3: Write a custom Alloy configuration file (advanced)

Use this method to write the Alloy configuration yourself instead of generating it from the Configuration page.

Add the PostgreSQL configuration blocks

Add these blocks to Alloy for each PostgreSQL instance. Replace <DB_NAME>. Create a local.file with the data source name string, for example, "postgresql://<DB_O11Y_USER>:<DB_O11Y_PASSWORD>@<DB_HOST>:<DB_PORT>/<DB_DATABASE>?sslmode=require":

Alloy
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"]

  stat_statements {
    exclude_users = ["db-o11y"]
  }

  autodiscovery {
    enabled = true
  }
}

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"]
}

loki.relabel "database_observability_postgres_<DB_NAME>" {
  forward_to = [loki.write.grafana_cloud_loki.receiver]

  // OPTIONAL: add any additional relabeling rules; must be consistent with rules in "discovery.relabel"
  rule {
    target_label = "instance"
    replacement  = "<INSTANCE_LABEL>"
  }
  rule {
    target_label = "<CUSTOM_LABEL_1>"
    replacement  = "<CUSTOM_VALUE_1>"
  }
}

discovery.relabel "database_observability_postgres_<DB_NAME>" {
  targets = database_observability.postgres.postgres_<DB_NAME>.targets

  rule {
    target_label = "job"
    replacement  = "integrations/db-o11y"
  }

  // OPTIONAL: add any additional relabeling rules; must be consistent with rules in "loki.relabel"
  // OPTIONAL: relabel `instance` to `dsn` before overwriting `instance`;
  // the `dsn` label is used in the integration with the knowledge graph
  rule {
    source_labels = ["instance"]
    target_label  = "dsn"
  }
  rule {
    target_label = "instance"
    replacement  = "<INSTANCE_LABEL>"
  }
  rule {
    target_label = "<CUSTOM_LABEL_1>"
    replacement  = "<CUSTOM_VALUE_1>"
  }
}

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).
  • INSTANCE_LABEL: Value that sets the instance label on logs and metrics (optional).
  • CUSTOM_LABEL_1, CUSTOM_VALUE_1: Optional custom label key and value you attach to logs and metrics.
  • Secret file content example: "postgresql://<DB_O11Y_USER>:<DB_O11Y_PASSWORD>@<DB_HOST>:<DB_PORT>/<DB_DATABASE>?sslmode=require".
    • DB_O11Y_USER: The monitoring user you created and granted privileges to, for example, db-o11y. This name must also appear in exclude_users.
    • DB_O11Y_PASSWORD: Password for the database user.
    • DB_HOST: Hostname or IP address of the database.
    • DB_PORT: Database port number.
    • DB_DATABASE: Logical database name in the DSN (recommended: use postgres).

Find more about the options supported by the database_observability.postgres component in the reference documentation.

Add processing of PostgreSQL logs (optional)

Add processing of PostgreSQL logs to gather detailed metrics about query and server errors.

The logs collector processes PostgreSQL logs received through the logs_receiver entry point. By default it counts server errors. When you enable per-query error processing, it also associates errors with the queries that caused them, which powers the per-query Errors views.

Configure log_line_prefix

The database must be configured with the following log_line_prefix value:

SQL
-- Set log format (run as superuser)
ALTER SYSTEM SET log_line_prefix = '%m:%r:%u@%d:[%p]:%l:%e:%s:%v:%x:%c:%q%a:';

-- Reload configuration
SELECT pg_reload_conf();

Verify the that the setting has been applied correctly:

SQL
SHOW log_line_prefix;

Expected value: %m:%r:%u@%d:[%p]:%l:%e:%s:%v:%x:%c:%q%a:

Example log line:

text
2026-02-19 11:36:11.767 GMT:172.18.0.3(35058):user@dbname:[151]:326:40001:2026-02-19 11:35:11 GMT:24/106:0:6996f56f.97:[unknown]:ERROR:  could not serialize access due to concurrent update
Add logs processing configuration

To capture per-query error telemetry, which powers the per-query Errors views, add a logs block to the database_observability.postgres component you defined earlier and set enable_error_logs_processing to true:

Alloy
  logs {
    enable_error_logs_processing = true
  }

Without this block, log processing still produces the global error rate, but per-query error data isn’t collected.

Add the logs file processing configuration block:

Alloy
loki.source.file "database_observability_postgres_<DB_NAME>" {
  targets = [{
    __path__ = "/var/log/postgresql/postgresql-*.log",
    job      = "postgres_logs",
  }]

  forward_to = [database_observability.postgres.postgres_<DB_NAME>.logs_receiver]
}

Note

Persistent storage: The data path (--storage.path) must be persisted across restarts to maintain loki.source.file positions file.

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:

Alloy
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 "grafana_cloud_loki" {
  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.

Verify telemetry in Grafana Cloud

After Alloy starts, verify that Database Observability is receiving telemetry.

  1. In Grafana Cloud, open Database Observability.
  2. Go to Configuration.
  3. Select Telemetry status.
  4. Select your database instance.
  5. Confirm that telemetry status checks pass.
  6. Open Queries overview and confirm that query metrics appear.

After telemetry appears, the database instance should be visible and Queries overview should show query metrics. Additional data such as query samples, wait events, schema details, and explain plans becomes available as Alloy collects it and as the database engine supports it.

Telemetry can take a few minutes to appear. For detailed status checks, refer to Verify telemetry status.

Troubleshoot first-run issues

If data doesn’t appear after setup:

  • If the database instance doesn’t appear in Database Observability, check Alloy connectivity and labels.
  • If telemetry status checks fail, use the Configuration page to identify the failed requirement.
  • If query metrics appear but samples, schema details, or explain plans are missing, check database privileges and pg_stat_statements settings.
  • If Alloy can’t connect to the database, check network and firewall settings, DNS, and the monitoring user’s host restrictions.

For detailed guidance, refer to Troubleshoot Alloy or Troubleshoot PostgreSQL.

Next steps