QuestDB Python Client

4.1.0 · active · verified Fri Apr 17

The `questdb` library is the official Python client for QuestDB, a high-performance open-source SQL database for time-series and analytics. It provides efficient ingestion of data via InfluxDB Line Protocol (ILP) over TCP or HTTP, supporting various Python data types, Pandas DataFrames, and NumPy arrays. The current version is 4.1.0, and releases are typically feature-driven, often aligning with new QuestDB server capabilities.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `Sender` using a configuration string for ILP over HTTP and ingest two rows of data. It also highlights the support for nanosecond precision timestamps introduced in version 4.0.0. The connection details can be configured via environment variables for easy deployment.

import os
import time
from questdb.client import Sender
from datetime import datetime, timezone

# Configure QuestDB connection (ILP over HTTP)
# Use os.environ.get for security and flexibility in deployment
QDB_HOST = os.environ.get("QDB_HOST", "localhost")
QDB_PORT = os.environ.get("QDB_PORT", "9000")
QDB_AUTH = os.environ.get("QDB_AUTH", "") # Format: "username:token"

conf = f'http::addr={QDB_HOST}:{QDB_PORT};'
if QDB_AUTH:
    conf += f'auth={QDB_AUTH};' # Assumes auth syntax for QuestDB 7.0+
else:
    print("Warning: QDB_AUTH environment variable not set. Connecting without authentication.")

try:
    with Sender.from_conf(conf) as sender:
        # Ingest a single row of data
        sender.row(
            "sensor_data",
            columns={
                "location": "london",
                "temperature": 15.5,
                "humidity": 70,
                "event_time": datetime.now(timezone.utc)
            },
            at=datetime.now(timezone.utc) # QuestDB timestamp column
        )

        # Ingest another row, demonstrating nanosecond precision (client v4.0.0+)
        # Note: Requires QuestDB server 9.1.0+ for nanosecond TIMESTAMP_NS type
        nanos_timestamp = int(time.time_ns()) # Current time in nanoseconds
        sender.row(
            "sensor_data",
            columns={
                "location": "paris",
                "temperature": 18.2,
                "humidity": 65,
                "event_time": datetime.now(timezone.utc)
            },
            at=nanos_timestamp # This sends nanosecond precision
        )

        sender.flush() # Ensure all buffered data is sent
        print("Data sent successfully to QuestDB!")

except Exception as e:
    print(f"Failed to send data: {e}")
    print(f"Please ensure QuestDB server is running and accessible at http://{QDB_HOST}:{QDB_PORT}")

view raw JSON →