Skip to content.
Joe Heaton's Webzone
🥐 ☕
Online since 2011

TimescaleDB SQL in Grafana

Grafana supports SQL through MySQL/MariaDB, PostgreSQL & SQL Server. Additionally Grafana supports the PostgreSQL extension TimescaleDB, this database is the best fit of all the SQL databases for Grafana since it is built specifically for large-scale time-series datasets.

Below is an example of a Timescale query using time_bucket_gapfill function from Timescale to pad missing values. You will also need to populate or swap out the Grafana templating variables I used, they save a lot of time once your dashboards start to grow.

SELECT time_bucket_gapfill('$step', time) AS "time",
    category AS category,
    avg(metric) AS metric
FROM $table
WHERE $__timeFilter(time)
AND metric IS NOT NULL
GROUP BY 1,2
ORDER BY 1,2

Where $step represents an amount of time to group the data by, such as 1month, 1day, 1minute.

Settings!