CockroachDB template variables
Template variables let you create dynamic, reusable dashboards. Instead of hard-coding values like database names, table names, or regions into your queries, you can use variables that appear as drop-down menus at the top of the dashboard. When a user changes a variable’s value, every panel that references it updates automatically.
Before you begin
- Configure the CockroachDB data source.
- Familiarize yourself with Grafana template variables.
Supported variable types
The CockroachDB data source supports the following variable types.
Create a query variable
Query variables populate their options by running a SQL query against your CockroachDB instance. The query must return a single column – each row becomes an option in the variable drop-down.
To create a query variable:
- Navigate to Dashboard settings > Variables.
- Click Add variable.
- Select Query as the variable type.
- Select your CockroachDB data source.
- Enter a SQL query that returns the values you want.
- Optionally, set Refresh to control when the variable re-runs its query:
- On dashboard load: Re-runs the query each time the dashboard loads.
- On time range change: Re-runs the query when the dashboard time range changes.
- Click Apply.
Variable query examples
The following examples show common SQL queries for populating variables.
List all databases
SELECT database_name FROM information_schema.databasesList all tables
SELECT table_name FROM information_schema.tables
WHERE table_schema = 'public'List distinct values from a column
Useful for building region, environment, or status selectors:
SELECT DISTINCT region FROM clusters ORDER BY regionCascading variables
You can reference one variable inside another variable’s query to create dependent drop-down menus. For example, if you have a $database variable, you can create a $table variable that lists tables for the selected database:
SELECT table_name FROM information_schema.tables
WHERE table_catalog = '$database'
AND table_schema = 'public'When the user changes the $database selection, the $table drop-down automatically updates.
List CockroachDB cluster nodes
SELECT DISTINCT node_id::text FROM crdb_internal.gossip_nodes ORDER BY 1Use variables in queries
After creating variables, reference them in your CockroachDB panel queries using the $variable syntax.
Single-value variable
When a variable holds one value at a time, use it directly in a comparison:
SELECT created_at AS time, temperature
FROM sensor_readings
WHERE region = '$region'
AND $__timeFilter(created_at)
ORDER BY created_at ASCMulti-value variable
When a variable allows multiple selections, use IN to match any of the selected values:
SELECT created_at AS time, temperature, region
FROM sensor_readings
WHERE region IN ($region)
AND $__timeFilter(created_at)
ORDER BY created_at ASCVariable in a GROUP BY clause
You can also use variables to control grouping dynamically:
SELECT
date_trunc('$interval', created_at) AS time,
AVG(response_time_ms) AS avg_response_time
FROM requests
WHERE $__timeFilter(created_at)
GROUP BY time
ORDER BY time ASCIn this example, $interval is a custom variable with options like minute, hour, and day, letting users control the time granularity.
For more information about variable syntax, refer to Variable syntax.



