Self-hosting
Run Haystash DB yourself: one server process beside your own Postgres. This is how it works today. If you'd rather not run a server, see Hosted (invite-only, not open yet).
This page is the short version. docs/db/SELF_HOSTING.md in the repository has every setting, rate limits and 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, beside its Postgres. A second server started on the same database refuses to start. There is no cluster mode and no failover.
- The Key Gate's data is in Postgres, in schema
hs_gate, which only the server's own role can read. Its secrets are encrypted undergate.master.keyin the data directory. A database dump without that file opens nothing. - The Postgres role must own its database and be able to create roles. It should not be a superuser.
Run it on your machine
You need the repository, Rust, Node 22, and Postgres 15 or later installed (the helper scripts use its initdb, pg_ctl and psql).
scripts/db-dev.sh
This starts a private Postgres on 127.0.0.1:54329, creates a database role that can create roles but is not a superuser, builds the server and starts it. On its first start the server 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 the admin API. The service key is for your backend (see The console and keys). The data directory (.haystash-db/dev) holds the master key of the Key Gate. Losing it locks every user out of their locked data for good.
Then follow the Quickstart from step 2 with http://127.0.0.1:7400 as the address. The same steps 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, press Ctrl-C in the terminal running scripts/db-dev.sh, then run scripts/pg-dev.sh stop.
Docker Compose
deploy/docker-compose.db.yml runs Postgres 17 and the server. Postgres is not published on the host. The server listens on 127.0.0.1:7400 only.
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'
The Docker Compose setup was not tested on the machine these docs were written on.
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 | The data directory: the Key Gate's master key, the admin token, the service key's 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 |
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 can also be a flag (--port 9000). Rate limits and the full list are in docs/db/SELF_HOSTING.md.
TLS in front
The server speaks plain HTTP. Anything beyond 127.0.0.1 must reach it through a reverse proxy that terminates TLS, because passwords, session tokens and the admin token travel in requests. A minimal nginx block:
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;
}
}
Behind it, set HAYSTASH_TRUST_PROXY=1.
Backups
Back up Postgres and the data directory together, at the same moment. One without the other is not a backup. Postgres holds your tables and the Key Gate's (users, their encrypted keys, the signed list of locked columns). The data directory holds gate.master.key, the key that opens those keys. If that file is lost, every user is locked out of their locked data for good, and nobody, including you, can get it back.
# 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
Keep the master key file somewhere other than the database dumps: a dump alone opens nothing, and that is the point. Backups made before a column was locked still hold its old values in plaintext. Expire them on your own schedule.
Restoring a backup
Restore Postgres and the data directory together, from the same backup, with the server stopped. Then keep users away until you have done two things:
- Skip versions of the signed list of locked columns. Devices that saw a newer list refuse an older one (
manifest_rollback): they can't tell a restore from a server rolling a lock back.POST /admin/manifest/skipsigns the same list at a higher version. - Redo every lock (and every drop) made after the backup.
Everything since the backup is gone, including accounts created since. The exact commands are in docs/db/SELF_HOSTING.md.
Upgrades
- Back up Postgres and the data directory.
- Stop the server, replace the binary and the static files (
apps/,packages/db/), start it. - At start the server adds what is missing to its schemas and checks every lock constraint against the signed list of locked columns. A missing one stops it, with a message.
Upgrading from a release that kept the Key Gate in gate.db: the first start moves gate.db into Postgres (hs_gate), all of it in one transaction, and renames the file gate.db.migrated. The log says how many rows moved. It does this once. It refuses, and changes nothing, if hs_gate already holds users or if the file's secrets don't open under gate.master.key (the wrong data directory). Some users may have 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. An upgrade between released versions has not been tested yet.