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:

    1. Import the sql package in your Flux query
    2. Use the sql.from() function to specify the driver, data source name (DSN), and query used to query data from your SQL data source:

    MySQL SQLite Athena

    1. import "sql"
    2. sql.from(
    3. driverName: "mysql",
    4. dataSourceName: "user:password@tcp(localhost:3306)/db",
    5. query: "SELECT * FROM example_table",
    6. )
    1. import "sql"
    2. sql.from(
    3. driverName: "snowflake",
    4. dataSourceName: "user:password@account/db/exampleschema?warehouse=wh",
    5. query: "SELECT * FROM example_table",
    6. )
    1. // NOTE: InfluxDB OSS and InfluxDB Cloud do not have access to
    2. // the local filesystem and cannot query SQLite data sources.
    3. // Use the Flux REPL to query an SQLite data source.
    4. import "sql"
    5. sql.from(
    6. driverName: "sqlite3",
    7. dataSourceName: "file:/path/to/test.db?cache=shared&mode=ro",
    8. query: "SELECT * FROM example_table",
    1. import "sql"
    2. sql.from(
    3. driverName: "sqlserver",
    4. dataSourceName: "sqlserver://user:password@localhost:1234?database=examplebdb",
    5. query: "GO SELECT * FROM Example.Table",
    6. )

    For information about authenticating with SQL Server using ADO-style parameters, see SQL Server ADO authentication.

    1. import "sql"
    2. driverName: "awsathena",
    3. dataSourceName: "s3://myorgqueryresults/?accessID=12ab34cd56ef&region=region-name&secretAccessKey=y0urSup3rs3crEtT0k3n",
    4. query: "GO SELECT * FROM Example.Table",
    5. )

    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.

    1. // Import the "sql" package
    2. import "sql"
    3. // Query data from PostgreSQL
    4. sensorInfo = sql.from(
    5. driverName: "postgres",
    6. dataSourceName: "postgresql://localhost?sslmode=disable",
    7. query: "SELECT * FROM sensors",
    8. )
    9. // Query data from InfluxDB
    10. sensorMetrics = from(bucket: "example-bucket")
    11. |> range(start: -1h)
    12. |> filter(fn: (r) => r._measurement == "airSensors")
    13. // Join InfluxDB query results with PostgreSQL query results
    14. 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.

    1. import "sql"
    2. sql.from(
    3. driverName: "postgres",
    4. dataSourceName: "postgresql://localhost?sslmode=disable",
    5. query: "SELECT * FROM sensors",
    6. )
    7. |> rename(columns: {location: "_value"})
    8. |> 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.

    influx CLI

    1. curl --request PATCH http://localhost:8086/api/v2/orgs/<org-id>/secrets \
    2. --header 'Authorization: Token YOURAUTHTOKEN' \
    3. --header 'Content-type: application/json' \
    4. --data '{
    5. "POSTGRES_PASS": "example-password"
    6. }'

    To store secrets, you need:

    1. # Syntax
    2. influx secret update -k <secret-key>
    3. # Example
    4. 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.

    1. 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

    1. 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:

      1. import "influxdata/influxdb/sample"
      2. option task = {name: "Collect sample air sensor data", every: 15m}
      3. sample.data(set: "airSensor")
      4. |> to(org: "example-org", bucket: "example-bucket")
    2. Query your target bucket after the first task run to ensure the sample data is writing successfully.

      1. from(bucket: "example-bucket")
      2. |> range(start: -1m)
      3. |> filter(fn: (r) => r._measurement == "airSensors")

    Import the sample sensor information

    1. Download and install PostgreSQL.

    2. Download the sample sensor information CSV.

    3. Use a PostgreSQL client (psql or a GUI) to create the sensors table:

      1. CREATE TABLE sensors (
      2. sensor_id character varying(50),
      3. location character varying(50),
      4. model_number character varying(50),
      5. last_inspected date
      6. );
    4. Import the downloaded CSV sample data. Update the FROM file path to the path of the downloaded CSV sample data.

      1. COPY sensors(sensor_id,location,model_number,last_inspected)
      2. FROM '/path/to/sample-sensor-info.csv' DELIMITER ',' CSV HEADER;
    5. 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.