--- title: Remote Postgres emoji: 🐘 colorFrom: indigo colorTo: blue sdk: docker app_port: 7860 pinned: false --- # Remote Postgres on Hugging Face Spaces A single Docker container that runs **PostgreSQL** + a small **web UI** that hands you a **public connection URL** to drop into any demo project that needs a Postgres database. Because Hugging Face Spaces only exposes one HTTP port (no raw TCP), the container opens an outbound **TCP tunnel** so Postgres is reachable from anywhere with a normal `postgresql://…` URL and any client/ORM. The default tunnel is **[bore](https://github.com/ekzhang/bore)** — open-source, **no signup, no token**. Set `NGROK_AUTHTOKEN` to use ngrok instead. > ⚠️ **Throwaway by default.** The public URL changes on every restart. Data is **backed up** > (see Persistence) but treat the cluster itself as disposable. --- ## Deploy (5 minutes) 1. **Create a Space** → New Space → **SDK: Docker** → **Visibility: Public** (so keep-alive can reach it; the UI is still password-locked). 2. **Push these files** to the Space repo (or upload them in the web UI): `Dockerfile`, `start.sh`, `README.md`, and the `app/` folder. 3. **Add Space secrets** (Settings → *Variables and secrets*): | Secret | Required | Purpose | |---|---|---| | `APP_PASSWORD` | recommended | Locks the web UI (Basic auth, user `admin`) so only you see the URL | | `HF_TOKEN` + `HF_BACKUP_REPO` | for backups | Off-Space dumps to a private HF Dataset (see Persistence) | | `POSTGRES_PASSWORD` | recommended | Fixed DB password (otherwise one is generated per boot) | | `NGROK_AUTHTOKEN` | optional | Use ngrok instead of bore for the tunnel | | `POSTGRES_USER` / `POSTGRES_DB` | optional | Defaults: `demo` / `demo` | 4. The Space builds and starts. Open it → the UI shows your **connection URL** + a Copy button. ## Use it Open the Space, copy the `postgresql://…` URL, and use it anywhere: ```bash psql "postgresql://demo:PASSWORD@bore.pub:26134/demo" ``` ```python import psycopg conn = psycopg.connect("postgresql://demo:PASSWORD@bore.pub:26134/demo") ``` Works the same with SQLAlchemy, Prisma, Drizzle, node-postgres, etc. ## Endpoints - `/` — **Gradio uptime-watcher dashboard**: live Postgres/tunnel status, container uptime, availability %, recent-checks table, keep-alive counter, and the copyable connection URL. Login-protected (user `admin`) when `APP_PASSWORD` is set. - `/api/connection` — JSON with the URL + fields (also protected). - `/keepalive?src=