querybara query
- CLI
- PostgreSQL
- MySQL
- MariaDB
- MongoDB
- Redis / Valkey
- Elasticsearch
querybara query runs statements against a target and prints the results. The input comes from
-e, from a file, or from stdin. On PostgreSQL, MySQL and MariaDB it runs SQL statements one by
one; on MongoDB, Redis and Elasticsearch it runs that engine’s commands or requests. Use it for
reports, checks in CI, and one-off statements.
Usage: querybara query [options] <target>
run SQL statements one by one from -e, a file, or stdin
Arguments: target profile name or id, or connection URI
Options: -e, --execute <sql> SQL to run (several statements allowed) -f, --file <path> a .sql file to run, streamed ("-" for stdin) --format <format> result format (choices: "table", "csv", "tsv", "json", "jsonl", default: "table") -p, --param <name=value> bind a placeholder (:name, $1 or ?) to a value; repeatable --stop-on-error stop at the first failed statement (default) --continue keep going after a failed statement --error-log <file> write failed statements and their errors to a file --row-limit <n> print at most n rows per result set (0: all) (default: 0) --max-column-width <n> table format: cut longer cells with … --database <name> database to connect to --read-only refuse statements that write -y, --yes run statements that need confirmation without asking --tls <mode> TLS mode for this run: disable, require, verify-ca or verify-full --ssh <user@host[:port]> reach URI targets through this SSH server; repeat for jump hosts, in order --ssh-key <path> SSH private key file (OpenSSH, PEM or PuTTY .ppk) --ssh-password-env <VAR> take the SSH password from this variable (default QUERYBARA_SSH_PASSWORD, else a prompt) --ssh-agent log in with the keys of ssh-agent (SSH_AUTH_SOCK) or Pageant --proxy <url> reach URI targets (or their first SSH server) through socks5://host:port or http://host:port --ssh-accept-new trust and remember an SSH host key not seen before (a changed key is always refused) --known-hosts <path> SSH known hosts file (default: the desktop app's) -h, --help show help for a command
Statements run in order, each in its own auto-commit unless the script opens a transaction.Files stream through the statement splitter (DELIMITER, dollar quoting and comments arehandled), so large dumps run in flat memory; progress shows on stderr in a terminal.
Results go to stdout; row counts, timings and notices to stderr. csv, tsv, json and jsonlare exact and stream: CSV writes NULL as an empty field and '' as "", TSV follows PostgreSQLCOPY text (\N for NULL), JSON keeps bigints exact and writes binary as base64.
Safety: UPDATE/DELETE without WHERE, DROP and TRUNCATE ask for confirmation (or need--yes); production profiles confirm every write; read-only profiles refuse writes.Placeholders are bound only when --param is given. Ctrl+C cancels the running statement.
Examples: querybara query prod -e "select * from users where id = :id" --param id=42 querybara query "postgres://app@localhost/app" -f migrate.sql --continue --error-log errors.log querybara query dev -f dump.sql --yes --quiet cat report.sql | querybara query dev --format csv > report.csv querybara query "mysql://[email protected]/app" --ssh ops@jump:22 --ssh ops@bastion --ssh-key ~/.ssh/id_ed25519 -e "select 1"
Redis targets (redis://, rediss:// or a Redis profile) run redis-cli command lines, one perline, and print redis-cli's output (--format json or jsonl: the replies as JSON); DEL, FLUSHDBand other destructive commands ask for confirmation (or need --yes): querybara query "redis://localhost:6379/0" -e 'SET greeting "hello world"'
Elasticsearch targets (http://, https:// or a saved profile) run Kibana consolerequests: a method and path per request, then its JSON body (NDJSON lines for _bulk); eachresponse body prints as JSON with numbers exactly as sent (--format json or jsonl: objects withthe request, status and body). Deleting or closing indices, delete by query and otherdestructive requests ask for confirmation (or need --yes); an error status fails the request: querybara query "http://elastic@localhost:9200" -e 'GET _cluster/health' querybara query search-prod -f requests.txt --format jsonlThe --tls and SSH options are described in
Global options.
Run SQL
Section titled “Run SQL”Statements run in order, each in its own auto-commit unless the script opens a transaction. A
file streams through the statement splitter, which handles DELIMITER, dollar quoting and
comments, so large dumps run in flat memory.
querybara query shop-prod -e "select id, total from shop.orders where customer_id = :id" --param id=42querybara query "postgres://[email protected]/shop" -f migrate.sql --continue --error-log errors.logcat report.sql | querybara query shop-dev --format csv > report.csvPlaceholders (:name, $1 or ?) are bound only when you pass --param.
If the script leaves a transaction open, the CLI rolls it back at the end and warns.
Output formats
Section titled “Output formats”Results go to stdout; row counts, timings and notices go to stderr.
--format |
Output |
|---|---|
table |
A text table (default). --max-column-width cuts longer cells |
csv |
CSV. NULL is an empty field and an empty string is "" |
tsv |
PostgreSQL COPY text: \N for NULL |
json |
JSON. Bigints stay exact; binary values are base64 |
jsonl |
One JSON object per line |
csv, tsv, json and jsonl are exact and stream as rows arrive. --row-limit prints at most
that many rows per result set.
MongoDB, Redis and Elasticsearch
Section titled “MongoDB, Redis and Elasticsearch”On a MongoDB target, each input is a command document, in Extended JSON or shell syntax. An array
of documents runs in order. Documents print as relaxed Extended JSON; --format json prints
canonical Extended JSON as an array, and jsonl one canonical document per line.
querybara query "mongodb://[email protected]/catalog" -e '{ find: "products", filter: { price: { $gt: 100 } } }'On a Redis target (redis://, rediss:// or a Redis profile), each line is a redis-cli command
line, and the output is redis-cli’s. --format json or jsonl prints the replies as JSON.
querybara query "redis://cache.example.com:6379/0" -e 'HGETALL cart:8812'On an Elasticsearch target (http://, https:// or a saved profile), the input is console
requests: a method and path per request, then its JSON body (NDJSON lines for _bulk). Each
response body prints as JSON with numbers exactly as sent. An error status fails the request.
querybara query search-prod -f requests.txt --format jsonlSafety
Section titled “Safety”UPDATEorDELETEwithoutWHERE,DROPandTRUNCATEask for confirmation, or need--yes.- Profiles whose environment is production, or that confirm writes, ask before every write.
- Read-only profiles refuse writes, and so does any target with
--read-only. - On MongoDB, a read-only target refuses commands that write; drops, multi-document updates and
deletes, and other destructive commands ask or need
--yes. - On Redis,
DEL,FLUSHDBand other destructive commands ask or need--yes. - On Elasticsearch, deleting or closing indices, delete by query and other destructive requests
ask or need
--yes.
In a terminal you answer [y]es, [N]o, [a]ll; all runs the remaining statements of the run
without asking again. Without a terminal, and whenever the script comes from stdin, a statement
that needs confirmation fails unless you pass --yes.
Ctrl + C cancels the running statement on the server. A second Ctrl + C quits at once.
Exit codes
Section titled “Exit codes”| Code | Meaning |
|---|---|
| 0 | Every statement ran |
| 2 | A statement failed or was refused (also with --continue), or another error |
| 130 | Interrupted |
To run a file and get exit code 1 when some statements failed but the run went on, use
querybara run-file with --continue.
Related
Section titled “Related”Documents Querybara 0.1.1 · built frombc9f5aa