Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(selfhost): pass the generated Postgres password to Compose on first run (#10523)

MookeChee24d6777150b3
diff --git a/Makefile b/Makefile
index 72ed29e900..65047cce3c 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-.PHONY: help makehelp dev server daemon cli multica build test migrate-up migrate-down sqlc seed clean setup start stop check worktree-env setup-main start-main stop-main check-main setup-worktree start-worktree stop-worktree check-worktree remove-worktree db-up db-down db-drop db-reset selfhost selfhost-build selfhost-stop up down status list destroy gc env-exec api-dev web-dev desktop-dev
+.PHONY: help makehelp dev server daemon cli multica build test migrate-up migrate-down sqlc seed clean setup start stop check worktree-env setup-main start-main stop-main check-main setup-worktree start-worktree stop-worktree check-worktree remove-worktree db-up db-down db-drop db-reset selfhost selfhost-start selfhost-build selfhost-build-start selfhost-stop up down status list destroy gc env-exec api-dev web-dev desktop-dev
MAIN_ENV_FILE ?= .env
WORKTREE_ENV_FILE ?= .env.worktree
@@ -100,6 +100,12 @@ selfhost: ## Create .env if needed, then pull and start the official self-hosted
fi; \
echo "==> Generated random JWT_SECRET, POSTGRES_PASSWORD, and MULTICA_VCS_SECRET_KEY"; \
fi
+ @$(MAKE) --no-print-directory selfhost-start ENV_FILE=.env
+
+# Internal: runs in a second make so that a .env the recipe above just created
+# is included before Compose reads POSTGRES_PASSWORD from the environment.
+# ENV_FILE is pinned because Compose reads .env even when .env.worktree exists.
+selfhost-start:
@echo "==> Pulling official Multica images..."
@if ! $(COMPOSE) -f docker-compose.selfhost.yml pull; then \
echo ""; \
@@ -133,6 +139,10 @@ selfhost-build: ## Build backend/web from the current checkout and start the sel
fi; \
echo "==> Generated random JWT_SECRET, POSTGRES_PASSWORD, and MULTICA_VCS_SECRET_KEY"; \
fi
+ @$(MAKE) --no-print-directory selfhost-build-start ENV_FILE=.env
+
+# Internal: see selfhost-start.
+selfhost-build-start:
@echo "==> Building Multica from the current checkout..."
$(COMPOSE) -f docker-compose.selfhost.yml -f docker-compose.selfhost.build.yml up -d --build
@bash scripts/selfhost-wait.sh build
diff --git a/SELF_HOSTING.md b/SELF_HOSTING.md
index fd04b88c3d..72027ba595 100644
--- a/SELF_HOSTING.md
+++ b/SELF_HOSTING.md
@@ -482,6 +482,30 @@ If the selected GHCR tag has not been published yet, fall back to `make selfhost
> **Upgrading from `v0.3.4` to `v0.3.5+` fails with `refusing to drop legacy daily rollups: ...`?** That's migration `103`'s fail-closed guard: it requires `task_usage_hourly` to be seeded before the legacy daily rollups are dropped. As of MUL-2957 `migrate up` runs that backfill automatically right before applying `103`, so the upgrade completes in a single invocation. If you are still on a pre-MUL-2957 binary or the auto-hook fails, run `backfill_task_usage_hourly` manually first, then re-run the upgrade. Full instructions in [Advanced Configuration → Usage Dashboard Rollup](SELF_HOSTING_ADVANCED.md#usage-dashboard-rollup).
+> **Backend restarting with `password authentication failed for user "multica"` after an upgrade or a second `make selfhost`?** Installs first started by an older `make selfhost` or `make selfhost-build` initialized PostgreSQL with the default password instead of the generated `POSTGRES_PASSWORD` in `.env`, so the backend is rejected once the containers are recreated. Set the database password to the value in `.env`; this keeps your data:
+>
+> ```bash
+> docker compose -f docker-compose.selfhost.yml up -d --wait postgres
+> docker compose -f docker-compose.selfhost.yml exec -T postgres sh -c 'psql -v ON_ERROR_STOP=1 -v role="$POSTGRES_USER" -U "$POSTGRES_USER" -d postgres' <<'SQL'
+> \getenv pw POSTGRES_PASSWORD
+> ALTER USER :"role" WITH PASSWORD :'pw';
+> SQL
+> ```
+>
+> Then restart the stack using the Compose files for your installation. For official images (`make selfhost`):
+>
+> ```bash
+> docker compose -f docker-compose.selfhost.yml up -d
+> ```
+>
+> For locally built images (`make selfhost-build`), keep the build override so recovery uses your existing local images:
+>
+> ```bash
+> docker compose -f docker-compose.selfhost.yml -f docker-compose.selfhost.build.yml up -d
+> ```
+>
+> The command connects as `POSTGRES_USER` from `.env`. If you changed that value after the first start, put the original user name back first: PostgreSQL keeps the role it was initialized with. Do not run `docker compose down -v` to fix this: it deletes the database volume.
+
---
## Manual Docker Compose Setup
diff --git a/scripts/selfhost-config.test.sh b/scripts/selfhost-config.test.sh
index ca0161f271..24c709ae96 100755
--- a/scripts/selfhost-config.test.sh
+++ b/scripts/selfhost-config.test.sh
@@ -260,6 +260,7 @@ process.stdin.on("end", () => {
for (const service of ["backend", "frontend"]) {
console.log(service + "=" + config.services[service].ports[0].published);
}
+ console.log("postgres_password=" + config.services.postgres.environment.POSTGRES_PASSWORD);
});
' >>"$STUB_PUBLISHED_RECORD"
;;
@@ -558,4 +559,53 @@ for shadowed_alias in BACKEND_PORT API_PORT SERVER_PORT; do
require_consistent "env-file ${shadowed_alias} over the same shell variable" 9700
done
+# ---------------------------------------------------------------------------
+# First run without an env file (regression for #10522)
+#
+# The recipe writes .env, including a random POSTGRES_PASSWORD, and then starts
+# Compose. Compose must receive that generated password. A database initialised
+# with any other value stops authenticating the backend the next time the
+# containers are recreated from the env file. run_recipe always seeds .env, so
+# this path needs its own driver.
+# ---------------------------------------------------------------------------
+
+# Args: <target> [worktree]. With `worktree`, a .env.worktree is present, the
+# way a contributor checkout looks after `make worktree-env`. The recipe still
+# writes .env and Compose still reads .env, so its password must win there too.
+run_first_recipe() {
+ local target=$1 layout=${2:-}
+
+ rm -f "$recipe_dir/.env" "$recipe_dir/.env.worktree"
+ if [ "$layout" = "worktree" ]; then
+ printf 'POSTGRES_PASSWORD=worktree-password\n' >"$recipe_dir/.env.worktree"
+ fi
+ : >"$record"
+ : >"$curl_log"
+
+ (
+ cd "$recipe_dir" || exit 1
+ env PATH="$stub_dir:$PATH" \
+ REAL_DOCKER="$real_docker" \
+ STUB_PUBLISHED_RECORD="$record" \
+ STUB_CURL_LOG="$curl_log" \
+ make "$target"
+ )
+}
+
+for first_run_target in selfhost selfhost-build; do
+ for first_run_layout in "" worktree; do
+ run_first_recipe "$first_run_target" "$first_run_layout" >/dev/null
+ generated_password="$(sed -n 's/^POSTGRES_PASSWORD=//p' "$recipe_dir/.env")"
+ received_password="$(grep '^postgres_password=' "$record" | tail -n 1 | cut -d= -f2- || true)"
+
+ if [ -z "$generated_password" ] || [ "$received_password" != "$generated_password" ]; then
+ echo "[$first_run_target first run${first_run_layout:+ with .env.$first_run_layout}] Compose did not receive the POSTGRES_PASSWORD written to .env"
+ echo " written to .env: ${generated_password:-<none>}"
+ echo " received by Compose: ${received_password:-<none>}"
+ exit 1
+ fi
+ done
+done
+rm -f "$recipe_dir/.env.worktree"
+
echo "self-host env derivation ok"