### Client Configuration Examples Source: https://questdb.com/docs/ingestion/message-brokers/kafka Examples of client.conf.string configurations for minimal setups, HTTPS with timeouts, and authentication tokens. ```properties # Minimal configuration client.conf.string=http::addr=localhost:9000; # With HTTPS and retry timeout client.conf.string=https::addr=questdb.example.com:9000;retry_timeout=60000; # With authentication token from environment variable client.conf.string=http::addr=localhost:9000;token=${QUESTDB_TOKEN}; ``` -------------------------------- ### Install QuestDB Service on Windows Source: https://questdb.com/docs/configuration/overview Installs the QuestDB service on Windows. The service will start automatically at startup. ```shell questdb.exe install ``` -------------------------------- ### Install QuestDB Go Client Source: https://questdb.com/docs/ingestion/clients/go Use the go get command to add the QuestDB client dependency to your project. ```toml go get github.com/questdb/go-questdb-client/ ``` -------------------------------- ### Schema and dataset setup Source: https://questdb.com/docs/query/sql/limit Table creation and data insertion used for the following examples. ```questdb-sql CREATE TABLE orders (id LONG); INSERT INTO orders VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10); ``` -------------------------------- ### Start Minikube Cluster Source: https://questdb.com/docs/deployment/kubernetes Starts a local Kubernetes cluster using minikube. Ensure minikube is installed and configured. ```shell minikube start ``` -------------------------------- ### Start Profiler with Custom Agent Parameters Source: https://questdb.com/docs/troubleshooting/profiling Starts continuous profiling and overrides all default async-profiler settings. This example profiles CPU events and specifies a custom output file and sampling interval. ```shell ./questdb.sh start -p -- start,event=cpu,file=/tmp/profile.jfr,interval=10ms ``` -------------------------------- ### Python Insert and Query Example Source: https://questdb.com/docs/query/pgwire/overview Demonstrates connecting to QuestDB, creating a table, inserting multiple rows with timestamps, and querying the data using the psycopg3 adapter. Ensure the 'psycopg[binary]' package is installed. ```shell python3 -m pip install "psycopg[binary]" ``` ```python import psycopg as pg import time # Connect to an existing QuestDB instance conn_str = 'user=admin password=quest host=127.0.0.1 port=8812 dbname=qdb' with pg.connect(conn_str, autocommit=True) as connection: # Open a cursor to perform database operations with connection.cursor() as cur: # Execute a command: this creates a new table cur.execute(''' CREATE TABLE IF NOT EXISTS test_pg ( ts TIMESTAMP, name STRING, value INT ) timestamp(ts); ''') print('Table created.') # Insert data into the table. for x in range(10): # Converting datetime into millisecond for QuestDB timestamp = time.time_ns() // 1000 cur.execute(''' INSERT INTO test_pg VALUES (%s, %s, %s); ''', (timestamp, 'python example', x)) print('Rows inserted.') #Query the database and obtain data as Python objects. cur.execute('SELECT * FROM test_pg;') records = cur.fetchall() for row in records: print(row) # the connection is now closed ``` -------------------------------- ### Go Client Example Source: https://questdb.com/docs/query/overview Example of connecting to QuestDB and executing a query using the Go client library. ```APIDOC ## Go Client Example ### Description This Go code demonstrates how to connect to QuestDB using the `github.com/lib/pq` driver and execute a SQL query. ### Method N/A (Client-side code) ### Endpoint N/A (Client-side code) ### Parameters N/A ### Request Example ```go package main import ( "database/sql" "fmt" _ "github.com/lib/pq" ) const ( host = "localhost" port = 8812 user = "admin" password = "quest" dbname = "qdb" ) func main() { connStr := fmt.Sprintf( "host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname) db, err := sql.Open("postgres", connStr) checkErr(err) defer db.Close() stmt, err := db.Prepare("SELECT x FROM long_sequence(5);") checkErr(err) defer stmt.Close() rows, err := stmt.Query() checkErr(err) defer rows.Close() var num string for rows.Next() { err = rows.Scan(&num) checkErr(err) fmt.Println(num) } err = rows.Err() checkErr(err) } func checkErr(err error) { if err != nil { panic(err) } } ``` ### Response N/A (Client-side code, output is printed to console) #### Success Response (200) N/A #### Response Example Output will be printed to the console, e.g.: ``` 0 1 2 3 4 ``` ``` -------------------------------- ### Schema and data setup Source: https://questdb.com/docs/query/sql/lateral-join Initial tables and data used for lateral join examples. ```questdb-sql CREATE TABLE orders ( id INT, desk SYMBOL, min_qty DOUBLE, ts TIMESTAMP ) TIMESTAMP(ts) PARTITION BY DAY; CREATE TABLE fills ( id INT, order_id INT, qty DOUBLE, ts TIMESTAMP ) TIMESTAMP(ts) PARTITION BY DAY; INSERT INTO orders VALUES (1, 'eq', 15.0, '2024-01-01T00:00:00.000000Z'), (2, 'fi', 35.0, '2024-01-01T01:00:00.000000Z'), (3, 'cmd', 5.0, '2024-01-01T02:00:00.000000Z'); INSERT INTO fills VALUES (1, 1, 10.0, '2024-01-01T00:10:00.000000Z'), (2, 1, 20.0, '2024-01-01T00:40:00.000000Z'), (3, 1, 30.0, '2024-01-01T01:10:00.000000Z'), (4, 2, 40.0, '2024-01-01T01:10:00.000000Z'), (5, 2, 50.0, '2024-01-01T01:40:00.000000Z'); ``` -------------------------------- ### Example Prompt for TSBS Benchmark Skill Source: https://questdb.com/docs/getting-started/ai-coding-agents This is an example prompt to initiate the full TSBS benchmark against QuestDB using the default cpu-only dataset. The agent will manage all steps from prerequisite installation to result reporting. ```text Run the full TSBS benchmark against QuestDB with the default cpu-only dataset. ``` -------------------------------- ### Download and Install JDK and QuestDB Source: https://questdb.com/docs/deployment/systemd This script downloads and installs the JDK and QuestDB, setting up necessary directories and environment variables. ```bash #!/bin/bash # Download and install the JDK curl -s https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.tar.gz -o jdk.tar.gz mkdir -p ~/jdk tar -xzf jdk.tar.gz -C ~/jdk --strip-components=1 export JAVA_HOME=~/jdk export PATH=$JAVA_HOME/bin:$PATH # Download and set up QuestDB curl -s https://dl.questdb.io/snapshots/questdb-latest-no-jre-bin.tar.gz -o questdb.tar.gz mkdir -p ~/questdb/binary mkdir -p ~/bin ~/var/lib/questdb tar -xzf questdb.tar.gz -C ~/questdb/binary --strip-components 1 mv ~/questdb/binary/questdb.jar ~/bin/ ``` -------------------------------- ### Psycopg3 Connection Pooling Example Source: https://questdb.com/docs/query/pgwire/python Demonstrates how to establish and use a connection pool with psycopg3 for efficient connection management. Ensure the psycopg_pool package is installed. ```python from psycopg_pool import ConnectionPool pool = ConnectionPool( min_size=5, max_size=20, kwargs={ 'host': '127.0.0.1', 'port': 8812, 'user': 'admin', 'password': 'quest', 'dbname': 'qdb', 'autocommit': True } ) with pool.connection() as conn: with conn.cursor(binary=True) as cur: cur.execute("SELECT * FROM trades LIMIT 10") rows = cur.fetchall() print(f"Fetched {len(rows)} rows") pool.close() ``` -------------------------------- ### Start Kafka and Connector Services Source: https://questdb.com/docs/ingestion/message-brokers/kafka Commands to initialize storage and start the Kafka and connector processes. ```shell # Generate a unique cluster ID KAFKA_CLUSTER_ID="$(bin/kafka-storage.sh random-uuid)" # Format storage directories (run once) bin/kafka-storage.sh format --standalone -t $KAFKA_CLUSTER_ID -c config/server.properties # Start Kafka bin/kafka-server-start.sh config/server.properties # Start the connector (from another terminal) bin/connect-standalone.sh config/connect-standalone.properties config/questdb-connector.properties ``` -------------------------------- ### Start New QuestDB Version After Upgrade Source: https://questdb.com/docs/deployment/azure Navigate to the bin directory of the new QuestDB version and start the service. ```bash cd questdb-*/bin ./questdb.sh start ``` -------------------------------- ### Quick Start: HTTP Ingestion Example Source: https://questdb.com/docs/ingestion/clients/java Creates a client instance using HTTP transport to connect to a QuestDB server and sends two rows of data with server-assigned timestamps. ```java package com.example.sender; import io.questdb.client.Sender; public class HttpExample { public static void main(String[] args) { try (Sender sender = Sender.fromConfig("http::addr=localhost:9000;")) { sender.table("trades") .symbol("symbol", "ETH-USD") .symbol("side", "sell") .doubleColumn("price", 2615.54) .doubleColumn("amount", 0.00044) .atNow(); sender.table("trades") .symbol("symbol", "TC-USD") .symbol("side", "sell") .doubleColumn("price", 39269.98) .doubleColumn("amount", 0.001) .atNow(); } } } ``` -------------------------------- ### Install asyncpg Source: https://questdb.com/docs/query/pgwire/python Install the asyncpg library via pip. ```bash pip install asyncpg ``` -------------------------------- ### Download and Start QuestDB Source: https://questdb.com/docs/deployment/azure Download the QuestDB archive, extract it, and start the QuestDB service on your Azure VM. ```bash wget https://github.com/questdb/questdb/releases/download/9.4.1/questdb-9.4.1-rt-linux-x86-64.tar.gz tar xzf questdb-9.4.1-rt-linux-x86-64.tar.gz cd questdb-9.4.1-rt-linux-x86-64/bin ./questdb.sh start ``` -------------------------------- ### Install pgx Go Client Source: https://questdb.com/docs/query/pgwire/go Install the latest version of the pgx Go client using Go modules. ```bash go get github.com/jackc/pgx/v5 ``` -------------------------------- ### Download and Start QuestDB Binary Source: https://questdb.com/docs/deployment/aws Use this command to download the QuestDB binary directly and start it on an EC2 instance. ```bash curl -L https://questdb.com/download -o questdb.tar.gz tar xzf questdb.tar.gz ./questdb.sh start ``` -------------------------------- ### Monitoring and Examples Source: https://questdb.com/docs/query/sql/refresh-mat-view Examples of how to restore invalid views, perform manual refreshes, backfill data, and monitor refresh progress. ```APIDOC ## REFRESH MATERIALIZED VIEW Examples and Monitoring ### Restore an invalid view This example shows how to check the status of a view, refresh it if invalid, and verify its status. ```sql -- Check status and reason for invalidation SELECT view_name, view_status, invalidation_reason FROM materialized_views() WHERE view_name = 'trades_hourly'; -- Rebuild the view if invalid REFRESH MATERIALIZED VIEW trades_hourly FULL; -- Verify the view is valid again SELECT view_name, view_status FROM materialized_views() WHERE view_name = 'trades_hourly'; ``` ### Manual refresh workflow For views configured with `REFRESH MANUAL` strategy, this demonstrates how to trigger a refresh. ```sql -- Create a manually-refreshed view CREATE MATERIALIZED VIEW daily_summary REFRESH MANUAL AS SELECT timestamp, symbol, sum(amount) AS volume FROM trades SAMPLE BY 1d; -- Refresh when ready (e.g., after batch load completes) REFRESH MATERIALIZED VIEW daily_summary INCREMENTAL; ``` ### Backfill old data Use `RANGE` refresh to backfill data that arrived late and is outside the standard refresh limit. ```sql -- Late data arrived for May 1st -- Incremental refresh won't pick it up if outside the limit -- Refresh just that day REFRESH MATERIALIZED VIEW trades_hourly RANGE FROM '2025-05-01T00:00:00Z' TO '2025-05-02T00:00:00Z'; ``` ### Monitoring refresh progress Query the `materialized_views()` system table to monitor the status and progress of a refresh operation. ```sql SELECT view_name, view_status, refresh_base_table_txn, base_table_txn, last_refresh_start_timestamp, last_refresh_finish_timestamp FROM materialized_views() WHERE view_name = 'trades_hourly'; ``` **Interpretation:** When `refresh_base_table_txn` equals `base_table_txn`, the view is fully up-to-date. ``` -------------------------------- ### Install Databento and QuestDB Python libraries Source: https://questdb.com/docs/integrations/other/databento Install the necessary Python packages for Databento and QuestDB connectivity. ```bash pip3 install questdb pip3 install databento ``` -------------------------------- ### C Client Example Source: https://questdb.com/docs/query/overview Example of connecting to QuestDB and executing a query using the C client library. ```APIDOC ## C Client Example ### Description This C code demonstrates how to connect to QuestDB using `libpq` and execute a SQL query. ### Method N/A (Client-side code) ### Endpoint N/A (Client-side code) ### Parameters N/A ### Request Example ```c // compile with // g++ libpq_example.c -o libpq_example.exe -I pgsql\include -L dev\pgsql\lib // -std=c++17 -lpthread -lpq #include #include #include void do_exit(PGconn *conn) { PQfinish(conn); exit(1); } int main() { PGconn *conn = PQconnectdb( "host=localhost user=admin password=quest port=8812 dbname=testdb"); if (PQstatus(conn) == CONNECTION_BAD) { fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(conn)); do_exit(conn); } PGresult *res = PQexec(conn, "SELECT x FROM long_sequence(5);"); if (PQresultStatus(res) != PGRES_TUPLES_OK) { printf("No data retrieved\n"); PQclear(res); do_exit(conn); } int rows = PQntuples(res); for (int i = 0; i < rows; i++) { printf("%s\n", PQgetvalue(res, i, 0)); } PQclear(res); PQfinish(conn); return 0; } ``` ### Response N/A (Client-side code, output is printed to console) #### Success Response (200) N/A #### Response Example Output will be printed to the console, e.g.: ``` 0 1 2 3 4 ``` ``` -------------------------------- ### Ruby Client Example Source: https://questdb.com/docs/query/overview Example of connecting to QuestDB and executing a query using the Ruby client library. ```APIDOC ## Ruby Client Example ### Description This Ruby code demonstrates how to connect to QuestDB using the `pg` gem and execute a SQL query. ### Method N/A (Client-side code) ### Endpoint N/A (Client-side code) ### Parameters N/A ### Request Example ```ruby require 'pg' begin conn =PG.connect( host: "127.0.0.1", port: 8812, dbname: 'qdb', user: 'admin', password: 'quest' ) rows = conn.exec 'SELECT x FROM long_sequence(5);' rows.each do |row| puts row end rescue PG::Error => e puts e.message ensure conn.close if conn end ``` ### Response N/A (Client-side code, output is printed to console) #### Success Response (200) N/A #### Response Example Output will be printed to the console, e.g.: ``` 0 1 2 3 4 ``` ``` -------------------------------- ### PHP Client Example Source: https://questdb.com/docs/query/overview Example of connecting to QuestDB and executing a query using the PHP client library. ```APIDOC ## PHP Client Example ### Description This PHP code demonstrates how to connect to QuestDB using `pg_connect` and execute a SQL query. ### Method N/A (Client-side code) ### Endpoint N/A (Client-side code) ### Parameters N/A ### Request Example ```php getMessage(), "\n"; } finally { if (!is_null($db_conn)) { pg_close($db_conn); } } ?> ``` ### Response N/A (Client-side code, output is printed or logged) #### Success Response (200) N/A #### Response Example Output will be printed, e.g.: ``` Array ( [x] => 0 ) Array ( [x] => 1 ) ... ``` ``` -------------------------------- ### Setting Parquet Options Examples Source: https://questdb.com/docs/query/sql/alter-table-alter-column-set-parquet Examples demonstrating how to set encoding, compression, and bloom filter options for Parquet columns. ```APIDOC ## Examples ### Set encoding only ```questdb-sql title="Set encoding only" ALTER TABLE trades ALTER COLUMN price SET PARQUET(rle_dictionary); ``` ### Set compression only (with optional level) ```questdb-sql title="Set compression only (with optional level)" ALTER TABLE trades ALTER COLUMN price SET PARQUET(default, zstd(3)); ``` ### Set both encoding and compression ```questdb-sql title="Set both encoding and compression" ALTER TABLE trades ALTER COLUMN price SET PARQUET(rle_dictionary, zstd(3)); ``` ### Reset to defaults ```questdb-sql title="Reset to defaults" ALTER TABLE trades ALTER COLUMN price SET PARQUET(default); ``` ### Enable bloom filter with default encoding ```questdb-sql title="Enable bloom filter with default encoding" ALTER TABLE trades ALTER COLUMN symbol SET PARQUET(default, BLOOM_FILTER); ``` ### Enable bloom filter with encoding and compression ```questdb-sql title="Enable bloom filter with encoding and compression" ALTER TABLE trades ALTER COLUMN symbol SET PARQUET(rle_dictionary, zstd(3), BLOOM_FILTER); ``` ### Clear bloom filter, keep encoding ```questdb-sql title="Clear bloom filter, keep encoding" ALTER TABLE trades ALTER COLUMN symbol SET PARQUET(rle_dictionary); ``` ``` -------------------------------- ### C# Client Example Source: https://questdb.com/docs/query/overview Example of connecting to QuestDB and executing a query using the C# client library. ```APIDOC ## C# Client Example ### Description This C# code demonstrates how to connect to QuestDB using Npgsql and execute a SQL query. ### Method N/A (Client-side code) ### Endpoint N/A (Client-side code) ### Parameters N/A ### Request Example ```csharp using Npgsql; string username = "admin"; string password = "quest"; string database = "qdb"; int port = 8812; var connectionString = $"host=localhost;port={port};username={username};password={password}; database={database};ServerCompatibilityMode=NoTypeLoading;"; await using NpgsqlConnection connection = new NpgsqlConnection(connectionString); await connection.OpenAsync(); var sql = "SELECT x FROM long_sequence(5);"; await using NpgsqlCommand command = new NpgsqlCommand(sql, connection); await using (var reader = await command.ExecuteReaderAsync()) { while (await reader.ReadAsync()) { var x = reader.GetInt64(0); } } ``` ### Response N/A (Client-side code, data is read into variables) #### Success Response (200) N/A #### Response Example Data is processed within the `reader.GetInt64(0)` call. ``` -------------------------------- ### Install psycopg[binary] Source: https://questdb.com/docs/query/pgwire/python Install the binary version of psycopg3 for improved performance. This is recommended over the pure-Python version. ```bash pip install psycopg[binary] ``` -------------------------------- ### Install QuestDB Kafka Connector Source: https://questdb.com/docs/ingestion/message-brokers/kafka Commands to download and extract the connector JAR files for Kafka installation. ```shell curl -s https://api.github.com/repos/questdb/kafka-questdb-connector/releases/latest | jq -r '.assets[]|select(.content_type == "application/zip")|.browser_download_url'| wget -qi - ``` ```shell unzip kafka-questdb-connector-*-bin.zip cd kafka-questdb-connector cp ./*.jar /path/to/kafka_*.*-*.*.*/libs ``` -------------------------------- ### Install .NET QuestDB Client Source: https://questdb.com/docs/ingestion/clients/dotnet Install the latest version of the QuestDB .NET client using the dotnet CLI. ```shell dotnet add package net-questdb-client ``` -------------------------------- ### Install jose and jq on Ubuntu Source: https://questdb.com/docs/ingestion/ilp/overview Installs the jose and jq utilities on Ubuntu using apt. These are prerequisites for generating authentication tokens. ```bash apt install jose apt install jq ``` -------------------------------- ### Ubuntu - Install ZFS Source: https://questdb.com/docs/getting-started/enterprise-quick-start Install the ZFS utilities on an Ubuntu system. This is a prerequisite for enabling ZFS compression. ```bash sudo apt update sudo apt install zfsutils-linux ``` -------------------------------- ### Install QuestDB Helm Chart Source: https://questdb.com/docs/deployment/kubernetes Installs the QuestDB Helm chart with a custom release name 'my-questdb'. ```shell helm install my-questdb questdb/questdb ``` -------------------------------- ### Start Containers Source: https://questdb.com/docs/integrations/other/cube Prepare the model directory and launch the services. ```bash mkdir model docker-compose up -d ``` -------------------------------- ### Create Service Account Examples Source: https://questdb.com/docs/query/sql/acl/create-service-account Basic usage examples for creating service accounts with and without the IF NOT EXISTS clause. ```questdb-sql CREATE SERVICE ACCOUNT audit; CREATE SERVICE ACCOUNT IF NOT EXISTS audit; ``` -------------------------------- ### Install PostgreSQL Client Source: https://questdb.com/docs/deployment/hetzner Installs the PostgreSQL client package required for executing database checkpoint commands during backup operations. ```bash # Install PostgreSQL client questdb01$ apt update && apt install -y postgresql-client ``` -------------------------------- ### Main Function Example Source: https://questdb.com/docs/query/pgwire/go Demonstrates the usage of the QuestDB client by connecting to the database, fetching recent trades, sampled data, and latest prices, then printing the results. Ensure QuestDB is running and accessible at the specified connection string. ```go type Trade struct { Timestamp time.Time Symbol string Price float64 Amount float64 } type PriceSample struct { Timestamp time.Time Symbol string AvgPrice float64 MinPrice float64 MaxPrice float64 } func main() { ctx := context.Background() client, err := NewQuestDBClient(ctx, "postgres://admin:quest@localhost:8812/qdb") if err != nil { log.Fatalf("Failed to create QuestDB client: %v", err) } defer client.Close() // Get recent trades rades, err := client.GetRecentTrades(ctx, "BTC-USD", 5) if err != nil { log.Fatalf("Failed to get recent trades: %v", err) } fmt.Println("Recent BTC-USD trades:") for _, t := range trades { fmt.Printf(" %s: Price: %.2f, Amount: %.6f\n", t.Timestamp, t.Price, t.Amount) } // Get sampled data samples, err := client.GetSampledData(ctx, "BTC-USD", 1) if err != nil { log.Fatalf("Failed to get sampled data: %v", err) } fmt.Println("\nHourly BTC-USD price samples (last 24 hours):") for _, s := range samples { fmt.Printf(" %s: Avg: %.2f, Range: %.2f - %.2f\n", s.Timestamp, s.AvgPrice, s.MinPrice, s.MaxPrice) } // Get latest prices latestPrices, err := client.GetLatestPrices(ctx) if err != nil { log.Fatalf("Failed to get latest prices: %v", err) } fmt.Println("\nLatest prices for all symbols:") for _, t := range latestPrices { fmt.Printf(" %s: %.2f\n", t.Symbol, t.Price) } } ``` -------------------------------- ### Initialize Project Directory Source: https://questdb.com/docs/integrations/other/cube Create a new directory for the project and navigate into it. ```shell mkdir questdb-cube && cd $_ ``` -------------------------------- ### Get Server Postmaster Start Time Source: https://questdb.com/docs/query/functions/date-time Retrieves the timestamp indicating when the QuestDB server process was started. This value is static for the duration of the server's runtime. ```questdb-sql SELECT pg_postmaster_start_time(); ``` -------------------------------- ### Create a Sample Table with Weekly Partitions Source: https://questdb.com/docs/query/functions/meta This snippet demonstrates how to create a sample table named 'my_table' with data and configure it to be partitioned by week. This setup is useful for testing partition-related queries. ```questdb-sql CREATE TABLE my_table AS ( SELECT rnd_symbol('EURO', 'USD', 'OTHER') symbol, rnd_double() * 50.0 price, rnd_double() * 20.0 amount, to_timestamp('2023-01-01', 'yyyy-MM-dd') + x * 6 * 3600 * 100000L timestamp FROM long_sequence(700) ), INDEX(symbol capacity 32) TIMESTAMP(timestamp) PARTITION BY WEEK; ``` -------------------------------- ### Create Table Example Source: https://questdb.com/docs/query/functions/timestamp An example demonstrating the creation of a table named 'temperatures' with a designated timestamp column 'ts'. ```questdb-sql CREATE TABLE temperatures(ts timestamp, sensorID symbol, sensorLocation symbol, reading double) timestamp(ts); ``` -------------------------------- ### Install QuestDB Node.js Client Source: https://questdb.com/docs/ingestion/clients/nodejs Install the QuestDB Node.js client using npm. Ensure you have Node.js v16 or newer. ```shell npm i -s @questdb/nodejs-client ``` -------------------------------- ### Get session start times Source: https://questdb.com/docs/cookbook/sql/time-series/session-windows Groups by session to retrieve the timestamp and status at the beginning of each session. ```sql SELECT first(timestamp) as ts, ... FROM global_sessions GROUP BY session ``` -------------------------------- ### Rust PGWire Client Example Source: https://questdb.com/docs/query/pgwire/overview Demonstrates basic connection, batch execution for table creation, parameterized inserts, and prepared statements using the rust-postgres client. ```rust use postgres::{Client, NoTls, Error}; use chrono::{Utc}; use std::time::SystemTime; fn main() -> Result<(), Error> { let mut client = Client::connect("postgresql://admin:quest@localhost:8812/qdb", NoTls)?; // Basic query client.batch_execute( "CREATE TABLE IF NOT EXISTS trades ( \ ts TIMESTAMP, date DATE, name STRING, value INT \ ) timestamp(ts);")?; // Parameterized query let name: &str = "rust example"; let val: i32 = 123; let utc = Utc::now(); let sys_time = SystemTime::now(); client.execute( "INSERT INTO trades VALUES($1,$2,$3,$4)", &[&utc.naive_local(), &sys_time, &name, &val], )?; // Prepared statement let mut txn = client.transaction()?; let statement = txn.prepare("INSERT INTO trades VALUES ($1,$2,$3,$4)")?; for value in 0..10 { let utc = Utc::now(); let sys_time = SystemTime::now(); txn.execute(&statement, &[&utc.naive_local(), &sys_time, &name, &value])?; } txn.commit()?; println!("import finished"); Ok(()) } ``` -------------------------------- ### Install QuestDB Python Client Source: https://questdb.com/docs/ingestion/clients/python Install or update the QuestDB client globally or within a virtual environment using pip. ```bash python3 -m pip install -U questdb ``` ```bash pip install -U questdb ``` -------------------------------- ### Initialize Databento Client Source: https://questdb.com/docs/integrations/other/databento Set up the Databento client using your API key. ```python import databento as db db_client = db.Live(key="YOUR_API_KEY") ``` -------------------------------- ### Extract Interval Lower Bound Source: https://questdb.com/docs/query/functions/date-time Extracts the lower bound of an interval. Use this function to get the start timestamp of a time range. ```questdb-sql SELECT interval_start( interval('2024-10-08T11:09:47.573Z', '2024-10-09T11:09:47.573Z') ) ``` -------------------------------- ### Example Usage Source: https://questdb.com/docs/query/sql/alter-table-squash-partitions Demonstrates how to use ALTER TABLE SQUASH PARTITIONS and verify the results using SHOW PARTITIONS. ```APIDOC ## Example Usage This example shows how to squash partitions for a table named 'x' and then verify the partition structure. ### Steps 1. **Show initial partitions:** ```questdb-sql SHOW PARTITIONS FROM x; ``` *(This would display tables with split partitions, e.g., two partitions for '2023-02-05')* 2. **Squash partitions:** ```questdb-sql ALTER TABLE x SQUASH PARTITIONS; ``` 3. **Show partitions after squashing:** ```questdb-sql SHOW PARTITIONS FROM x; ``` *(This would display the merged partitions, e.g., a single partition for '2023-02-05')* ### Request Example ```questdb-sql ALTER TABLE x SQUASH PARTITIONS; ``` ### Response Example *(The response is the state of the table's partitions after the command is executed. Use `SHOW PARTITIONS` to verify.)* **Before Squashing (Example):** ``` | index | partitionBy | name | minTimestamp | maxTimestamp | numRows | diskSize | diskSizeHuman | readOnly | active | attached | detached | attachable | | ----- | ----------- | ------------------------ | --------------------------- | --------------------------- | ------- | -------- | ------------- | -------- | ------ | -------- | -------- | ---------- | | 0 | DAY | 2023-02-04 | 2023-02-04T00:00:00.000000Z | 2023-02-04T23:59:59.940000Z | 1440000 | 71281136 | 68.0 MiB | FALSE | FALSE | TRUE | FALSE | FALSE | | 1 | DAY | 2023-02-05 | 2023-02-05T00:00:00.000000Z | 2023-02-05T20:59:59.880000Z | 1259999 | 65388544 | 62.4 MiB | FALSE | FALSE | TRUE | FALSE | FALSE | | 2 | DAY | 2023-02-05T205959-880001 | 2023-02-05T20:59:59.940000Z | 2023-02-05T21:59:59.940000Z | 60002 | 83886080 | 80.0 MiB | FALSE | TRUE | TRUE | FALSE | FALSE | ``` **After Squashing (Example):** ``` | index | partitionBy | name | minTimestamp | maxTimestamp | numRows | diskSize | diskSizeHuman | readOnly | active | attached | detached | attachable | | ----- | ----------- | ---------- | --------------------------- | --------------------------- | ------- | -------- | ------------- | -------- | ------ | -------- | -------- | ---------- | | 0 | DAY | 2023-02-04 | 2023-02-04T00:00:00.000000Z | 2023-02-04T23:59:59.940000Z | 1440000 | 71281136 | 68.0 MiB | FALSE | FALSE | TRUE | FALSE | FALSE | | 1 | DAY | 2023-02-05 | 2023-02-05T00:00:00.000000Z | 2023-02-05T21:59:59.940000Z | 1320001 | 65388544 | 62.4 MiB | FALSE | TRUE | TRUE | FALSE | FALSE | ``` ``` -------------------------------- ### Configure and Start Redpanda with Docker Compose Source: https://questdb.com/docs/ingestion/message-brokers/redpanda This YAML file configures a single Redpanda broker and the Redpanda Console for management. Ensure Docker and a JDK are installed. ```yaml --- version: "3.7" name: redpanda-quickstart networks: redpanda_network: driver: bridge volumes: redpanda-0: null services: redpanda-0: command: - redpanda - start - --kafka-addr - internal://0.0.0.0:9092,external://0.0.0.0:19092 # use the internal addresses to connect to the Redpanda brokers' # from inside the same Docker network. # # use the external addresses to connect to the Redpanda brokers' # from outside the Docker network. # # address the broker advertises to clients that connect to the Kafka API. - --advertise-kafka-addr - internal://redpanda-0:9092,external://localhost:19092 - --pandaproxy-addr - internal://0.0.0.0:8082,external://0.0.0.0:18082 # address the broker advertises to clients that connect to PandaProxy. - --advertise-pandaproxy-addr - internal://redpanda-0:8082,external://localhost:18082 - --schema-registry-addr - internal://0.0.0.0:8081,external://0.0.0.0:18081 # Redpanda brokers use the RPC API to communicate with eachother internally. - --rpc-addr - redpanda-0:33145 - --advertise-rpc-addr - redpanda-0:33145 # tells Seastar (the framework Redpanda uses under the hood) to use 1 core on the system. - --smp 1 # the amount of memory to make available to Redpanda. - --memory 1G # the amount of memory that's left for the Seastar subsystem. # For development purposes this is set to 0. - --reserve-memory 0M # Redpanda won't assume it has all of the provisioned CPU # (to accommodate Docker resource limitations). - --overprovisioned # enable logs for debugging. - --default-log-level=debug image: docker.redpanda.com/vectorized/redpanda:v22.3.11 container_name: redpanda-0 volumes: - redpanda-0:/var/lib/redpanda/data networks: - redpanda_network ports: - 18081:18081 - 18082:18082 - 19092:19092 - 19644:9644 console: container_name: redpanda-console image: docker.redpanda.com/vectorized/console:v2.1.1 networks: - redpanda_network entrypoint: /bin/sh command: -c 'echo "$$CONSOLE_CONFIG_FILE" > /tmp/config.yml; /app/console' environment: CONFIG_FILEPATH: /tmp/config.yml CONSOLE_CONFIG_FILE: | kafka: brokers: ["redpanda-0:9092"] schemaRegistry: enabled: true urls: ["http://redpanda-0:8081"] redpanda: adminApi: enabled: true urls: ["http://redpanda-0:9644"] ports: - 8080:8080 depends_on: - redpanda-0 ``` -------------------------------- ### Create Partitioned Table and Generate Sample Data Source: https://questdb.com/docs/operations/data-retention Sets up a table partitioned by day and populates it with hourly data for demonstration purposes. This example uses `timestamp_sequence` and `long_sequence` functions. ```questdb-sql CREATE TABLE my_table (timestamp TIMESTAMP, x LONG) timestamp(timestamp) PARTITION BY DAY; INSERT INTO my_table SELECT timestamp_sequence( to_timestamp('2021-01-01T00:00:00', 'yyyy-MM-ddTHH:mm:ss'),100000L * 36000), x FROM long_sequence(120); ``` -------------------------------- ### Get Day of Week (Sunday First) Source: https://questdb.com/docs/query/functions/date-time Returns the day number in a week, where Sunday is 1 and Saturday is 7. Use this when your week definition starts on Sunday. ```questdb-sql SELECT to_str(ts,'EE'),day_of_week_sunday_first(ts) FROM myTable; ``` -------------------------------- ### Squash Partitions Example Source: https://questdb.com/docs/query/sql/alter-table-squash-partitions Demonstrates squashing split partitions for table 'x' and verifying the merge. Use SHOW PARTITIONS to inspect partition states before and after. ```questdb-sql ALTER TABLE x SQUASH PARTITIONS; SHOW PARTITIONS FROM x; ``` -------------------------------- ### Import Log for Non-Partitioned Tables Source: https://questdb.com/docs/ingestion/import-csv Example of log entries for importing into non-partitioned tables, which uses a serial import and reports only start and finish records in the status table. ```questdb-sql SELECT * FROM sys.text_import_log WHERE id = '42d31603842f771a'; ``` -------------------------------- ### Get First Value in Partition Source: https://questdb.com/docs/query/functions/window-functions/reference Retrieves the first value within a partition, useful for establishing a baseline comparison or identifying the start of a sequence. Supports ignoring nulls. ```questdb-sql SELECT symbol, price, timestamp, first_value(price) OVER ( PARTITION BY symbol ORDER BY timestamp ) AS first_price, first_value(price) IGNORE NULLS OVER ( PARTITION BY symbol ORDER BY timestamp ) AS first_non_null_price FROM trades WHERE timestamp IN '$today'; ``` -------------------------------- ### Configure and Use QuestDB C Client Source: https://questdb.com/docs/ingestion/clients/c-and-cpp Example of configuring and initializing the QuestDB C client for data ingestion using a direct configuration string. ```c #include ... line_sender_utf8 conf = QDB_UTF8_LITERAL( "http::addr=localhost:9000;"); line_sender_error *error = NULL; line_sender *sender = line_sender_from_conf( line_sender_utf8, &error); if (!sender) { /* ... handle error ... */ } ``` -------------------------------- ### Connect to QuestDB using tokio-postgres Source: https://questdb.com/docs/query/pgwire/rust Basic example demonstrating how to establish a connection and execute a query using a connection string. ```rust use tokio_postgres::{NoTls, Error}; #[tokio::main] async fn main() -> Result<(), Error> { let connection_string = "host=localhost port=8812 user=admin password=quest dbname=qdb"; let (client, connection) = tokio_postgres::connect(connection_string, NoTls).await?; // Spawn a background task that keeps the connection alive for its entire lifetime // This task will terminate only when the connection closes tokio::spawn(async move { if let Err(e) = connection.await { eprintln!("Connection error: {}", e); } }); // Use the client to execute a simple query let rows = client.query("SELECT version()", &[]).await?; // Print the version let version: &str = rows[0].get(0); println!("QuestDB version: {}", version); Ok(()) } ``` -------------------------------- ### Execute SQL Query using GET /exec Source: https://questdb.com/docs/query/rest-api This endpoint allows executing SQL queries against QuestDB and retrieving results in JSON format. Examples are provided for cURL, Python, NodeJS, and Go. ```APIDOC ## GET /exec ### Description Executes a SQL query and returns the results as JSON. Useful for creating tables and inserting data. ### Method GET ### Endpoint /exec ### Parameters #### Query Parameters - **query** (string) - Required - The SQL query to execute. - **fmt** (string) - Optional - The desired response format. Use 'json' for JSON output. ### Request Example ```shell # Create Table curl -G \ --data-urlencode "query=CREATE TABLE IF NOT EXISTS trades(name STRING, value INT)" \ http://localhost:9000/exec # Insert a row curl -G \ --data-urlencode "query=INSERT INTO trades VALUES('abc', 123456)" \ http://localhost:9000/exec ``` ### Response #### Success Response (200) - The response contains the results of the SQL query in JSON format. #### Response Example ```json // Example response for a successful query { "query": "CREATE TABLE IF NOT EXISTS trades(name STRING, value INT)", "columns": [], "rows": [], "timestamp": 1678886400000000 } ``` ``` -------------------------------- ### Connect to QuestDB with Npgsql Source: https://questdb.com/docs/query/pgwire/dotnet Example demonstrating how to establish a connection to QuestDB using Npgsql with the required compatibility settings. ```csharp using Npgsql; namespace QuestDBExample { class Program { static async Task Main(string[] args) { AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); // Connection string with required ServerCompatibilityMode=NoTypeLoading string connectionString = "Host=localhost;Port=8812;Username=admin;Password=quest;Database=qdb;" + "ServerCompatibilityMode=NoTypeLoading;"; try { await using var connection = new NpgsqlConnection(connectionString); await connection.OpenAsync(); Console.WriteLine("Connected to QuestDB successfully!"); await connection.CloseAsync(); } catch (Exception ex) { Console.WriteLine($"Error connecting to QuestDB: {ex.Message}"); } } } } ``` -------------------------------- ### C/C++ Client Configuration String Example Source: https://questdb.com/docs/ingestion/clients/c-and-cpp Illustrates the general structure of a configuration string for the C/C++ QuestDB client, specifying transport, host, port, and parameters. ```plain ::addr=host:port;param1=val1;param2=val2;... ``` -------------------------------- ### QuestDB Command Options (Windows) Source: https://questdb.com/docs/getting-started/quick-start This outlines the command-line options for `questdb.exe` on Windows, including starting, stopping, status checks, installation, removal, specifying the data directory, forcing web console re-deployment, setting JAVA_HOME, and tagging services. ```shell questdb.exe [start|stop|status|install|remove] \ [-d dir] [-f] [-j JAVA_HOME] [-t tag] ``` -------------------------------- ### Initialize QuestDB C Client from Environment Variable Source: https://questdb.com/docs/ingestion/clients/c-and-cpp Example of initializing the QuestDB C client for data ingestion when the configuration is set via the QDB_CLIENT_CONF environment variable. ```c #include ... line_sender *sender = line_sender_from_env(&error); ``` -------------------------------- ### Show all parameters Source: https://questdb.com/docs/query/sql/show Displays all configuration parameters, their environment variable names, current values, how they are set, and sensitivity. Useful for understanding the current system configuration. ```questdb-sql SHOW PARAMETERS; ``` -------------------------------- ### QuestDB TICK Quick Start Examples Source: https://questdb.com/docs/query/operators/tick Common patterns for using TICK syntax to query time series data, including last hour, last 30 minutes, today, last business days, and specific times with durations. ```questdb-sql -- Last hour of data WHERE ts IN '$now - 1h..$now' -- Last 30 minutes WHERE ts IN '$now - 30m..$now' -- Today's data (full day) WHERE ts IN '$today' -- Last 5 business days WHERE ts IN '$today - 5bd..$today - 1bd' -- All workdays in January at 09:00 for 8 hours WHERE ts IN '[2024-01]T09:00#workday;8h' -- All of 2024 at 09:30 WHERE ts IN '[2024]T09:30' -- Multiple times on one day WHERE ts IN '2024-01-15T[09:00,12:00,18:00];1h' -- With timezone WHERE ts IN '2024-01-15T09:30@America/New_York;6h30m' ``` -------------------------------- ### Install Dagster and Dependencies Source: https://questdb.com/docs/integrations/orchestration/dagster Installs Dagster, the webserver, and the psycopg library for PostgreSQL interaction. Ensure Python 3.9+ is installed. ```bash pip install dagster dagster-webserver psycopg ``` -------------------------------- ### Manual Copy Partition Preparation Source: https://questdb.com/docs/query/sql/alter-table-attach-partition Example commands to download and prepare partition data from S3 for attachment. ```bash cd /var/lib/questdb/db/x # Table x is the original table where the partition were detached from. mkdir 2019-02-01.attachable && aws s3 cp s3://questdb-internal/blobs/20190201.tar.gz - | tar xvfz - -C 2019-02-01.attachable --strip-components 1 mkdir 2019-02-02.attachable && aws s3 cp s3://questdb-internal/blobs/20190202.tar.gz - | tar xvfz - -C 2019-02-01.attachable --strip-components 1 ``` -------------------------------- ### Install questdb-connect Package Source: https://questdb.com/docs/integrations/other/sqlalchemy Install the QuestDB Connect package for SQLAlchemy using pip. Ensure you have Python 3.9-3.11, Psycopg2, and SQLAlchemy <= 1.4.47 installed. ```shell pip install questdb-connect ```