# CLI

The keeldb command line for databases, branches, connection strings, backups and snapshots, SQL over HTTP and PostgreSQL logs, with JSON output for scripts.

`keeldb` manages your databases from a terminal or a CI job. It is a thin client of the [REST API](/docs/api): everything it does, the API does, with the same API keys, permissions and limits.

## Install

The CLI is not on npm yet. It will be published as `keeldb` with KeelDB's public launch, and this section will then show how to install it; until then this page describes the CLI as it will ship. It needs Node.js 20 or later and has no other dependencies.

Until this section shows the install command, a package named `keeldb` on npm is not KeelDB's: do not install it, and never give it an API key.

## Log in

Create an API key under **API keys** in the console, then:

```bash
keeldb login
```

It asks for the key without echoing it, checks it against the API and saves it in `~/.config/keeldb/config.json` (or `$XDG_CONFIG_HOME/keeldb/config.json`; `KEELDB_CONFIG` names another file), readable only by you. `keeldb logout` deletes that file.

In CI, skip the file and set the key in the environment:

```bash
export KEELDB_API_KEY=keel_xxx
keeldb databases list
```

The key is looked up in this order: `--api-key`, then `KEELDB_API_KEY`, then the saved file. A key acts with the role and permissions chosen when it was created, never more than its creator's role, so `databases delete` needs a key that may delete databases (`db.delete`). See [API keys](/docs/security#api-keys). Keep the key in your CI's secret store.

The CLI talks to `https://keeldb.com/api/v1`. `--api-url <url>` or `KEELDB_API_URL` points it at another base URL, in that order before the saved file.

## Commands

| Command | What it does |
| --- | --- |
| `keeldb databases list` | Every database and branch of the organization |
| `keeldb databases get <id>` | One database: status, plan, hosts, storage, slow query threshold, protection, the snapshot it was restored from |
| `keeldb databases create <name> --plan <free\|launch\|dedicated>` | Creates a database. Dedicated also takes `--size` and `--topology`; any plan takes `--region`, `--postgres-version` and `--database`. `--wait` returns once it is ready |
| `keeldb databases delete <id>` | Deletes a database and its snapshots after you type its id, or with `--yes` |
| `keeldb branches list <id>` | The direct branches of a database |
| `keeldb branches create <id> <name>` | Creates a branch of the current state, or of a point in time with `--at <time>`; `--expires-at <time>` deletes it then; `--plan` and `--wait` as for databases |
| `keeldb branches delete <branch-id>` | Deletes a branch (refuses a root database) |
| `keeldb connection-string <id>` | Prints the connection URI, including the password. `--pooled` for the pooler, `--read-only` for the read-only host. Short form `keeldb cs` |
| `keeldb query <id> "<sql>"` | Runs SQL over HTTP as the database owner. `--file <path>` reads it from a file, `-` or no SQL reads standard input, `--read-only` runs one statement in a read-only transaction |
| `keeldb backups list <id>` | Backups, newest first, with their restore test. Backups are kept for the database's restore window |
| `keeldb backups create <id>` | Takes a backup now; `--wait` waits for it to complete |
| `keeldb snapshots list <id>` | [Snapshots](/docs/backups-and-restore#snapshots), newest first, with what each holds and its size. Snapshots are kept until you delete them |
| `keeldb snapshots create <id> <name>` | Takes a named snapshot; `--wait` waits until it is ready |
| `keeldb snapshots restore <id> <snapshot-id> <name> --to <branch\|database>` | Restores a ready snapshot into a new branch of the database or a new database, with its own hostname and password; `--wait` returns once it is ready |
| `keeldb snapshots delete <id> <snapshot-id>` | Deletes a snapshot after you type its name, or with `--yes`; it is no longer billed from then |
| `keeldb logs <id>` | PostgreSQL logs, newest first. `--kind error,slow_query,connection`, `--since` and `--until`, `--search`, `--min-duration-ms`, `--limit`, `--cursor` |

Times are ISO 8601 with `Z` or an offset, for example `2026-10-16T09:00:00Z`. `--wait` gives up after 15 minutes (`--timeout <seconds>`), and what it waited for keeps going.

## Examples

A preview database per pull request, deleted after a week:

```bash
id=$(keeldb branches create k7m2x9qa4pz1 "pr-$PR" --expires-at "$(date -u -d '+7 days' +%Y-%m-%dT%H:%M:%SZ)" --wait --json | jq -r .id)
export DATABASE_URL=$(keeldb connection-string "$id" --pooled)
```

Run a migration file and fail the job on an SQL error, with a snapshot taken first:

```bash
keeldb snapshots create k7m2x9qa4pz1 "before-0042" --wait
keeldb query k7m2x9qa4pz1 --file migrations/0042_add_orders.sql
```

The slow statements of the last hour:

```bash
keeldb logs k7m2x9qa4pz1 --kind slow_query --since "$(date -u -d '-1 hour' +%Y-%m-%dT%H:%M:%SZ)"
```

## JSON output

Add `--json` to any command to print the API's `data` as JSON instead of text, for `jq` and scripts. Errors are printed as JSON too:

```json
{ "error": { "status": 409, "code": "conflict", "message": "Delete its branches first: preview" } }
```

## Exit codes

| Code | Meaning |
| --- | --- |
| 0 | Done |
| 1 | The API refused or failed (the message and its `code` are printed), a query returned an SQL error, or a `--wait` ended in failure |
| 2 | The command line was wrong: an unknown command or flag, a missing argument, an id that is not a database or snapshot id |

The CLI tries a request at most 3 times (2 retries) when it gets `429 rate_limited` or `503 unavailable`, waiting the `Retry-After` the API sends, at most 30 seconds. See [Rate limits](/docs/api#rate-limits).
