<!-- Generated from apps/site/self-hosting.html by scripts/agent-docs.mjs. Edit the HTML, then rerun it. -->

# Self-hosting

> Run Haystash DB yourself, one server beside your Postgres: local setup, Docker Compose, settings, TLS, backups, restoring and upgrades.

Run one server process beside your own Postgres. To skip running a server, see [Hosted](/hosted.html) (invite-only, not open yet). `docs/db/SELF_HOSTING.md` in the repository has every setting, rate limits, timeouts, TLS to Postgres and how to run the tests.

## What runs

```
browsers, apps, backends
       │ HTTPS
reverse proxy (TLS)          ← you provide it
       │ HTTP
haystash-db (one process)    data directory: gate.master.key (opens the Key Gate's secrets),
  /gate  Key Gate                            admin.token, service key hash
  /v1    data API
  /admin admin API, /console/ the console
       │ SQL
Postgres 15 or later         your tables, and the Key Gate's (schema hs_gate: users,
                             devices, wrapped keys, the signed list of locked columns)
```

- **One server per app.** A second server on the same database refuses to start. No cluster mode, no failover.
- **Key Gate data** in `hs_gate` is readable only by the server's role; its secrets are encrypted under `gate.master.key`.
- **The Postgres role** must own its database and be able to create roles, but should not be a superuser.

## Run it on your machine

You need the repository, Rust, Node 22 and Postgres 15+ (for `initdb`, `pg_ctl` and `psql`).

```
scripts/db-dev.sh
```

This starts a private Postgres on `127.0.0.1:54329`, creates a non-superuser role that can create roles, then builds and starts the server. The first start prints two secrets:

```
Admin token: <32 characters>
  (stored in .haystash-db/dev/admin.token; read it there later)
Service key: hsk_<43 characters>
  (shown only this once: only its hash is kept; copy it now, or rotate it later in the console)

Haystash DB is running
  Console:     http://127.0.0.1:7400/console/
  Example app: http://127.0.0.1:7400/journal/
  API:         http://127.0.0.1:7400  (data API /v1, Key Gate /gate)
```

The **admin token** opens the console and admin API; the **service key** is for your backend (see [The console and keys](/docs.html#console)). The data directory `.haystash-db/dev` holds the Key Gate's master key: **lose it and every user loses their locked data for good.**

Continue with the [Quickstart](/docs.html#quickstart) from step 2 at `http://127.0.0.1:7400`, or without the console:

```
TOKEN=$(cat .haystash-db/dev/admin.token)
curl -s -X POST http://127.0.0.1:7400/admin/tables -H "x-admin-token: $TOKEN" \
  -H 'content-type: application/json' \
  -d '{"name":"notes","access":"owner","columns":[{"name":"title","type":"text"},{"name":"body","type":"text"}]}'
curl -s -X POST http://127.0.0.1:7400/admin/tables/notes/columns/body/lock -H "x-admin-token: $TOKEN" \
  -H 'content-type: application/json' -d '{}'
# {"manifest_version":1,"state":"locked"}
```

To stop: Ctrl-C in the `scripts/db-dev.sh` terminal, then `scripts/pg-dev.sh stop`.

## Docker Compose

`deploy/docker-compose.db.yml` runs Postgres 17 (not published on the host) and the server (on `127.0.0.1:7400` only). We haven’t tested this setup yet.

```
cp deploy/db.env.example deploy/.env
# put two different long random passwords in deploy/.env:
#   POSTGRES_PASSWORD and HAYSTASH_DB_PASSWORD (letters and digits only)
docker compose -f deploy/docker-compose.db.yml up -d --build
docker compose -f deploy/docker-compose.db.yml logs server | grep -E 'Admin token|Service key'
```

## Without Docker

```
cargo build --release -p haystash-db
DATABASE_URL=postgres://app_owner@db-host:5432/myapp HAYSTASH_DATA_DIR=/var/lib/haystash \
  target/release/haystash-db
```

## Settings

| Variable | Default | Meaning |
| --- | --- | --- |
| `DATABASE_URL` | required | Postgres connection string. Use `sslmode=require` (or `verify-full`) for a hosted Postgres. |
| `HAYSTASH_DATA_DIR` | `.haystash-db` | Key Gate master key, admin token, service key hash. Back it up with Postgres. |
| `PORT`, `HAYSTASH_BIND` | `7400`, `127.0.0.1` | Where it listens |
| `HAYSTASH_ORIGINS` | none | Browser origins allowed to call `/v1` and `/gate` from another site |
| `HAYSTASH_TRUST_PROXY` | `0` | `1` only behind a reverse proxy that sets `X-Forwarded-For` |
| `HAYSTASH_PASSWORD_SIGNIN` | `on` | `off` when your app signs people in with [tickets](/docs.html#own-sign-in) |
| `HAYSTASH_KEEPER` | `off` | The optional Keeper; a much weaker promise |
| `HAYSTASH_EXAMPLES` | `0` | `1` serves the example app at `/journal/`; not for production |

Each setting also works as a flag (`--port 9000`).

## TLS in front

The server speaks plain HTTP, and requests carry passwords, session tokens and the admin token. Anything beyond `127.0.0.1` must go through a TLS-terminating reverse proxy, such as nginx:

```
server {
  listen 443 ssl;
  server_name db.example.com;
  ssl_certificate     /etc/ssl/db.example.com/fullchain.pem;
  ssl_certificate_key /etc/ssl/db.example.com/privkey.pem;
  client_max_body_size 8m;

  location / {
    proxy_pass http://127.0.0.1:7400;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;
  }
}
```

Then set `HAYSTASH_TRUST_PROXY=1`.

## Backups

**Back up Postgres and the data directory together, at the same moment.** Postgres holds your tables and the Key Gate's (users, encrypted keys, the signed list of locked columns); `gate.master.key` in the data directory opens those keys. **Lose that file and every user loses their locked data for good**; nobody, including you, can recover it.

```
# stop the server first
pg_dump -Fc -f backup.dump "$DATABASE_URL"
cp -a "$HAYSTASH_DATA_DIR" backup-data      # all of it
# start the server again, after pg_dump has finished
```

Store the master key file apart from the dumps, so a dump alone opens nothing. Backups from before a column was locked still hold its plaintext; expire them on your own schedule.

## Restoring a backup

With the server stopped, restore Postgres and the data directory from the same backup. Keep users away until you:

1. **Skip versions** of the signed list of locked columns: `POST /admin/manifest/skip` signs the same list at a higher version. Otherwise devices that saw a newer list refuse it (`manifest_rollback`), since a restore looks like a server rolling a lock back.
2. **Redo every lock** (and drop) made after the backup.

Everything since the backup is gone, including new accounts. Exact commands: `docs/db/SELF_HOSTING.md`.

## Upgrades

1. Back up Postgres and the data directory.
2. Stop the server, replace the binary and static files (`apps/`, `packages/db/`), start it.
3. On start it adds anything missing to its schemas and checks every lock constraint against the signed list. A missing constraint stops it with a message.

**From a release that kept the Key Gate in `gate.db`:** the first start moves it into Postgres (`hs_gate`) in one transaction, renames it `gate.db.migrated` and logs the row count. It runs once, and refuses without changing anything if `hs_gate` already has users or the file's secrets don't open under `gate.master.key` (wrong data directory). Some users may need to sign in again. Keep `gate.db.migrated` with your backups until you trust the move, then delete it: it still holds encrypted secrets and password hashes.

There is no downgrade, and upgrades between released versions are untested.
