# Supabase Stack Files Generator — v3

Generated by https://www.joemore.dev/supabase-add-backend-stack

For Linux and Docker Compose v2 (`docker compose`). Each website needs its own
stack name, credentials, host ports, and data directory. These templates retain
the existing Kong 2.8 / Postgres 15 stack; they do not migrate it to Envoy.

## ZIP contents

- `.env` with generated credentials and your form values
- `docker-compose.yml`
- `setup-supabase-volumes.sh`
- `remove-supabase-stack.sh` (preview first; permanent removal requires confirmation)
- `update-email-templates.sh`
- `test-supabase-endpoints.sh`
- `auth-email-templates/` with the five auth emails
- `volumes/` with bundled Kong, Vector, SQL, and pooler configuration
- This README

Extract the entire ZIP, including `volumes/`. `.env` is hidden by default
(Cmd + Shift + . on macOS, usually Ctrl + H on Linux). Keep this bundle private:
it contains database passwords, API keys, and your SMTP credential.

## Deploy a new stack

1. Extract into a new directory and review `.env`. Choose a unique
   `DOCKER_STACK_NAME` (1–56 lowercase letters, digits or hyphens, starting with
   a letter or digit). Never reuse another stack's name or credentials.
2. Assign free host ports for **all five** published ports: `KONG_HTTP_PORT`,
   `KONG_HTTPS_PORT`, `ANALYTICS_PORT`, `POSTGRES_PORT`, and
   `POOLER_PROXY_PORT_TRANSACTION`. The form cannot detect ports on your server.
3. Ensure your reverse proxy and the stack share the external `edge` network:

   ```bash
   docker network inspect edge >/dev/null 2>&1 || docker network create edge
   ```

4. From the extracted directory, install the bundled volume files:

   ```bash
   chmod +x setup-supabase-volumes.sh update-email-templates.sh test-supabase-endpoints.sh
   ./setup-supabase-volumes.sh
   docker compose config --quiet
   docker compose up -d --wait
   ```

   Setup asks for confirmation and may use sudo. It copies files to
   `${SUPABASE_DATA_DIR}/${DOCKER_STACK_NAME}`. It does not download configuration
   from GitHub, and ignores any old `SUPABASE_REF` setting. Docker still needs to
   pull the container images. Use Compose's default container naming; Vector log
   filters expect `<stack>-<service>-1` names.

5. Configure Nginx Proxy Manager on `edge`:

   | Public hostname | Scheme | Forward hostname | Container port |
   | --- | --- | --- | --- |
   | Your API hostname | `http` | `<DOCKER_STACK_NAME>-kong` | `8000` |
   | Your Studio hostname | `http` | `<DOCKER_STACK_NAME>-studio` | `3000` |

   Use the unique aliases, for example `supabase-example-com-kong`. Use container
   port `8000` even when `KONG_HTTP_PORT=8004`. Protect the Studio proxy host with
   authentication / Cloudflare Access before exposing it: Studio has no built-in
   login here. The API's dashboard catch-all returns `403`, so it cannot bypass
   the protection on the Studio hostname. MCP remains blocked.

   If using Cloudflare Tunnel, send both public hostnames to your Nginx Proxy
   Manager HTTP listener (for example `http://nginx-proxy-manager:80`); Nginx
   selects the proxy host using the preserved public Host header.

6. Verify public routing:

   ```bash
   ./test-supabase-endpoints.sh
   ```

   Expected statuses: Auth `200`, a nonexistent REST table `404`, Storage `200`,
   Realtime tenant administration `403`, API dashboard catch-all `403`, MCP `403`.
   The Realtime check verifies that administration is blocked; it does not test
   a WebSocket subscription.

## If the API returns 502

Check Kong before changing the proxy or tunnel:

```bash
docker compose ps kong
docker compose logs --tail=100 kong
# Replace 8004 with this stack's KONG_HTTP_PORT:
BASE_URL=http://127.0.0.1:8004 ./test-supabase-endpoints.sh
```

If the direct checks pass and public checks fail, inspect the proxy hostname,
container port, and shared `edge` network. Studio and Kong use unique aliases
for communication across `edge`; services connected only to the stack's private
network can safely use short names such as `kong`.

## Existing stacks

