---
title: "Configure PostgreSQL exporter to generate Prometheus metrics | Grafana Cloud documentation"
description: "Configure PostgreSQL to send metrics"
---

# Configure PostgreSQL exporter to generate Prometheus metrics

To enable PostgreSQL for the Prometheus metrics, use the [Prometheus exporter for PostgreSQL](https://github.com/prometheus-community/postgres_exporter).

To configure PostgreSQL exporter to generate Prometheus metrics, perform the following steps:

1. Run the following command on a running instance of PostgreSQL to enable exporter using Docker.
   
   `1docker run \ 2 --net=host \ 3 -e DATA_SOURCE_NAME="postgresql://postgres:password@localhost:5432/postgres?sslmode=disable" \ 4 quay.io/prometheuscommunity/postgres-exporter`
2. To confirm that the exporter is attached to the database instance, ensure the following metrics are available in Prometheus.
   
   - `pg_stat_database_numbackends`
   - `pg_stat_database_xact_commit`

## Request and error metrics

Expand table

| **Metric**                                                                                                                                                                 | **Key Performance Indicator (KPI)**                                                                                                                                                                          |
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Requests<br /><br /><br /><br />`pg_stat_database_xact_commit`<br /><br /><br /><br />`pg_stat_database_xact_rollback`<br /><br /><br /><br />`pg_stat_database_blks_read` | Request Rate<br /><br /><br /><br />`rate(pg_stat_database_xact_commit[5m])`<br /><br /><br /><br />`rate(pg_stat_database_xact_rollback[5m])`<br /><br /><br /><br />`rate(pg_stat_database_blks_read[5m])` |
| Errors<br /><br /><br /><br />`pg_stat_database_conflicts`                                                                                                                 | Error Ratio<br /><br /><br /><br />`rate(pg_stat_database_conflicts[5m])`/ `rate(pg_stat_database_xact_commit[5m])`                                                                                          |
| Latency<br /><br /><br /><br />`pg_stat_database_blks_hit + pg_stat_database_blks_read`                                                                                    | Latency Average<br /><br /><br /><br />`rate(pg_stat_database_blks_hit + pg_stat_database_blks_read[5m])`/ `rate(pg_stat_database_blk_read_time[5m])`                                                        |

## Resource metrics

Expand table

| **Metric**                                                          | **Key Performance Indicator (KPI)**                                                                                                                                                                                                   |
|---------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Connections<br /><br /><br /><br />`pg_stat_database_numbackends`   | Connections Usage<br /><br /><br /><br />`avg_over_time(pg_stat_database_numbackends[5m)`<br /><br /><br /><br />Connection usage against limit<br /><br /><br /><br />`pg_stat_database_numbackends` / `pg_settings_max_connections` |
| Database Disk Usage<br /><br /><br /><br />`pg_database_size_bytes` | Database disk usage limit<br /><br /><br /><br />`predict_linear(pg_database_size_bytes[1h])`                                                                                                                                         |

## Alerts

Expand table

| **KPI**             | **Alert**                                                                                                                                            |
|---------------------|------------------------------------------------------------------------------------------------------------------------------------------------------|
|                     |                                                                                                                                                      |
| Request Rate        | **RequestRateAnomaly**                                                                                                                               |
| Error Ratio         | **ErrorRatioBreach** and **ErrorBuildup** based on an availability SLO of `99.9`                                                                     |
| Latency Average     | **LatencyAverageBreach** and **LatencyAverageAnomaly**                                                                                               |
| Database Disk Usage | **ResourceMayExhaust**                                                                                                                               |
| Connections         | **ResourceRateAnomaly** and **Saturation** with severity level of `warning` and `critical` when memory utilization exceeds 80% and 90%, respectively |

### Failure alerts

- PostgresIsDown: Postgresql is not running
  
  pg\_up != 1
- PostgresGotRestarted: Postgresql server restarted
  
  time() - pg\_postmaster\_start\_time\_seconds &lt; 60
- PostgresHasHighDeadLocks: Postgresql is having too many deadlocks
  
  rate(pg\_stat\_database\_deadlocks\[1m]) * 60 &gt; 5
- PostgresHasExporterErrors: Exporter is not running properly
  
  pg\_exporter\_last\_scrape\_error &gt; 0

## Dashboards

The following dashboard shows key metrics such as:

- Uptime
- Transactions
- Active Session
- Max Connection

You can configure a custom dashboard if you require detailed information for each PostgreSQL Prometheus metric.
