Skip to main content

PostgreSQL Exporter Setup (with Alloy + Grafana Cloud Integration)

This guide documents the verified working configuration for installing and connecting Prometheus PostgreSQL Exporter to Grafana Alloy, which forwards metrics to Grafana Cloud.


1. Download & Install

cd /tmp
wget https://github.com/prometheus-community/postgres_exporter/releases/download/v0.9.0/postgres_exporter-0.9.0.linux-amd64.tar.gz
tar -xvf postgres_exporter-0.9.0.linux-amd64.tar.gz
sudo mv postgres_exporter-0.9.0.linux-amd64/postgres_exporter /usr/local/bin/postgres_exporter
sudo chmod +x /usr/local/bin/postgres_exporter

Verify:

/usr/local/bin/postgres_exporter --version

2. Create Environment File

This step is mandatory — systemd cannot safely parse passwords that contain special characters (like #), so we define the connection string in a standalone file.

Create /etc/postgres_exporter.env:

DATA_SOURCE_NAME="host=<db-hostname> user=<db-username> password=<db-password> 
dbname=<db-name> port=<db-port> sslmode=require"

Important notes:

  • The entire value must be wrapped in double quotes.
  • Azure PostgreSQL requires SSL, so keep sslmode=require.
  • If your password has #, @, or spaces, quoting prevents systemd from truncating it.

3. Create Systemd Unit

Create /etc/systemd/system/postgres-exporter.service:

[Unit]
Description=Prometheus PostgreSQL Exporter
After=network.target

[Service]
EnvironmentFile=/etc/postgres_exporter.env
ExecStart=/usr/local/bin/postgres_exporter
Restart=always

[Install]
WantedBy=multi-user.target

Then enable and start:

sudo systemctl daemon-reload
sudo systemctl enable --now postgres-exporter
sudo systemctl status postgres-exporter

4. Verify Exporter Metrics

Run:

curl -s http://127.0.0.1:9187/metrics | grep -E 'pg_|postgres' | head

Expected output example:

pg_up 1
pg_database_size_bytes{datname="prezence"} 123456789
pg_stat_database_tup_returned{datname="prezence"} 987654

If you only see go_ metrics:

  • Check /etc/postgres_exporter.env quoting.
  • Ensure the password wasn’t truncated by systemd.
  • Confirm your Azure firewall allows the VM IP.

5. Add to Grafana Alloy

If you’re using Grafana Alloy to forward metrics to Grafana Cloud, edit the configuration file:

/etc/alloy/config.alloy

Append the following to your Alloy configuration file (keeping the same style used for Redis/Celery exporters):

prometheus.scrape "postgres" {
targets = [
{ __address__ = "127.0.0.1:9187", },
]
metrics_path = "/metrics"
forward_to = [prometheus.relabel.metrics_instance.receiver]
}

Restart Alloy:

sudo systemctl restart alloy

Check Status:

sudo systemctl status alloy

6. Verify in Grafana Cloud

In Grafana Cloud → Explore, run:

up{__address__="127.0.0.1:9187"}

or check a Postgres metric:

pg_up

You should see 1 (meaning exporter is up and reporting).


7. Troubleshooting

IssueLikely CauseFix
Only go_ metricsDATA_SOURCE_NAME not loaded or truncatedRecheck /etc/postgres_exporter.env quotes
Auth or SSL errorAzure requires TLSKeep sslmode=require
Port 9187 busyService already runningStop service before manual test
No metrics in GrafanaAlloy not restartedRestart Alloy and re-verify scrape config

Final Verification Checklist

CheckCommandExpected
Exporter binary installed/usr/local/bin/postgres_exporter --versionshows version 0.9.0
Service runningsystemctl status postgres-exporteractive (running)
Local metricscurl -s http://127.0.0.1:9187/metricscontains pg_ metrics
Grafana Cloud receivingquery pg_upvalue = 1