Grafana Cloud Enterprise Open source

PostgreSQL alerting

You can use Grafana Cloud to avoid installing, maintaining, and scaling your own instance of Grafana. Create a free account to get started, which includes free forever access to 10k metrics, 50GB logs, 50GB traces, 500VUh k6 testing & more.

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.

Before you begin

Supported query format

Only time series queries can be used in alert rule conditions.

  • Your query must return a column named time (native SQL date/time or UNIX epoch) and one or more numeric value columns.
  • Table formatted queries are not supported in alert rule conditions.

For details on writing time series queries, refer to Time series queries in the PostgreSQL query editor.

Create an alert rule

To create an alert rule that uses PostgreSQL:

  1. Go to Alerting (bell icon) in the left menu and select Alert rules.
  2. Click New alert rule.
  3. In the query section, select your PostgreSQL data source.
  4. Set the query Format to Time series.
  5. Write a SQL query that returns a time column and numeric value(s).
  6. Configure the condition, evaluation group, and notification settings.
  7. Save the rule.

For step-by-step guidance, refer to Create a Grafana-managed alert rule.

Example time series query for alerting

The following query returns a time series suitable for a threshold alert (e.g. alert when value exceeds a limit):

SQL
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.

Template annotations and labels

You can use template annotations and labels to include query results or metadata in alert notifications and labels.