Skip to main content

QuestDB REST API Python client library and CLI

Project description

QuestDB REST API Python Client, CLI and REPL Shell

QuestDB comes with a very nice web console, but there's no CLI, so I wrote one (can't live without the terminal!).

The REST API is very well defined: https://questdb.com/docs/reference/api/rest/, only 3 documented endpoints. One undocumented endpoints I also implemented are /chk to check for if a table exists, I found the route when trying to ingest CSV via the web console.

Features beyond what the vanilla REST API provides

Docs, screenshots and video demos

Originally I just wrote the CLI (cli.py), then it becomes really complicated that I had to split the code and put the REST API interfacing part into a module (__init__.py).

imp programmatically derives table name from filename when uploading CSVs

questdb-cli imp options that are not part of the REST API spec:

  --name-func {stem,add_prefix}
                        Function to generate table name from filename (ignored if --name set). Available: stem, add_prefix (default: None)
  --name-func-prefix NAME_FUNC_PREFIX
                        Prefix string for 'add_prefix' name function. (default: )
  -D, --dash-to-underscore
                        If table name is derived from filename (i.e., --name not set), convert dashes (-) to underscores (_). Compatible with --name-func. (default: False)

Global flag --stop-on-error controls if it should stop talking to the API on first CSV import error or not.

exec supports multiple queries in one go

The API and web console will only take your last query if you attempt to give it more than 1, while this project uses sqlparser to split the queries and send them one by one for you for convenience. Global flag --stop-on-error controls if it should stop talking to the API on first error or not. Since the API doesn't always return a status code other than 200 on error, I dived in to the Dev Tools to see what exactly tells me if a request is successful or not.

The queries can be piped in from stdin, or read from a file, or you can supply it from the command line.

Query output parsing and formatting

The /exec endpoints only speaks JSON, this tool gives you options to format the output table to as markdown with --markdown or a psql-style ASCII table with --psql (default is JSON).

For CSV output, use questdb-cli exp instead.

Global options to fine tune log levels

qdb-cli -h

usage: questdb-cli [-h] [-H HOST] [--port PORT] [-u USER] [-p PASSWORD]
                   [--timeout TIMEOUT] [--scheme {http,https}] [-i | -D] [-R]
                   [--config CONFIG] [--stop-on-error | --no-stop-on-error]
                   {imp,exec,exp,chk,schema,gen-config} ...
QuestDB REST API Command Line Interface.
Logs to stderr, outputs data to stdout.
Uses QuestDB REST API via questdb_rest library.
positional arguments:
  {imp,exec,exp,chk,schema,gen-config}
                        Available sub-commands
    imp                 Import data from file(s) using /imp.
    exec                Execute SQL statement(s) using /exec (returns JSON).
                        Reads SQL from --query, --file, --get-query-from-python-module, or stdin.
    exp                 Export data using /exp (returns CSV to stdout or file).
    chk                 Check if a table exists using /chk (returns JSON). Exit code 0 if exists, 3 if not.
    schema              Fetch CREATE TABLE statement(s) for one or more tables.
    gen-config          Generate a default config file at ~/.questdb-rest/config.json
options:
  -h, --help            Show this help message and exit.
  -H HOST, --host HOST  QuestDB server host.
  --port PORT           QuestDB REST API port.
  -u USER, --user USER  Username for basic authentication.
  -p PASSWORD, --password PASSWORD
                        Password for basic authentication. If -u is given but -p is not, will prompt securely unless password is in config.
  --timeout TIMEOUT     Request timeout in seconds.
  --scheme {http,https}
                        Connection scheme (http or https).
  -i, --info            Use info level logging (default is WARNING).
  -D, --debug           Enable debug level logging to stderr.
  -R, --dry-run         Simulate API calls without sending them. Logs intended actions.
  --config CONFIG       Path to a specific config JSON file (overrides default ~/.questdb-rest/config.json).
  --stop-on-error, --no-stop-on-error
                        Stop execution immediately if any item (file/statement/table) fails.

schema

Convenience command to fetch schema for 1 or more tables. Hard to do without reading good chunk of the QuestDB doc. The web console supports copying schemas from the tables list.

chk

The chk command to talk to /chk endpoint, which is used by the web console's CSV upload UI.

PyPI packages and installation

questdb-cli, questdb-rest and questdb-api are the same package (just aliases), with questdb-rest guaranteed to be the most updated.

Installing any of them will give you the questdb-cli and qdb-cli commands (same thing).

Install (Python >=3.11 required):

uv tool install questdb-rest
pipx install questdb-rest
# not recommended, but if you really want to:
pip install questdb-rest

The Python API

These classes are provided with extensive methods to interact with the REST API (it's all in __init__.py).

QuestDBError
QuestDBConnectionError
QuestDBAPIError
QuestDBClient

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

questdb_api-1.3.2.tar.gz (25.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

questdb_api-1.3.2-py3-none-any.whl (25.3 kB view details)

Uploaded Python 3

File details

Details for the file questdb_api-1.3.2.tar.gz.

File metadata

  • Download URL: questdb_api-1.3.2.tar.gz
  • Upload date:
  • Size: 25.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.9 Darwin/24.3.0

File hashes

Hashes for questdb_api-1.3.2.tar.gz
Algorithm Hash digest
SHA256 ae4e9c0ff1a4026db334fe4496b3d91f431af015e08cee9c35d78a4ed73b012e
MD5 b252948066673488a61c1c0e7d235b7c
BLAKE2b-256 e0bd2b7370c21233502eb08da55ae91ade4cfb02d7b434b0658cf29d8406d238

See more details on using hashes here.

File details

Details for the file questdb_api-1.3.2-py3-none-any.whl.

File metadata

  • Download URL: questdb_api-1.3.2-py3-none-any.whl
  • Upload date:
  • Size: 25.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.9 Darwin/24.3.0

File hashes

Hashes for questdb_api-1.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3cde041ce107e613cb896c20d83cbc6dc9b73e220f2caf4d55f038d432f529b6
MD5 0734794b7f5ec8f05fee7144504eee3a
BLAKE2b-256 5007203b470a21bca6f9ee890d6760e98a877631a91a389c7af383396ff31ade

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page