# Upgrades

> Upgrade a Postern Hub built from source, what happens on the first start of a new version, how releases will work, version compatibility with the apps, and how to uninstall.

Source: https://docs.posternmail.app/admin/upgrades/

This page shows how to bring your Hub to a newer version, what the Hub does on its first start
after an upgrade, and how to go back or remove it.

> [!NOTE]
> Postern is in beta and **no releases are published yet**: no container images, no `.deb` or
> `.rpm` packages, no release archives and no signed desktop installers. Today you upgrade by
> pulling the source and building again. The [release-based steps](https://docs.posternmail.app/admin/upgrades/#once-releases-exist) below
> describe how it will work once the first release is out.

## Before you upgrade

1. Read the commit log or release notes for anything marked as a breaking change.
2. Make sure you have a recent backup and a copy of the key file somewhere other than the Hub's
   machine ([Backups and restore](https://docs.posternmail.app/admin/backups/)). The Hub copies its database before changing it,
   but not `blobs/` and not the key.
3. Note the version that runs now (**Settings → Admin → Status → Version**, or
   `postern-hub version`), in case you have to go back.

## Upgrade from source

**Docker Compose:**
The source checkout lives in `~/postern-src` and the Compose project in `~/postern`. Pull, build
the image under the same tag, and recreate the container:

```sh
cd ~/postern-src
git pull
docker build -f deploy/docker/Dockerfile -t ghcr.io/theoriuhd/postern-hub:local .
cd ~/postern
docker compose up -d
```

`docker compose up -d` sees the new image behind the `local` tag and replaces the container. The
volumes, and with them your data and key, stay.

Without a version build argument the admin panel shows `0.0.0-docker` as the version. To see which
commit runs, build with
`--build-arg VERSION=$(git describe --tags --always) --build-arg COMMIT=$(git rev-parse HEAD)`.

Old images pile up over time. `docker image prune` removes the untagged ones.
**Debian and Ubuntu, Fedora and RHEL and Raspberry Pi OS:**
The source checkout lives in `~/postern`. Pull, build, install the new binary and restart:

```sh
cd ~/postern
git pull
pnpm install --frozen-lockfile --filter "@postern/web..."
scripts/build-hub.sh
sudo install -m 0755 hub/bin/postern-hub /usr/local/bin/postern-hub
sudo systemctl restart postern-hub
```

`scripts/build-hub.sh` builds the web app, embeds it and writes one static binary to
`hub/bin/postern-hub`, with the version from `git describe`. If the build fails after a pull,
check whether the Go or Node version the project needs has changed (see
[Set up your Hub](https://docs.posternmail.app/setup/)).
**Desktop app (built-in Hub):**
The built-in Hub is part of the desktop app, so you upgrade it by building and installing a new
app. In your source checkout:

```sh
git pull
pnpm install --frozen-lockfile
pnpm --filter @postern/desktop bundle-hub -- --with-web
pnpm --filter @postern/desktop dist
```

Install the app from `apps/desktop/release/` over the old one. Your data folder is kept. Details:
[Development](https://docs.posternmail.app/development/).

**Check for Updates…** in the app only reports desktop releases on GitHub, which do not exist yet.

> **Check:** open **Settings → Admin**. **Version** shows the new version and **Running for** a
> few seconds or minutes. Then run `postern-hub doctor` ([Doctor](https://docs.posternmail.app/admin/doctor/)): under
> **Database** it shows `schema up to date`.

> **If it did not work:** look at the log ([Logs](https://docs.posternmail.app/admin/logs/)). A failed migration makes the Hub
> exit with `error: migration <name>: ...`. The `pre-migrate-*.db` copy from that start is in the
> backup directory: see [Going back](https://docs.posternmail.app/admin/upgrades/#going-back).

## What happens on the first start

On every start the Hub compares its built-in database migrations with the ones already applied.
If there are new ones:

1. It writes `pre-migrate-<time>.db` (a consistent copy of the database, without `blobs/`) into the
   backup directory. If that fails, the Hub exits with `error: pre-migration backup: ...` and
   changes nothing.
2. It applies each new migration in its own transaction, in order.
3. It starts as usual.

A fresh install skips the copy. Migrations usually take seconds; on a large database, give the
first start a minute. `postern-hub doctor` run with the new binary before the restart lists the
migrations that are about to run ("N schema migration(s) pending: ... (applied automatically on
next start, after an automatic backup)").

The `pre-migrate-*.db` copies are never deleted automatically. Delete them once the new version
works, especially if you have promised people deletion of their data (see
[Backups and restore](https://docs.posternmail.app/admin/backups/#deleted-accounts-and-the-gdpr)).

## Going back

**Downgrades are not supported.** An older Hub does not refuse a database that a newer one has
changed; it would start on a schema it does not know, with unpredictable results. To go back to
an older version, go back to the database from before the upgrade too:

1. Stop the Hub.
2. Install the older version (check out the old commit and build it).
3. Restore the `pre-migrate-*.db` written by the upgrade with `postern-hub restore --from
   <backup-dir>/pre-migrate-<time>.db` (see [Restore](https://docs.posternmail.app/admin/backups/#restore)). This replaces the
   database only and keeps `blobs/`.
4. Start the Hub.

Anything that happened between the upgrade and the restore is lost on the Hub.

## Versions of the Hub and the apps

The Hub serves the web app itself, so they always match. The desktop and mobile apps check the
Hub's API version (`apiVersion` in `/api/v1/info`, currently `1`) when they connect:

| Situation | What the desktop app says |
|---|---|
| Same API version | Connects. |
| Hub's API version is older | "Your Hub is too old for this app" with "Update your Hub, then connect again." |
| Hub's API version is newer | "Your Hub is newer than this app" with "Update Postern on this computer, then connect again." |

Within one API version the API is meant to change only by adding things, so an app usually works
with a somewhat older or newer Hub. New features of a newer app can need a newer Hub, so upgrade
the Hub first and the apps after it. The Hub also receives the security fixes, so keep it current.

## Knowing when there is a new version

The Hub asks Postern's download server (`https://updates.posternmail.app`) for the newest
version 20 seconds after it starts and then every 10 minutes. **Settings → Admin → Updates** shows
the running version, the newest one with a **What's new** link, and **Check now**. The request
carries nothing about your mail or accounts (see [Updates](https://docs.posternmail.app/updates/#what-a-check-sends)).
`POSTERN_UPDATE_FEED=off` turns the checks off.

**Channel**: **Stable** (the default) or **Beta**, which gets new versions first. Switching back
to Stable never installs an older version; the Hub waits until Stable catches up.

## Automatic updates

A Hub that can replace itself shows **Update automatically** (on by default) and **Update now**:

| Installed with | Updates itself? |
|---|---|
| The installer, with its self-update units (`deploy/systemd/README.md`) | Yes. The Hub downloads the new binary and checks its signature; a small root service swaps it in and restarts the Hub. |
| Docker, with the `updater` service of `compose.yaml` and the image tag `latest` | Yes. The updater pulls the new image and recreates the Hub. |
| Docker without the updater, or with `POSTERN_VERSION` pinned | No. The page shows `docker compose pull && docker compose up -d`. |
| The `.deb` or `.rpm` package | No. Install the new package with your package manager. |
| Built from source, or a Hub installed as `mailclient-hub` | No. The page shows the newest version; update as described above. |

An automatic update waits until your Hub is quiet: no message is being sent, no change is being
written to a mail provider and no backup is running. Open apps show that the Hub is updating
while it restarts, which takes a few seconds. If the new version doesn't start within two
minutes, the Hub goes back to the old one and says why on the Updates page. It tries that version
again after 5 minutes, then after 15, then once an hour, until a newer version comes out or you
press **Update now**.

Going back restores the program, not the database: see [Going back](https://docs.posternmail.app/admin/upgrades/#going-back) if a new
version changed it.

## Once releases exist

These steps use the planned release files. They do not work until the first release is
published.

**Docker Compose:**
Set `POSTERN_VERSION` in `~/postern/.env` to the release you want (a version such as `1.4`, or
`latest`) instead of `local`, then:

```sh
cd ~/postern
docker compose pull
docker compose up -d
```

The image is `ghcr.io/theoriuhd/postern-hub`. Pinning a major or minor version gives calmer
upgrades than `latest`.
**Debian and Ubuntu, Fedora and RHEL and Raspberry Pi OS:**
With the packages (installed to `/usr/bin/postern-hub`), install the new package over the old one.
It restarts a running Hub:

```sh
sudo apt install ./postern-hub_*.deb
```

On Fedora:

```sh
sudo dnf install ./postern-hub-*.rpm
```

With `deploy/install.sh`, run it again. It downloads the release archive
`postern-hub_<version>_linux_<arch>.tar.gz`, checks it against `checksums.txt`, replaces
`/usr/local/bin/postern-hub` and restarts the Hub if it was running. `--version 1.4.2` (or
`POSTERN_VERSION`) picks a version:

```sh
sudo bash install.sh --version 1.4.2
```
**Desktop app (built-in Hub):**
Signed desktop builds will update themselves: they check GitHub 15 seconds after launch and every
6 hours, download in the background and offer **Restart to Update**. The built-in Hub is updated
with the app.

## Uninstall

Uninstalling removes the program. **Your data and key are always kept** until you delete them
yourself, so you can reinstall and carry on.

**Docker Compose:**
```sh
cd ~/postern
docker compose down
docker image rm ghcr.io/theoriuhd/postern-hub:local
```

`docker compose down` removes the containers and keeps the volumes. To delete all data for good,
including the master key (make sure you no longer need any backup inside the volume):

```sh
docker volume rm postern_postern-data postern_postern-key
rm -rf ~/postern ~/postern-src
```
**Debian and Ubuntu, Fedora and RHEL and Raspberry Pi OS:**
For a Hub built from source, remove the service and the binary:

```sh
sudo systemctl disable --now postern-hub
sudo rm /etc/systemd/system/postern-hub.service /usr/local/bin/postern-hub
sudo systemctl daemon-reload
```

Kept until you remove them: `/var/lib/postern` (data and backups), `/var/lib/postern-key` (master
key), `/etc/postern` (configuration), the `postern` user and the source checkout `~/postern`. To
remove everything for good:

```sh
sudo rm -rf /var/lib/postern /var/lib/postern-key /etc/postern
sudo userdel postern
rm -rf ~/postern
```

Later, an install made with `deploy/install.sh` is removed with `sudo bash install.sh --uninstall`
(binary and unit only), and a package with `sudo apt remove postern-hub` or
`sudo dnf remove postern-hub`. Neither deletes the data, the key or the user, not even a purge.
**Desktop app (built-in Hub):**
Remove the app as you remove any other app. The built-in Hub's data stays in the app's data
folder (`hub-data/` and `hub-key/`). Delete that folder too if you want nothing left: on macOS
`~/Library/Application Support/Postern`, on Windows `%APPDATA%\Postern`, on Linux
`~/.config/Postern`. It also contains the desktop app's settings and its copy of your mail.
