arcsu1 commited on
Commit
3d2b43e
·
1 Parent(s): 722855a

Add initial Docker setup with PostgREST and PostgreSQL configuration

Browse files
Files changed (4) hide show
  1. .env.example +48 -0
  2. Dockerfile +58 -0
  3. init.sql +46 -0
  4. start.sh +39 -0
.env.example ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ============================================================
2
+ # PostgREST + PostgreSQL (Hugging Face Space) — env vars
3
+ # Copy to `.env` and adjust. HF Spaces only allows PORT to be
4
+ # set via the Space settings; everything else can live here.
5
+ # ============================================================
6
+
7
+ # --- Server ---
8
+ # Port PostgREST listens on. HF Spaces requires 7860.
9
+ PORT=7860
10
+ # Bind address (keep 0.0.0.0 for HF Spaces)
11
+ PGRST_SERVER_HOST=0.0.0.0
12
+
13
+ # --- Database connection ---
14
+ # Full connection URI. Inside this container Postgres runs on a
15
+ # local unix socket, so the default below is usually fine.
16
+ # For an external DB, use e.g.:
17
+ # postgresql://authenticator:secret@db.example.com:5432/prod
18
+ PGRST_DB_URI=postgresql://?host=/var/run/postgresql&port=5432&dbname=postgres
19
+
20
+ # Comma-separated schemas exposed by the API
21
+ PGRST_DB_SCHEMA=api
22
+
23
+ # Role used for unauthenticated (anonymous) requests
24
+ PGRST_DB_ANON_ROLE=web_anon
25
+
26
+ # Role used to apply schema changes / own objects
27
+ AUTHENTICATOR_PASSWORD=secret
28
+
29
+ # --- Auth (optional) ---
30
+ # Secret used to sign/verify JWTs. Generate one with:
31
+ # echo "secret" | openssl dgst -sha256 -hmac "your-key" -binary | base64
32
+ # Leave empty to disable JWT auth (public API).
33
+ PGRST_JWT_SECRET=
34
+ # Claim that carries the DB role in the JWT (default: "role")
35
+ PGRST_JWT_ROLE_CLAIM_KEY=role
36
+ # Audience claim to require, if any
37
+ PGRST_JWT_AUDIENCE=
38
+
39
+ # --- API behavior (optional) ---
40
+ # Max rows per request (Content-Range limiting)
41
+ PGRST_MAX_ROWS=
42
+ # Allow CORS from any origin (useful for browser clients)
43
+ PGRST_CORS_ALLOWED_ORIGINS=*
44
+ # Path prefix if served behind a proxy (usually empty on HF)
45
+ PGRST_SERVER_PATH_PREFIX=
46
+
47
+ # --- PostgREST version pinned in the Dockerfile ---
48
+ PGRST_VERSION=16.2
Dockerfile ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # PostgREST + PostgreSQL in a single container (Hugging Face Space)
2
+ # Serves a fully RESTful API from a Postgres database on port 7860.
3
+
4
+ FROM debian:bookworm-slim
5
+
6
+ ENV DEBIAN_FRONTEND=noninteractive \
7
+ PGRST_VERSION=16.2 \
8
+ PORT=7860 \
9
+ PGRST_DB_SCHEMAS=api \
10
+ PGRST_DB_ANON_ROLE=web_anon \
11
+ PGRST_SERVER_HOST=0.0.0.0
12
+
13
+ # Debian's postgresql package installs binaries outside the default PATH
14
+ ENV PATH="/usr/lib/postgresql/15/bin:${PATH}"
15
+
16
+ # --- System dependencies: PostgreSQL server + tools ---
17
+ RUN apt-get update && apt-get install -y --no-install-recommends \
18
+ ca-certificates \
19
+ curl \
20
+ xz-utils \
21
+ libpq5 \
22
+ postgresql \
23
+ postgresql-contrib \
24
+ postgresql-client \
25
+ sudo \
26
+ && rm -rf /var/lib/apt/lists/*
27
+
28
+ # --- PostgREST binary (static build from GitHub releases) ---
29
+ RUN ARCH="$(dpkg --print-architecture)" \
30
+ && case "$ARCH" in \
31
+ amd64) PGRST_ARCH=x86-64 ;; \
32
+ arm64) PGRST_ARCH=aarch64 ;; \
33
+ *) echo "unsupported arch: $ARCH" && exit 1 ;; \
34
+ esac \
35
+ && curl -fsSL \
36
+ "https://github.com/PostgREST/postgrest/releases/download/v${PGRST_VERSION}/postgrest-v${PGRST_VERSION}-linux-static-${PGRST_ARCH}.tar.xz" \
37
+ -o /tmp/postgrest.tar.xz \
38
+ && tar -xJf /tmp/postgrest.tar.xz -C /usr/local/bin \
39
+ && chmod +x /usr/local/bin/postgrest \
40
+ && rm /tmp/postgrest.tar.xz \
41
+ && postgrest --version
42
+
43
+ # --- Non-root user (HF Spaces runs containers as a random UID) ---
44
+ RUN useradd -m -u 10001 appuser \
45
+ && mkdir -p /var/lib/postgresql/data /var/run/postgresql \
46
+ && chown -R appuser:appuser /var/lib/postgresql /var/run/postgresql /usr/local/bin/postgrest
47
+
48
+ USER appuser
49
+ WORKDIR /home/appuser
50
+
51
+ # --- Sample schema: a tiny "todos" API to demo PostgREST ---
52
+ COPY --chown=appuser:appuser init.sql /docker-entrypoint-init.d/init.sql
53
+ COPY --chown=appuser:appuser start.sh /usr/local/bin/start.sh
54
+ RUN chmod +x /usr/local/bin/start.sh
55
+
56
+ EXPOSE 7860
57
+
58
+ CMD ["start.sh"]
init.sql ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ -- Schema + roles for PostgREST, executed once at container startup.
2
+
3
+ -- Role PostgREST connects as (anonymous / public access)
4
+ CREATE ROLE web_anon LOGIN NOINHERIT;
5
+
6
+ -- Role used to apply schema changes
7
+ CREATE ROLE authenticator NOINHERIT LOGIN PASSWORD 'secret';
8
+ GRANT web_anon TO authenticator;
9
+
10
+ CREATE SCHEMA api;
11
+ GRANT USAGE ON SCHEMA api TO web_anon;
12
+
13
+ CREATE TABLE api.todos (
14
+ id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
15
+ done boolean NOT NULL DEFAULT false,
16
+ task text NOT NULL,
17
+ due timestamptz,
18
+ created_at timestamptz NOT NULL DEFAULT now()
19
+ );
20
+
21
+ -- Change ownership BEFORE granting: ALTER TABLE ... OWNER TO drops
22
+ -- privileges granted by the previous owner.
23
+ ALTER TABLE api.todos OWNER TO authenticator;
24
+ GRANT SELECT, INSERT, UPDATE, DELETE ON api.todos TO web_anon;
25
+ GRANT USAGE, SELECT ON SEQUENCE api.todos_id_seq TO web_anon;
26
+
27
+ INSERT INTO api.todos (task, due) VALUES
28
+ ('Try GET /todos', now() + interval '1 day'),
29
+ ('Try POST /todos', now() + interval '2 days'),
30
+ ('Try PATCH /todos?id=eq.1', now() + interval '3 days');
31
+
32
+ -- Expose row counts in Content-Range headers
33
+ CREATE OR REPLACE FUNCTION api.todos_count()
34
+ RETURNS bigint
35
+ LANGUAGE sql
36
+ AS $$
37
+ SELECT count(*) FROM api.todos;
38
+ $$;
39
+
40
+ -- OpenAPI description endpoint
41
+ CREATE OR REPLACE FUNCTION api.openapi_description()
42
+ RETURNS text
43
+ LANGUAGE sql
44
+ AS $$
45
+ SELECT 'A demo PostgREST API running on Hugging Face Spaces';
46
+ $$;
start.sh ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ # Start PostgreSQL, apply the schema, then launch PostgREST on $PORT.
3
+ set -euo pipefail
4
+
5
+ # Load .env if present (local runs; HF Spaces injects vars itself)
6
+ if [ -f .env ]; then
7
+ set -a
8
+ . ./.env
9
+ set +a
10
+ fi
11
+
12
+ PGDATA=/var/lib/postgresql/data
13
+ SOCKET_DIR=/var/run/postgresql
14
+ INIT_SQL=/docker-entrypoint-init.d/init.sql
15
+
16
+ # --- Initialize and start PostgreSQL ---
17
+ if [ ! -s "$PGDATA/PG_VERSION" ]; then
18
+ initdb --username=appuser --pgdata="$PGDATA" --auth-local=trust --auth-host=trust
19
+ fi
20
+
21
+ pg_ctl -D "$PGDATA" -o "-c listen_addresses=127.0.0.1 -c unix_socket_directories=$SOCKET_DIR -c port=5432" -w start
22
+
23
+ # Wait until the server accepts connections
24
+ until pg_isready -h "$SOCKET_DIR" -p 5432 -q; do
25
+ sleep 0.2
26
+ done
27
+
28
+ # --- Apply schema once ---
29
+ if ! psql -h "$SOCKET_DIR" -p 5432 -d postgres -Atqc "SELECT 1 FROM pg_roles WHERE rolname='web_anon'" | grep -q 1; then
30
+ psql -v ON_ERROR_STOP=1 -h "$SOCKET_DIR" -p 5432 -d postgres -f "$INIT_SQL"
31
+ fi
32
+
33
+ # --- Launch PostgREST ---
34
+ export PGRST_DB_URI="postgresql://?host=$SOCKET_DIR&port=5432&dbname=postgres"
35
+ export PGRST_DB_SCHEMA="${PGRST_DB_SCHEMA:-api}"
36
+ export PGRST_DB_ANON_ROLE="${PGRST_DB_ANON_ROLE:-web_anon}"
37
+ export PGRST_SERVER_PORT="${PORT:-7860}"
38
+
39
+ exec postgrest