# Changing datasource programmatically

**URL:** <https://community.grafana.com/t/changing-datasource-programmatically/122930>\
**Category:** Dashboards\
**Tags:** dashboard-variables, datasource\
**Created:** [May 31, 2024, 8:24am UTC](https://community.grafana.com/t/changing-datasource-programmatically/122930 "2024-05-31T08:24:51Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![RocketSurgeon](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@RocketSurgeon](https://community.grafana.com/u/RocketSurgeon)\
**Post date:** [May 31, 2024, 8:24am UTC](https://community.grafana.com/t/changing-datasource-programmatically/122930/1 "2024-05-31T08:24:51Z")

</div>

Hey guys,

I’m trying to work out a way to automatically change a datasource rather than having to have a user select from a dropdown.

I have a query variable which is happily deducing the correct data source name depending on timescale, as an arbitrary string, but there seems to be a functionality gap from having that as a “normal” variable to being able to use it as a datasource.

I know if I were to instead return the UUID of the datasource then I can hack that variable in to the dashboard JSON file and it works, but that seems way too off the wall a solution to use in a professional, multiuser environment.

Can anyone suggest any only half mad ways that I change the value of a datasource dashboard variable without any direct user input?

If I could trigger a reload of the page with a hand-crafted url which includes a datasource variable then I see that would probably, however I doubt there’s a viable to way to get from “user zoomed out past 30 days” to “reload this page”.

I am running with HTML sanitize disabled, so actually, maybe some sort of javascript solution coooould work?

---

<div class="post-metadata">

**Author:** ![jangaraj](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/jangaraj/32/39197_2.png) [@jangaraj](https://community.grafana.com/u/jangaraj)\
**Post date:** [May 31, 2024, 10:07am UTC](https://community.grafana.com/t/changing-datasource-programmatically/122930/2 "2024-05-31T10:07:13Z")

</div>

> [@RocketSurgeon](#):
>
> I have a query variable which is happily deducing the correct data source name depending on timescale

That sounds as overengineered solution. Why you just don’t query all datasources and just merge their results into ingle timeserie? Then you don’t need to care about timescales and datasources. Keep it simple.

---

<div class="post-metadata">

**Author:** ![RocketSurgeon](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@RocketSurgeon](https://community.grafana.com/u/RocketSurgeon)\
**Post date:** [May 31, 2024, 10:13am UTC](https://community.grafana.com/t/changing-datasource-programmatically/122930/3 "2024-05-31T10:13:00Z")

</div>

Because I’d be doing vastly more query work for everything I do?

The point of the timescale is to switch to a downsampled database when I’m after old data or over a long time period. I don’t want to query a 10000 10s interval records if I can instead just query 333 5m interval records.

Your suggesting of “just” querying BOTH everytime seems… odd…? There is no “simple” benefit here as far as I can tell, just significantly increased system load.

---

<div class="post-metadata">

**Author:** ![yosiasz](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/yosiasz/32/45847_2.png) [@yosiasz](https://community.grafana.com/u/yosiasz)\
**Post date:** [May 31, 2024, 10:27am UTC](https://community.grafana.com/t/changing-datasource-programmatically/122930/4 "2024-05-31T10:27:00Z")

</div>

what are your data sources? mssql? mysql? or …

---

<div class="post-metadata">

**Author:** ![RocketSurgeon](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@RocketSurgeon](https://community.grafana.com/u/RocketSurgeon)\
**Post date:** [May 31, 2024, 10:38am UTC](https://community.grafana.com/t/changing-datasource-programmatically/122930/5 "2024-05-31T10:38:31Z")

</div>

It’s all influxdb v3. I know it’s not possible to do something like referencing different databases inside the queries, it has to be a datasource change, so (as I see it) data source type agnostic.

---

<div class="post-metadata">

**Author:** ![yosiasz](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/yosiasz/32/45847_2.png) [@yosiasz](https://community.grafana.com/u/yosiasz)\
**Post date:** [May 31, 2024, 10:40am UTC](https://community.grafana.com/t/changing-datasource-programmatically/122930/6 "2024-05-31T10:40:40Z")

</div>

absolutely doable if it was mssql and different databases even across differrebt servers via linked server but not sure about influxdb

---

<div class="post-metadata">

**Author:** ![RocketSurgeon](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@RocketSurgeon](https://community.grafana.com/u/RocketSurgeon)\
**Post date:** [May 31, 2024, 10:45am UTC](https://community.grafana.com/t/changing-datasource-programmatically/122930/7 "2024-05-31T10:45:13Z")

</div>

No, it’s not possible at the moment. It is in the v1 and v2 platforms but not in v3, so gotta switch the datasource. I have this working so simply in v2 with a Flux query, but elsewhere, it’s not proving as simple.

---

<div class="post-metadata">

**Author:** ![jangaraj](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/jangaraj/32/39197_2.png) [@jangaraj](https://community.grafana.com/u/jangaraj)\
**Post date:** [May 31, 2024, 11:16am UTC](https://community.grafana.com/t/changing-datasource-programmatically/122930/8 "2024-05-31T11:16:20Z")

</div>

> [@RocketSurgeon](#):
>
> I don’t want to query a 10000 10s interval records if I can instead just query 333 5m interval records

But why you need raw data, when you can have time aggregated data based on current dashboard timerange?

E.g. InfluxQL (InfluxDB v1):

```auto
SELECT mean("availability") 
FROM "tests" 
WHERE $timeFilter 
GROUP BY time($__interval)

```

That `$__interval` is magical variable - it depends on selected timerange, e.g. you select 1 year time range, so it can be 1month, you select 1 day timerange - it can be 1hour

---

<div class="post-metadata">

**Author:** ![RocketSurgeon](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@RocketSurgeon](https://community.grafana.com/u/RocketSurgeon)\
**Post date:** [May 31, 2024, 11:40am UTC](https://community.grafana.com/t/changing-datasource-programmatically/122930/9 "2024-05-31T11:40:10Z")

</div>

Because we downsample our data after 30 days, so we have a month of 10s data and over a year of 5m data.

That GROUP BY is still expensive, it still takes time and load. More time the wider the time range. Grouping (which I’m absolutely already doing) 330 rows is quicker than grouping 10000.

So if we’re past that, what options do we have to change the data source without user interaction?

---

<div class="post-metadata">

**Author:** ![jangaraj](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/jangaraj/32/39197_2.png) [@jangaraj](https://community.grafana.com/u/jangaraj)\
**Post date:** [May 31, 2024, 11:51am UTC](https://community.grafana.com/t/changing-datasource-programmatically/122930/10 "2024-05-31T11:51:42Z")

</div>

> [@RocketSurgeon](#):
>
> That GROUP BY is still expensive

In theory yes, but it can be a few ms in absolute value - do you have numbers how much slower will it be in your worst case? IMHO it’s not worth to have datasource switching just to save \<500 ms - standard users won’t notice any significant difference (unless you have case for it).

---

<div class="post-metadata">

**Author:** ![RocketSurgeon](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@RocketSurgeon](https://community.grafana.com/u/RocketSurgeon)\
**Post date:** [May 31, 2024, 12:24pm UTC](https://community.grafana.com/t/changing-datasource-programmatically/122930/11 "2024-05-31T12:24:50Z")

</div>

Again, we downsample data after 30 days for storage reasons, so we need to use a different data source for older ranges. I’m not interested to merging two databases together and massively increasing system load and complexity of 50 panels. I’m really confused why you are going back to this again rather than answering the question I’m asking?

How can I change the datasource without user interaction?

---

<div class="post-metadata">

**Author:** ![yosiasz](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/yosiasz/32/45847_2.png) [@yosiasz](https://community.grafana.com/u/yosiasz)\
**Post date:** [May 31, 2024, 3:39pm UTC](https://community.grafana.com/t/changing-datasource-programmatically/122930/12 "2024-05-31T15:39:57Z")

</div>

so what would trigger the change if not the user?

---

<div class="post-metadata">

**Author:** ![pooh](https://avatars.discourse-cdn.com/v4/letter/p/f05b48/32.png) [@pooh](https://community.grafana.com/u/pooh)\
**Post date:** [May 31, 2024, 4:02pm UTC](https://community.grafana.com/t/changing-datasource-programmatically/122930/13 "2024-05-31T16:02:22Z")

</div>

I think the OP is trying to get Grafana to use data source A if the timescale  
for the query is within the last 30 days, and data source B if the timescale  
is older than that.

Antony.

---

<div class="post-metadata">

**Author:** ![RocketSurgeon](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@RocketSurgeon](https://community.grafana.com/u/RocketSurgeon)\
**Post date:** [May 31, 2024, 4:08pm UTC](https://community.grafana.com/t/changing-datasource-programmatically/122930/14 "2024-05-31T16:08:03Z")

</div>

Yep, that’s it. Various things trigger on timescale changes, so I can get the right UUID or something, I just can’t, say, use a UUID that’s stored a variable as the datasource in a panel, as it’s not a “datasource” variable, and I presume there’s no way to make it one.

---

<div class="post-metadata">

**Author:** ![yosiasz](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/yosiasz/32/45847_2.png) [@yosiasz](https://community.grafana.com/u/yosiasz)\
**Post date:** [May 31, 2024, 4:12pm UTC](https://community.grafana.com/t/changing-datasource-programmatically/122930/15 "2024-05-31T16:12:36Z")

</div>

this might be possible but you will have to use a custom time picker with key/value

the key would be datasource\_uid, value would be the datetime

the wheels for this approach will quickly fall off if/when you have a new datasource in the mix.

question for you @RocketSurgeon, are you using flux query language or influxql, if already mentioned above sorry to ask again.

---

<div class="post-metadata">

**Author:** ![RocketSurgeon](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@RocketSurgeon](https://community.grafana.com/u/RocketSurgeon)\
**Post date:** [May 31, 2024, 4:43pm UTC](https://community.grafana.com/t/changing-datasource-programmatically/122930/16 "2024-05-31T16:43:01Z")

</div>

I’m using FlightSQL / SQL principally in this situation, annoyingly if Flux worked on v3 then it would be very simple, but it’s deprecated.

---

<div class="post-metadata">

**Author:** ![yosiasz](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/yosiasz/32/45847_2.png) [@yosiasz](https://community.grafana.com/u/yosiasz)\
**Post date:** [May 31, 2024, 6:21pm UTC](https://community.grafana.com/t/changing-datasource-programmatically/122930/17 "2024-05-31T18:21:46Z")

</div>

in mssql you can call a table in another database (B) from database A

Is that possible in your sql language?

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/b/e/be6e0259cca90e2ed76ed6ae4b71ef3108324e1e.png)

---

<div class="post-metadata">

**Author:** ![RocketSurgeon](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@RocketSurgeon](https://community.grafana.com/u/RocketSurgeon)\
**Post date:** [May 31, 2024, 7:02pm UTC](https://community.grafana.com/t/changing-datasource-programmatically/122930/18 "2024-05-31T19:02:54Z")

</div>

No, they need to be specific datasources by design.

---

<div class="post-metadata">

**Author:** ![yosiasz](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/yosiasz/32/45847_2.png) [@yosiasz](https://community.grafana.com/u/yosiasz)\
**Post date:** [May 31, 2024, 7:47pm UTC](https://community.grafana.com/t/changing-datasource-programmatically/122930/19 "2024-05-31T19:47:15Z")

</div>

I am talking about within the datasource query space provided to add your query, can you query another database?

why was there a need to separate databases for older data? what is the technical reason behind this decision?

---

<div class="post-metadata">

**Author:** ![RocketSurgeon](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@RocketSurgeon](https://community.grafana.com/u/RocketSurgeon)\
**Post date:** [May 31, 2024, 9:48pm UTC](https://community.grafana.com/t/changing-datasource-programmatically/122930/20 "2024-05-31T21:48:11Z")

</div>

I’m really surprised that it’s deemed relevant to dig into this minutiae. We have a 10s database and 5m database. InfluxDB, as a time series database, has a concept of a retention period and automatically purges “old” data, so it’s standard to separate data by how long you want to retain it for.

So yet again, no, I can not query another database. Hence why I posted in the first place.

[Next page](https://community.grafana.com/t/changing-datasource-programmatically/122930.md?page=2)
