Switch Datasources on the Fly

Hi Dear Community !

We are working with Grafana to display performance data that is stored on a single Postgres Database.
The use case we have is that users will be processing performance data from different clusters using an internal tool we have created. That data will land in the same DB but we have a key on the tables that identify uniquely each cluster and in Grafana the user can select the cluster he wants to work on.

We believe that this solution is not ideal and we would like to create an independent DB for every cluster. The DB will be created on the fly by our internal tool.

The problem we are facing is on tha Grafana side and is: How can we make the datasource “selectable” from the dashboards we have OR how can generate a link on teh fly that would switch to the newly created datasource.

Our big problem is that we don’t know how can we work with multiple datasources on Grafana efficiently in order to respond to our particular use case.

We have done some research and we found out that there are API’s available to create/modify datasources but if we are going to have multiple users using the same Grafana frontent how can each user look at his own datasource without affecting the others.

Thank you so much !


I hope to understand well, and be able to help you.
Grafana has an option in which you can choose the datasource on the Flay.
In order to use this feature you must first create all your datasources, you can do it by hand or automate this a bit.

Once this is done, you can create a variable of type datasource, which will allow you to exchange between the different connections to postgres.

the other thing that you have to do, is in the querys tab, where you should not choose the normal datasource, but the variable $datasource.

Once this is done, you can easily choose which datasource to use for the panels.


The problem is that they are not created dynamically, but if I understood correctly, this will help you a bit.

Thank you so much Daniel ! Muchas Gracias !

I didn’t know we could do that and we will look at that option.
Basically we have a python script that does most of the heavy work.
We could do the following in our tool:

1 - Create the PostGresDB from the Python tool
2 - Create the Grafana DS based on the newly created DB from Python using the Grafana APIs
3 - Change all our Dashboards to include that new variable

I think we have plan now.

Thanks a lot !

1 Like