docs/sources/datasources/postgres/alerting/index.md
The PostgreSQL data source supports Grafana Alerting. You can create alert rules that evaluate time series queries against your PostgreSQL database and send notifications when conditions are met.
Only time series queries can be used in alert rule conditions.
time (native SQL date/time or UNIX epoch) and one or more numeric value columns.For details on writing time series queries, refer to Time series queries in the PostgreSQL query editor.
Keep the following limitations in mind when using PostgreSQL with Grafana Alerting:
$__timeGroup (for example, $__timeGroup(col, '5m', 0)) isn't applied during alert evaluation. Missing data points remain as gaps.To create an alert rule that uses PostgreSQL:
time column and one or more numeric values.For step-by-step guidance, refer to Create a Grafana-managed alert rule.
The following examples show common PostgreSQL alerting patterns.
The following query returns a time series suitable for a threshold alert (for example, alert when average value exceeds a limit):
SELECT
$__timeGroupAlias("time_date_time", '5m'),
avg("value_double") AS value
FROM test_data
WHERE $__timeFilter("time_date_time")
GROUP BY time
ORDER BY time
Use condition types such as Is above or Is below in the alert rule to evaluate the series.
To create separate alert instances for each value of a label column, include a string column in your query. Grafana creates one alert instance per unique label value, so you can monitor each host, service, or region independently.
SELECT
$__timeGroupAlias("time_date_time", '5m'),
avg("value_double") AS value,
hostname
FROM test_data
WHERE $__timeFilter("time_date_time")
GROUP BY time, hostname
ORDER BY time
This query produces a separate time series for each hostname. The alert rule evaluates the condition against each series independently, so you receive individual notifications per host.
To alert when the number of rows in a time window exceeds a threshold (for example, error count):
SELECT
$__timeGroupAlias("created_at", '5m'),
count(*) AS error_count
FROM error_logs
WHERE $__timeFilter("created_at")
AND severity = 'ERROR'
GROUP BY time
ORDER BY time
You can use template annotations and labels to include query results or metadata in alert notifications and labels.
Follow these best practices when using PostgreSQL for alerting:
WHERE clauses.$__timeFilter: Always include a $__timeFilter or $__unixEpochFilter macro to limit data to the evaluation window. Without it, the query scans the entire table on every evaluation.NULL values: If your data contains NULL values, Grafana treats them as "no data." Configure the alert rule's No data behavior to match your expectations (for example, Alerting, No data, or OK).