Query SQL data sources
If you’re just getting started with Flux queries, check out the following:
- for a conceptual overview of Flux and parts of a Flux query.
- Execute queries to discover a variety of ways to run your queries.
To query a SQL data source:
- Import the
sql
package in your Flux query - Use the
sql.from()
function to specify the driver, data source name (DSN), and query used to query data from your SQL data source:
import "sql"
sql.from(
driverName: "mysql",
dataSourceName: "user:password@tcp(localhost:3306)/db",
query: "SELECT * FROM example_table",
)
import "sql"
sql.from(
driverName: "snowflake",
dataSourceName: "user:password@account/db/exampleschema?warehouse=wh",
query: "SELECT * FROM example_table",
)
// NOTE: InfluxDB OSS and InfluxDB Cloud do not have access to
// the local filesystem and cannot query SQLite data sources.
// Use the Flux REPL to query an SQLite data source.
import "sql"
sql.from(
driverName: "sqlite3",
dataSourceName: "file:/path/to/test.db?cache=shared&mode=ro",
query: "SELECT * FROM example_table",
import "sql"
sql.from(
driverName: "sqlserver",
dataSourceName: "sqlserver://user:password@localhost:1234?database=examplebdb",
query: "GO SELECT * FROM Example.Table",
)
For information about authenticating with SQL Server using ADO-style parameters, see SQL Server ADO authentication.
import "sql"
driverName: "awsathena",
dataSourceName: "s3://myorgqueryresults/?accessID=12ab34cd56ef®ion=region-name&secretAccessKey=y0urSup3rs3crEtT0k3n",
query: "GO SELECT * FROM Example.Table",
)
For information about parameters to include in the Athena DSN, see .
For information about authenticating with BigQuery, see BigQuery authentication parameters.
See the for information about required function parameters.
One of the primary benefits of querying SQL data sources from InfluxDB is the ability to enrich query results with data stored outside of InfluxDB.
Using the air sensor sample data below, the following query joins air sensor metrics stored in InfluxDB with sensor information stored in PostgreSQL. The joined data lets you query and filter results based on sensor information that isn’t stored in InfluxDB.
// Import the "sql" package
import "sql"
// Query data from PostgreSQL
sensorInfo = sql.from(
driverName: "postgres",
dataSourceName: "postgresql://localhost?sslmode=disable",
query: "SELECT * FROM sensors",
)
// Query data from InfluxDB
sensorMetrics = from(bucket: "example-bucket")
|> range(start: -1h)
|> filter(fn: (r) => r._measurement == "airSensors")
// Join InfluxDB query results with PostgreSQL query results
join(tables: {metric: sensorMetrics, info: sensorInfo}, on: ["sensor_id"])
Use sql.from()
to from SQL query results. The following example uses the air sensor sample data below to create a variable that lets you select the location of a sensor.
import "sql"
sql.from(
driverName: "postgres",
dataSourceName: "postgresql://localhost?sslmode=disable",
query: "SELECT * FROM sensors",
)
|> rename(columns: {location: "_value"})
|> keep(columns: ["_value"])
Use the variable to manipulate queries in your dashboards.
If your SQL database requires authentication, use to store and populate connection credentials. By default, InfluxDB base64-encodes and stores secrets in its internal key-value store, BoltDB. For added security, store secrets in Vault.
Use the or the influx CLI to store your database credentials as secrets.
curl --request PATCH http://localhost:8086/api/v2/orgs/<org-id>/secrets \
--header 'Authorization: Token YOURAUTHTOKEN' \
--header 'Content-type: application/json' \
--data '{
"POSTGRES_PASS": "example-password"
}'
To store secrets, you need:
# Syntax
influx secret update -k <secret-key>
# Example
influx secret update -k POSTGRES_PASS
When prompted, enter your secret value.
You can provide the secret value with the -v
, --value
flag, but the plain text secret may appear in your shell history.
influx secret update -k <secret-key> -v <secret-value>
Use secrets in your query
Import the influxdata/influxdb/secrets
package and use string interpolation to populate connection credentials with stored secrets in your Flux query.
The and sample sensor information simulate a group of sensors that measure temperature, humidity, and carbon monoxide in rooms throughout a building. Each collected data point is stored in InfluxDB with a sensor_id
tag that identifies the specific sensor it came from. Sample sensor information is stored in PostgreSQL.
Sample data includes:
Simulated data collected from each sensor and stored in the
airSensors
measurement in InfluxDB:- temperature
- humidity
- co
Information about each sensor stored in the
sensors
table in PostgreSQL:- sensor_id
- location
- model_number
- last_inspected
Download sample air sensor data
Create an InfluxDB task and use the to download sample air sensor data every 15 minutes. Write the downloaded sample data to your new bucket:
import "influxdata/influxdb/sample"
option task = {name: "Collect sample air sensor data", every: 15m}
sample.data(set: "airSensor")
|> to(org: "example-org", bucket: "example-bucket")
Query your target bucket after the first task run to ensure the sample data is writing successfully.
from(bucket: "example-bucket")
|> range(start: -1m)
|> filter(fn: (r) => r._measurement == "airSensors")
Import the sample sensor information
Download the sample sensor information CSV.
Use a PostgreSQL client (
psql
or a GUI) to create thesensors
table:CREATE TABLE sensors (
sensor_id character varying(50),
location character varying(50),
model_number character varying(50),
last_inspected date
);
Import the downloaded CSV sample data. Update the
FROM
file path to the path of the downloaded CSV sample data.COPY sensors(sensor_id,location,model_number,last_inspected)
FROM '/path/to/sample-sensor-info.csv' DELIMITER ',' CSV HEADER;
Query the table to ensure the data was imported correctly:
Import the sample data dashboard
Download and import the Air Sensors dashboard to visualize the generated data:
For information about importing a dashboard, see Create a dashboard.