This page describes how to query data from QuestDB using different programming languages and tools. To query data in a running instance, there are three main methods that can be used:

Prerequisites

QuestDB must be running and accessible, you can do so from Docker, the binaries or Homebrew for macOS users.

Postgres compatibility

You can query data using the Postgres endpoint that QuestDB exposes. This is accessible via port 8812.

<Tabs defaultValue=”nodejs” values={[ { label: “NodeJS”, value: “nodejs” }, { label: “Go”, value: “go” }, { label: “Java”, value: “java” }, { label: “C#”, value: “csharp” }, { label: “C”, value: “c” }, { label: “Python”, value: “python” }, ]}>

  1. const { Client } = require("pg")
  2. const start = async () => {
  3. try {
  4. const client = new Client({
  5. database: "qdb",
  6. host: "127.0.0.1",
  7. password: "quest",
  8. port: 8812,
  9. user: "admin",
  10. })
  11. await client.connect()
  12. const res = await client.query("SELECT x FROM long_sequence(5);")
  13. console.log(res.rows)
  14. await client.end()
  15. } catch (e) {
  16. console.log(e)
  17. }
  18. }
  19. start()
  1. package main
  2. import (
  3. "database/sql"
  4. "fmt"
  5. _ "github.com/lib/pq"
  6. )
  7. const (
  8. host = "localhost"
  9. port = 8812
  10. user = "admin"
  11. password = "quest"
  12. dbname = "qdb"
  13. )
  14. func main() {
  15. connStr := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname)
  16. db, err := sql.Open("postgres", connStr)
  17. checkErr(err)
  18. defer db.Close()
  19. // Currently, we do not support queries with bind parameters in Go
  20. rows, err := db.Query("SELECT x FROM long_sequence(5);")
  21. checkErr(err)
  22. defer rows.Close()
  23. for rows.Next() {
  24. var num string
  25. err = rows.Scan(&num)
  26. checkErr(err)
  27. fmt.Println(num)
  28. }
  29. err = rows.Err()
  30. checkErr(err)
  31. }
  32. func checkErr(err error) {
  33. if err != nil {
  34. panic(err)
  35. }
  36. }
  1. // compile with
  2. // g++ libpq_example.c -o libpq_example.exe -I pgsql\include -L dev\pgsql\lib
  3. // -std=c++17 -lpthread -lpq
  4. #include <libpq-fe.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. void do_exit(PGconn *conn) {
  8. PQfinish(conn);
  9. exit(1);
  10. }
  11. int main() {
  12. PGconn *conn = PQconnectdb(
  13. "host=localhost user=admin password=quest port=8812 dbname=testdb");
  14. if (PQstatus(conn) == CONNECTION_BAD) {
  15. fprintf(stderr, "Connection to database failed: %s\n",
  16. PQerrorMessage(conn));
  17. do_exit(conn);
  18. }
  19. PGresult *res = PQexec(conn, "SELECT x FROM long_sequence(5);");
  20. if (PQresultStatus(res) != PGRES_TUPLES_OK) {
  21. printf("No data retrieved\n");
  22. PQclear(res);
  23. do_exit(conn);
  24. }
  25. int rows = PQntuples(res);
  26. for (int i = 0; i < rows; i++) {
  27. printf("%s\n", PQgetvalue(res, i, 0));
  28. }
  29. PQclear(res);
  30. PQfinish(conn);
  31. return 0;
  32. }
  1. package com.myco;
  2. import java.sql.*;
  3. import java.util.Properties;
  4. public class App {
  5. public static void main(String[] args) throws SQLException {
  6. Properties properties = new Properties();
  7. properties.setProperty("user", "admin");
  8. properties.setProperty("password", "quest");
  9. properties.setProperty("sslmode", "disable");
  10. final Connection connection = DriverManager.getConnection("jdbc:postgresql://localhost:8812/qdb", properties);
  11. try (PreparedStatement preparedStatement = connection.prepareStatement("SELECT x FROM long_sequence(5);")) {
  12. try (ResultSet rs = preparedStatement.executeQuery()) {
  13. while (rs.next()) {
  14. System.out.println(rs.getLong(1));
  15. }
  16. }
  17. }
  18. connection.close();
  19. }
  20. }
  1. using Npgsql;
  2. string username = "admin";
  3. string password = "quest";
  4. string database = "qdb";
  5. int port = 8812;
  6. var connectionString = $"host=localhost;port={port};username={username};password={password};
  7. database={database};ServerCompatibilityMode=NoTypeLoading;";
  8. await using NpgsqlConnection connection = new(connectionString);
  9. await connection.OpenAsync();
  10. var sql = "SELECT x FROM long_sequence(5);";
  11. await using NpgsqlCommand command = new (sql, connection);
  12. await using (var reader = await command.ExecuteReaderAsync()) {
  13. while (await reader.ReadAsync())
  14. {
  15. var station = reader.GetString(0);
  16. var height = double.Parse(reader.GetString(1));
  17. var timestamp = reader.GetString(2);
  18. }
  19. }
  1. import psycopg2
  2. try:
  3. connection = psycopg2.connect(user="admin",
  4. password="quest",
  5. host="127.0.0.1",
  6. port="8812",
  7. database="qdb")
  8. cursor = connection.cursor()
  9. postgreSQL_select_Query = "SELECT x FROM long_sequence(5);"
  10. cursor.execute(postgreSQL_select_Query)
  11. print("Selecting rows from test table using cursor.fetchall")
  12. mobile_records = cursor.fetchall()
  13. print("Print each row and it's columns values")
  14. for row in mobile_records:
  15. print("y = ", row[0], "\n")
  16. except (Exception, psycopg2.Error) as error:
  17. print("Error while fetching data from PostgreSQL", error)
  18. finally:
  19. #closing database connection.
  20. if (connection):
  21. cursor.close()
  22. connection.close()
  23. print("PostgreSQL connection is closed")

REST API

You can query data using the REST API, this will work with a very wide range of libraries and tools. The REST API is accessible on port 9000.

More information on the available endpoints alongside possible parameters and usage examples can be found on the REST API reference page.

import Tabs from “@theme/Tabs” import TabItem from “@theme/TabItem”

<Tabs defaultValue=”curl” values={[ { label: “cURL”, value: “curl” }, { label: “NodeJS”, value: “nodejs” }, { label: “Go”, value: “go” }, ]}>

  1. curl -G \
  2. --data-urlencode "query=SELECT x FROM long_sequence(5);" \
  3. http://localhost:9000/exec
  1. const fetch = require("node-fetch")
  2. const qs = require("querystring")
  3. const HOST = "http://localhost:9000"
  4. async function run() {
  5. try {
  6. const queryData = {
  7. query: "SELECT x FROM long_sequence(5);",
  8. }
  9. const response = await fetch(`${HOST}/exec?${qs.encode(queryData)}`)
  10. const json = await response.json()
  11. console.log(json)
  12. } catch (error) {
  13. console.log(error)
  14. }
  15. }
  16. run()
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "net/http"
  7. "net/url"
  8. )
  9. func main() {
  10. u, err := url.Parse("http://localhost:9000")
  11. checkErr(err)
  12. u.Path += "exec"
  13. params := url.Values{}
  14. params.Add("query", "SELECT x FROM long_sequence(5);")
  15. u.RawQuery = params.Encode()
  16. url := fmt.Sprintf("%v", u)
  17. res, err := http.Get(url)
  18. checkErr(err)
  19. defer res.Body.Close()
  20. body, err := ioutil.ReadAll(res.Body)
  21. checkErr(err)
  22. log.Println(string(body))
  23. }
  24. func checkErr(err error) {
  25. if err != nil {
  26. panic(err)
  27. }
  28. }

Web Console

Details for inserting data with the Web Console is covered on the insert data page. To query data from the web console, SQL statements can be written in the code editor and executed by clicking RUN.

  1. SHOW TABLES;
  2. SELECT * FROM my_table;
  3. --Note that `SELECT * FROM` is optional
  4. my_table;

Aside from the Code Editor, the Web Console includes a Visualization panel for viewing query results as tables or graphs and an Import tab for uploading datasets as CSV files. For more details on these components and general use of the console, see the Web Console reference page.