Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(docker): ship the PostgreSQL client so in-container backups work

Yudhi Armyndharis993c3980586a
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 918b90203..dcd930d89 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
+- The image ships the PostgreSQL client, so `backup.sh` and `restore.sh` work in-container with `DATABASE_TYPE=postgres`. Neither `pg_dump` nor `psql` was present, so the backup exited 1 and the restore printed an import that could not be run.
- A session launch that fails on a locked database is retried. The classifier looked for `SQLITE_BUSY` in the error message while the driver carries it on `code`, so the session stayed down until a restart.
- Meta-hosted ids (`@hosted`, `@hosted.lid`) normalize to the dialect they name. They parsed as unknown before, so a chat surfaced with kind `unknown` and the same id was then refused with a `400` on any write.
diff --git a/Dockerfile b/Dockerfile
index 35507c5d7..e5e2be59f 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -78,7 +78,11 @@ ENV NODE_ENV=production
# with --no-install-recommends the package is dropped, and chromium launches fine under --no-sandbox.
ARG TARGETARCH
# sqlite3 ships the CLI so an in-container scripts/backup.sh run takes online-consistent SQLite
-# snapshots (.backup) instead of plain-copying a live database (which can archive a torn file).
+# snapshots (.backup) instead of plain-copying a live database (which can archive a torn file). The
+# DATABASE_TYPE=postgres half of the same script needs pg_dump, installed further down.
+#
+# ca-certificates is required to fetch the PGDG signing key over HTTPS below: the base image has no
+# CA bundle, and curl alone does not pull one in under --no-install-recommends (curl: (77)).
#
# ffmpeg backs the opt-in media-conversion endpoints, and also repairs an existing gap: whatsapp-web.js
# requires fluent-ffmpeg at module load and calls it for video-to-webp animated stickers, so
@@ -110,10 +114,30 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
patch \
curl \
procps \
+ ca-certificates \
sqlite3 \
ffmpeg \
&& rm -rf /var/lib/apt/lists/*
+# The PostgreSQL client for the DATABASE_TYPE=postgres half of backup.sh/restore.sh, which the
+# runbook drives in-container. Debian bookworm ships client 15 and pg_dump refuses a newer server
+# outright ("aborting because of server version mismatch", reproduced against the postgres:16 the
+# bundled compose file runs), so the distro package would install a pg_dump that cannot dump our own
+# stack, and psql would be missing for restore. PGDG is PostgreSQL's own apt repository and carries
+# current majors for both architectures we build; measured cost ~59 MB.
+#
+# Deliberately NEWER than the postgres:16 the compose file ships. The rule is one-directional (a
+# client may be newer than its server, never older), and DATABASE_HOST often points at a managed
+# Postgres the compose file does not control, where 17 is the current default. Pinning to 16 would
+# have left every such deployment unable to back up. Verified against live 16 and 17 servers.
+RUN install -d /usr/share/postgresql-common/pgdg \
+ && curl -fsSL -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc \
+ https://www.postgresql.org/media/keys/ACCC4CF8.asc \
+ && echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] \
+http://apt.postgresql.org/pub/repos/apt bookworm-pgdg main" > /etc/apt/sources.list.d/pgdg.list \
+ && apt-get update && apt-get install -y --no-install-recommends postgresql-client-17 \
+ && rm -rf /var/lib/apt/lists/*
+
# Set Puppeteer to skip automatic download during npm install (we download it explicitly below)
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true
diff --git a/src/modules/docker/compose-parity.spec.ts b/src/modules/docker/compose-parity.spec.ts
index 8ec4e1e45..9e52c78f9 100644
--- a/src/modules/docker/compose-parity.spec.ts
+++ b/src/modules/docker/compose-parity.spec.ts
@@ -420,3 +420,34 @@ describe('DockerService managed specs ↔ docker-compose.yml parity', () => {
});
});
});
+
+/**
+ * The image's pg_dump/psql must never be OLDER than the Postgres it talks to: pg_dump refuses a
+ * newer server outright ("aborting because of server version mismatch"), so an in-container
+ * backup of a DATABASE_TYPE=postgres deployment fails on version alone. The server major is
+ * declared once for the compose stack and again for the container the app orchestrates itself,
+ * while the client major lives in the Dockerfile, so bumping either server silently breaks backups
+ * until this fails.
+ */
+describe('PostgreSQL client is not older than the servers the stack ships', () => {
+ const root = join(__dirname, '../../..');
+
+ const major = (file: string, pattern: RegExp): number => {
+ const match = readFileSync(join(root, file), 'utf8').match(pattern);
+ if (!match) throw new Error(`could not read a Postgres major version from ${file} via ${String(pattern)}`);
+ return Number(match[1]);
+ };
+
+ it('the Dockerfile client covers both the compose server and the managed-container server', () => {
+ const client = major('Dockerfile', /postgresql-client-(\d+)/);
+ const composeServer = major('docker-compose.yml', /image:\s*postgres:(\d+)/);
+ const managedServer = major('src/modules/docker/docker.service.ts', /image: 'postgres:(\d+)/);
+
+ // Reported together so a failure names every version at once rather than the first mismatch.
+ expect({
+ client,
+ composeServerCovered: composeServer <= client,
+ managedServerCovered: managedServer <= client,
+ }).toEqual({ client, composeServerCovered: true, managedServerCovered: true });
+ });
+});