These files are intended for new stacks. Do not replace an existing stack's
`.env` with newly generated credentials. To apply the Kong fix to an existing
stack, back up its configuration, retain its `.env` and stack name, copy in the
v3 bundle files, and install only `volumes/api/kong.yml` and
`volumes/api/kong-entrypoint.sh` into its data directory's `kong/` folder.
In the installed `kong.yml`, replace every `http://studio:3000` with
`http://<DOCKER_STACK_NAME>-studio:3000`. Use the v3 Compose file, validate with
`docker compose config --quiet`, then recreate `studio` and `kong`.
Review the bundled Vector changes separately. The setup script replaces SQL
initialization files and permissions and is intended for preparing new stacks.

## Updating auth email templates

Edit `auth-email-templates/`, then run `./update-email-templates.sh` from the
extracted directory.

## Stack Remove — permanent deletion

The generator page has a red **Stack Remove** button, collapsed by default. It
includes a standalone script download for existing stacks; generating new
credentials is unnecessary. The script requires Linux, Python 3 and Docker
Compose v2, and reads the selected stack's existing `.env` and Compose file.

1. Record the actual project name, API hostname and Studio hostname. Stop
   deployment automation. If Portainer manages the stack, remove it there with
   volume deletion disabled, then use this script to finish cleanup. Have a
   verified database/Storage backup and encryption-key backup outside the data
   directory, or explicitly choose to discard the data.
2. Save the removal script beside the existing stack files. From that directory:

   ```bash
   # Preview only. Check the exact project, containers, volumes and data path.
   sudo bash ./remove-supabase-stack.sh --stack-dir "$PWD"

   # Permanent removal: requires two typed confirmations.
   sudo bash ./remove-supabase-stack.sh --stack-dir "$PWD" --remove
   ```

   Type `BACKED UP` or `DISCARD DATA`, then `DELETE <exact-project-name>` at the
   prompts. This removes project containers, its private network, named volumes
   (including `db-config` encryption keys), anonymous volumes, and its dedicated
   data directory. Database/users, uploads, functions, logs and copied templates
   are deleted. The script checks ownership, paths and other containers' mounts,
   and verifies Docker removal before deleting bind-mounted data. Unexpected
   layouts, shared mounts and remote Docker contexts are refused for manual
   review. Run both commands with sudo on the Linux Docker host to allow removal
   of Postgres-owned files. The external `edge` network and images are retained.
3. In **Nginx Proxy Manager → Hosts → Proxy Hosts**, record settings for reuse,
   then delete only this backend's API and Studio hosts. Verify their forward
   aliases/ports against the existing stack (older entries may use published
   host ports). If a host entry includes other domains, edit out only the backend
   domains. Retain shared SSL certificates, access lists, the Nginx container and
   its data. Confirm the other hosts remain intact.
4. In **Cloudflare → Networking → Tunnels**, select the home-server tunnel and
   open **Routes → Published application**. Remove only these API and Studio
   hostname routes. Older dashboards use **Zero Trust → Networks → Connectors →
   Cloudflare Tunnels → Edit → Public Hostnames**.
   Save and verify. For a locally managed tunnel, edit only its matching ingress
   rules and reload the connector instead.
5. In the domain's **Cloudflare DNS → Records**, check for and delete only the
   exact backend CNAME records pointing to `<tunnel-id>.cfargotunnel.com` if they
   remain. Tunnel routes and DNS are separate configuration. Retain the shared
   tunnel/cloudflared service, apex and `www` records, email records, and other
   applications' routes. Keep Studio Access protection if reusing its hostname.
6. Verify removal in Docker, Nginx, the tunnel and DNS, and check another stack
   and the main website still work. A `502` response alone does not verify full
   cleanup; wildcard DNS/routes can still match the old names. The old stack
   folder and `.env` are retained for reference; keep them secure until cleanup
   is complete. Use a fresh deployment folder when preparing a replacement.

References: [Docker Compose down](https://docs.docker.com/reference/cli/docker/compose/down/),
[Nginx Proxy Manager](https://nginxproxymanager.com/guide/),
[Cloudflare Tunnel DNS](https://developers.cloudflare.com/cloudflare-one/networks/connectors/cloudflare-tunnel/routing-to-tunnel/dns/).
