I’m using Grafana with PostgreSQL as a data source. I’ve created a custom variable custom_sql
that I use in my panels, for example, a bar chart, where I define it as: ${custom_sql}
. This setup allows me to use a Telegram bot that accepts custom SQL queries as requests and returns a rendered graph in response.
This system works fine with general queries such as:
SELECT * FROM users
WHERE post_count > 10
However, it doesn’t work with any query that includes text literals. For example:
SELECT * FROM users
WHERE username = 'someUsername'
The Query Inspector shows that Grafana processes my request as:
SELECT * FROM users
WHERE username = ''someUsername''
Grafana is adding additional quotation marks around the text literal, causing the database to fail to find the specified user.
I’ve tried specifying the type explicitly, like this:
WHERE username = someUsername:text
But that doesn’t work either.
I also attempted replacing the username with a variable, but it seems Grafana doesn’t allow using a variable inside another variable.
I couldn’t find any resources or discussions about achieving the same result via the Grafana API. I like how the system works with general queries, and I’d love to find a way to handle these more complex queries correctly.
Adriano Visoccini is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2