> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cockroachlabs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Setup

> This page describes how to start from an empty environment and reach a running order service.

This page describes how to start from an empty environment and reach a running order service. Each step includes a verification check. Complete the check before proceeding to the next step.

## Prerequisites

| Requirement | Minimum | Check |
| - | - | - |
| CockroachDB | **v26.2.7+** (latest LTS) | `cockroach version` |
| SQL client | `cockroach sql`, or any Postgres driver | N/A |
| App runtime | your language + a Postgres driver (pgx, pg, psycopg, …) | N/A |

CockroachDB is compatible with the PostgreSQL wire protocol. Existing drivers and ORMs work with minimal change.

## 1. Start a cluster

**Local, single node** (development):

```bash theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
cockroach start-single-node --insecure \
  --listen-addr=localhost:26257 --http-addr=localhost:8080
```

✅ **Verify:** `cockroach sql --insecure -e "SELECT 1"` prints `1`. The DB Console is at `http://localhost:8080`.

> [!NOTE]
>
> **Other targets**
> `cockroach demo` starts a temporary in-memory cluster for evaluating SQL. For a managed cluster, set `DATABASE_URL` to a CockroachDB Cloud connection string. The schema and application are identical.

## 2. Create the database

```bash theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
cockroach sql --insecure -e "CREATE DATABASE IF NOT EXISTS ordermgmt;"
```

✅ **Verify:** `SHOW DATABASES;` lists `ordermgmt`.

## 3. Apply the schema

The reference implementation defines the schema in [`reference/schema.sql`](https://github.com/cockroachlabs/cockroach-cookbook/blob/blueprints/order-management/v1.0.0/blueprints/order-management/reference/schema.sql), which is the authoritative contract for the data model (see [Data model](https://docs.cockroachlabs.com/docs/cookbook/order-management/schema-design.md)). It includes Row-Level TTL on the reservations, idempotency, and outbox tables.

```bash theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
cockroach sql --insecure -d ordermgmt -f reference/schema.sql
```

✅ **Verify:**

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
SHOW TABLES;
-- expect: orders, order_lines, order_events, idempotency_keys,
--         inventory, reservations, payment_transactions, payment_events,
--         shipments, shipment_lines, returns, return_lines, outbox
```

## 4. Seed data

```bash theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
cockroach sql --insecure -d ordermgmt -f reference/seed.sql
```

✅ **Verify:** `SELECT count(*) FROM inventory;` returns the seeded SKUs. `SKU-HOT` is seeded with `on_hand = 1` for the oversell test.

## 5. Run the reference implementation

The reference handlers and tests read one environment variable:

```bash theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
export DATABASE_URL="postgresql://root@localhost:26257/ordermgmt?sslmode=disable"
cd reference
pip install -r requirements.txt
python -m pytest            # runs the regression suite
```

✅ **Verify:** the suite passes, including the 50-concurrent-buyers oversell test. See [Reference app](https://docs.cockroachlabs.com/docs/cookbook/order-management/reference-app.md) for what each handler does.

## 6. Check the accounting invariants

```bash theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
cockroach sql --insecure -d ordermgmt -f reference/verification.sql
```

✅ **Verify:** every check returns zero rows. See [Verification](https://docs.cockroachlabs.com/docs/cookbook/order-management/verification.md).

For an interactive walkthrough that connects a cluster from the browser and runs the same scenarios, use the [`agent-demo/`](https://github.com/cockroachlabs/cockroach-cookbook/tree/blueprints/order-management/v1.0.0/blueprints/order-management/agent-demo) app (`cd agent-demo && npm install && npm start`).
