diff --git a/.changeset/docs-setup-pg-version-check.md b/.changeset/docs-setup-pg-version-check.md new file mode 100644 index 000000000..4473faad8 --- /dev/null +++ b/.changeset/docs-setup-pg-version-check.md @@ -0,0 +1,4 @@ +--- +--- + +docs: check PG_VERSION before upgrading a 2.x relay diff --git a/docs/SETUP.md b/docs/SETUP.md index 6487f36af..7e2a27a2c 100644 --- a/docs/SETUP.md +++ b/docs/SETUP.md @@ -483,6 +483,59 @@ sudo docker compose logs -f --tail=100 nostream ### Updating the Relay When a new nostream version is released: +> **Coming from 2.x? Check the database version first.** Before 3.0 the compose file used the untagged `postgres` image, so `.nostr/data` was created by whatever major was `latest` at the time: 16, 17 or 18. 3.x pins `nostream-db` to `postgres:15`, and PostgreSQL can't open a data directory made by a newer major: `nostream-db` exits with `FATAL: database files are incompatible with server` and `nostream-migrate` and `nostream` wait on it. Look before you recreate anything: +> +> ```bash +> # the directory your compose file mounts at /var/lib/postgresql/data +> sudo cat /opt/nostream/.nostr/data/PG_VERSION # 3.x layout +> sudo cat ~/nostream/.nostr/data/PG_VERSION # a 2.x install that ran from the clone +> ``` +> +> Read whichever of those exists; if neither does, find where the old stack kept its data before you recreate anything, and stop if `PG_VERSION` is missing rather than updating blind. `15` needs nothing. For any other number, keep running that major with a `docker-compose.override.yml` next to `docker-compose.yml`. `docker compose` in `/opt/nostream` reads it on its own, `deploy/recreate-relay.sh` changes into the directory and runs a bare `docker compose` so it reads it too, and `bootstrap.sh` leaves it alone — but a command that names the file with `-f docker-compose.yml`, like the `compose` helper in step 5, does not: +> +> ```yaml +> services: +> nostream-db: +> image: postgres:17-bookworm # the number PG_VERSION printed +> ``` +> +> For 16 or 17 keep the C library the cluster was created with, because a changed one can invalidate collation-dependent indexes: `PG_VERSION` records the major and nothing else, so check the old image itself before choosing the variant — `sudo docker run --rm --entrypoint cat /etc/os-release` names the Debian release, and `postgres:16-bookworm`/`17-bookworm` (Debian 12) and `postgres:16-trixie`/`17-trixie` (Debian 13) both exist. An untagged pull made after `latest` moved to Trixie gives a Trixie-based cluster, so the older `-bookworm` is not automatically the match; when the image is gone and the base cannot be established, dump and restore into a fresh cluster of the same major, not into 15 — a 17 dump carries settings 15 rejects and PostgreSQL does not promise a newer dump restores into an older server. For 18 use `postgres:18` and also set `PGDATA`, because the 18 image looks for its data in `/var/lib/postgresql/18/docker` and refuses to start with a mount at `/var/lib/postgresql/data`: +> +> ```yaml +> services: +> nostream-db: +> image: postgres:18 +> environment: +> PGDATA: /var/lib/postgresql/data +> ``` +> +> Writing the override changes nothing on its own, and reading `PG_VERSION` under `~/nostream` does not put that cluster where the new project looks either: `/opt/nostream` mounts its own `.nostr/data`, and bootstrap creates it empty, so the database would initialise a fresh one. If the `PG_VERSION` you read was already under `/opt/nostream/.nostr/data`, the cluster is where it belongs and this move is unnecessary. Otherwise stop the old stack and move the cluster across — onto a destination that is empty, because `mv` onto a directory that already holds files puts the cluster one level too deep and the database will not find `PG_VERSION`: +> +> ```bash +> cd ~/nostream && sudo docker compose down +> sudo mkdir -p /opt/nostream/.nostr +> sudo ls -A /opt/nostream/.nostr/data 2>/dev/null # must print nothing; stop and inspect if it does +> sudo test -d /opt/nostream/.nostr/data && sudo rmdir /opt/nostream/.nostr/data +> sudo mv ~/nostream/.nostr/data /opt/nostream/.nostr/data +> sudo cat /opt/nostream/.nostr/data/PG_VERSION # the move is done only when this prints +> sudo cp -a ~/nostream/.nostr/settings.yaml /opt/nostream/.nostr/settings.yaml +> sudo chown 1000:1000 /opt/nostream/.nostr/settings.yaml +> sudo chmod 600 /opt/nostream/.nostr/settings.yaml +> ``` +> +> The copy has to succeed: `settings.yaml` is not optional, bootstrap leaves it out when it is absent, and the relay then runs on image defaults — a paid relay comes up with payments disabled. If `cp` reports an error, fix it before going on rather than continuing without the file. +> +> The relay's own environment carries over too. A fresh `/opt/nostream/.env` comes from `deploy/env.example`, whose values are placeholders, so copy every variable your 2.x `.env` actually set — `ZEBEDEE_API_KEY` and the other payment credentials among them — rather than trusting the defaults. The database is the one variable that works the other way round: the moved cluster keeps the password it was created with, and setting `POSTGRES_PASSWORD` does not change it, so `DB_PASSWORD` has to be that password (2.x used `nostr_ts_relay`), or set the stored one with `ALTER ROLE` before the migrations run. +> +> Then, once step 2 has refreshed `/opt/nostream`, bring up the database under the override **and the cache**, because `down` stopped both and step 4 recreates only the relay and the migrate job: +> +> ```bash +> cd /opt/nostream && sudo docker compose up -d nostream-db nostream-cache +> sudo docker compose exec nostream-db pg_isready +> ``` +> +> `recreate-relay.sh` passes `--no-deps` and a `restart` never applies a new image, so a service left stopped on the incompatible image stays stopped and the migrate job has nothing to connect to. + 1. Update your repository clone: ```bash cd ~/nostream