Configure Grafana Alloy
In this milestone, you’ll install Grafana Alloy and add the Database Observability configuration blocks for PostgreSQL. Alloy collects both metrics and structured logs from your database and forwards them to Grafana Cloud.
To configure Grafana Alloy to connect your PostgreSQL database to Grafana Cloud, complete the following steps:
Install Grafana Alloy 1.15.0 or later. Refer to the Alloy installation documentation for your platform.
Create the Data Source Name secret file. Replace the placeholders with your values:
echo 'postgresql://db-o11y:<DB_PASSWORD>@<DB_HOST>:<DB_PORT>/postgres?sslmode=require' > /var/lib/alloy/postgres_secretReplace:
- DB_PASSWORD — Password for the
db-o11yuser you created - DB_HOST — Hostname or IP of your PostgreSQL server
- DB_PORT — PostgreSQL port (default: 5432)
- DB_PASSWORD — Password for the
Add the core Database Observability blocks to your Alloy configuration file. These configure the PostgreSQL exporter for metrics and the Database Observability component for query-level logs:
local.file "postgres_secret" { filename = "/var/lib/alloy/postgres_secret" is_secret = true } prometheus.exporter.postgres "postgres" { data_source_names = [local.file.postgres_secret.content] enabled_collectors = ["stat_statements"] } database_observability.postgres "postgres" { data_source_name = local.file.postgres_secret.content forward_to = [loki.relabel.database_observability_postgres.receiver] targets = prometheus.exporter.postgres.postgres.targets enable_collectors = ["query_details", "query_samples", "schema_details", "explain_plans"] exclude_users = ["db-o11y"] }Add the relabeling rules and Prometheus scrape configuration:
loki.relabel "database_observability_postgres" { forward_to = [loki.write.logs_service.receiver] rule { target_label = "instance" replacement = "<DB_HOST>:<DB_PORT>" } } discovery.relabel "database_observability_postgres" { targets = database_observability.postgres.postgres.targets rule { target_label = "job" replacement = "integrations/db-o11y" } rule { target_label = "instance" replacement = "<DB_HOST>:<DB_PORT>" } } prometheus.scrape "database_observability_postgres" { targets = discovery.relabel.database_observability_postgres.output forward_to = [prometheus.remote_write.metrics_service.receiver] }In the relabeling blocks you just added, replace the
DB_HOSTandDB_PORTplaceholder values with your PostgreSQL host and port.Add the Prometheus remote write and Loki write blocks. Find your Grafana Cloud credentials in your stack’s configuration page:
prometheus.remote_write "metrics_service" { endpoint { url = "https://prometheus-<region>.grafana.net/api/prom/push" basic_auth { username = "<METRICS_USER>" password = "<METRICS_API_KEY>" } } } loki.write "logs_service" { endpoint { url = "https://logs-<region>.grafana.net/loki/api/v1/push" basic_auth { username = "<LOGS_USER>" password = "<LOGS_API_KEY>" } } }Replace the region, usernames, and API keys with your Grafana Cloud stack values.
You’ve configured Grafana Alloy with all the Database Observability components for PostgreSQL.
In the next milestone, you’ll verify that telemetry is flowing to Grafana Cloud.