---
title: "Configure Grafana Alloy | Grafana Labs"
description: "Use the Add a database wizard in Database Observability to generate the Grafana Alloy configuration for PostgreSQL, then deploy it to your Alloy host."
---

> For a curated documentation index, see [llms.txt](/llms.txt). For the complete documentation index, see [llms-full.txt](/llms-full.txt).

# Configure Grafana Alloy

In this milestone, you’ll use the **Add a database** wizard in Database Observability to generate the Grafana Alloy configuration for your PostgreSQL database, then deploy it on the machine where Alloy runs. Alloy collects both metrics and structured logs from your database and forwards them to Grafana Cloud.

Before you begin, install Grafana Alloy 1.17.0 or later on a host that can reach both your PostgreSQL server and Grafana Cloud. Refer to [Install Grafana Alloy](/docs/alloy/latest/set-up/install/) for your platform. Database Observability is generally available, so you don’t need a `--stability.level` flag. Alloy should connect directly to the database host, not through a load balancer or PgBouncer.

The wizard offers two deployment methods. This path uses **Manual configuration**, which works with any Alloy you run yourself. If your collector is enrolled in Fleet Management, you can choose **Fleet Management** instead and Grafana Cloud pushes the configuration to it for you.

To start the wizard and choose how to deploy, complete the following steps:

1. Open **Observability** &gt; **Database** &gt; **Configuration**.
2. On the **Setup** tab, click **Add a database**.
3. Select **PostgreSQL**.
4. Click **Next**.
5. Select **Manual configuration**.
6. Click **Next**.

You’ve chosen PostgreSQL and manual deployment. The wizard now asks for the details it needs to generate your configuration.

To generate the configuration, complete the following steps:

1. Enter a **Database name**. It becomes the Alloy component identifier, so use only letters, numbers, and underscores.
2. Leave **DSN storage method** set to environment variable, then click **Auto-fill from pipeline name** to set the **Environment variable name** Alloy reads the connection string from. Note the name — you’ll set this variable on the Alloy host.
3. Optionally set an **Instance label** to identify this database in Grafana Cloud. If you leave it empty, the instance is identified by the `host:port` from your connection string.
4. Click **Next**.
5. Review the generated configuration on **Deploy configuration**, then click **Copy to clipboard** (or **Download as .alloy file**).

You’ve generated a complete Alloy configuration: the PostgreSQL exporter with `stat_statements` and autodiscovery of logical databases, the `database_observability.postgres` component with query details, samples, schema details, and explain plans enabled, relabeling, and the Grafana Cloud metrics and logs writers, all wired together.

To deploy the configuration on the host where Alloy runs, complete the following steps:

1. Paste the copied configuration into your Alloy configuration file, for example `config.alloy`. If the file already contains Database Observability blocks for this database, replace them so the database isn’t collected twice.
2. Set the environment variables the configuration reads. The DSN variable name is the one the wizard generated (`DB_POSTGRES_` + your database name in uppercase + `_DSN`):
   
   Bash ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy
   
   ```bash
   export DB_POSTGRES_POSTGRES_PRIMARY_DSN='postgresql://db-o11y:YOUR_DB_O11Y_PASSWORD@YOUR_DB_HOST:YOUR_DB_PORT/postgres?sslmode=require'
   export GCLOUD_HOSTED_METRICS_URL='YOUR_PROMETHEUS_REMOTE_WRITE_URL'
   export GCLOUD_HOSTED_METRICS_ID='YOUR_PROMETHEUS_INSTANCE_ID'
   export GCLOUD_HOSTED_LOGS_URL='YOUR_LOKI_WRITE_URL'
   export GCLOUD_HOSTED_LOGS_ID='YOUR_LOKI_INSTANCE_ID'
   export GCLOUD_RW_API_KEY='YOUR_GRAFANA_CLOUD_TOKEN'
   ```
   
   Replace:
   
   - `YOUR_DB_O11Y_PASSWORD`, `YOUR_DB_HOST`, `YOUR_DB_PORT` — the `db-o11y` password and your PostgreSQL host and port (default `5432`). Keep `postgres` as the logical database in the DSN; autodiscovery finds the others.
   - `YOUR_PROMETHEUS_REMOTE_WRITE_URL` / `YOUR_PROMETHEUS_INSTANCE_ID` — from your stack’s Prometheus details in Grafana Cloud
   - `YOUR_LOKI_WRITE_URL` / `YOUR_LOKI_INSTANCE_ID` — from your stack’s Loki details in Grafana Cloud
   - `YOUR_GRAFANA_CLOUD_TOKEN` — a Grafana Cloud access policy token with write permissions
   
   How you set variables depends on how you run Alloy. For the Linux service, add them to its environment file (`/etc/default/alloy` on Debian-based systems, `/etc/sysconfig/alloy` on RedHat or SUSE-based systems); for containers, pass them in the container environment.
3. Restart Alloy so it loads the new configuration. On Linux with systemd:
   
   Bash ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy
   
   ```bash
   sudo systemctl restart alloy
   ```
   
   Check the Alloy logs for errors connecting to PostgreSQL or to Grafana Cloud.

You’ve deployed the Database Observability configuration to Alloy. Alloy now connects to PostgreSQL as `db-o11y`, scrapes `pg_stat_statements` metrics, and streams query telemetry to Grafana Cloud.

In the next milestone, you’ll verify that telemetry is flowing to Grafana Cloud.
