How to use table name filter in grafana PostgreSQL

Hello Grafana Community,

I’m currently working with a PostgreSQL database that contains 5 tables named: wmz_a, wmz_b, wmz_c, wmz_d, and wmz_e. Each of these tables shares the same structure, hosting 5 columns: timestamps, h, c, q, and e.

To streamline my Grafana dashboard, I’ve created a custom variable named heat_meters to dynamically select among these tables (wmz_a, wmz_b, wmz_c, wmz_d, and wmz_e). My goal is to utilize this variable within my SQL queries to fetch data accordingly.

However, I’m encountering difficulty in correctly formulating the SQL query to dynamically select the table name based on the heat_meters variable. My current SQL query structure is as follows:
timestamps as time,
h as heat,
FROM ‘$heat_meters’
ORDER BY timestamps

Despite my efforts, the dynamic table selection is not functioning as expected. I would deeply appreciate any guidance, tips, or solutions on how to correctly implement this dynamic selection within my SQL query for Grafana. Your assistance will be invaluable to me.

Thank you in advance for your support and contributions!

Welcome @polepallymanikanta

This question is related to postgres and not to grafana

Best to ask it on a postgres forum.
What you are looking for is dynamic query

Thank you for your quick response. I’m currently utilizing PostgreSQL as my data source within Grafana, and I’m in the process of writing SQL queries to extract the necessary data for my dashboards.

Specifically, I’m looking to understand the best way to incorporate custom table variable names into the FROM section of my SQL queries. This is part of a broader attempt to make my dashboards more dynamic and adaptable to changes in the underlying data structure.

Could anyone provide insights or examples on how to successfully implement custom table variables in the FROM clause of SQL queries used in Grafana with a PostgreSQL data source?

Thank you in advance for your assistance!


  timestamps as time,
  h as heat,
FROM ${heat_meters:raw} 

Use query debugger to see how variables are interpolated and improve query until it generates valid SQL syntax for your needs.

1 Like