ArunKr commited on
Commit
869d71f
·
verified ·
1 Parent(s): bd66f00

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +39 -0
  2. README.md +34 -5
  3. start.sh +43 -0
Dockerfile ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM postgres:15
2
+
3
+ # Install utilities
4
+ RUN apt-get update && apt-get install -y \
5
+ wget \
6
+ unzip \
7
+ && rm -rf /var/lib/apt/lists/*
8
+
9
+ # Install pgweb
10
+ RUN wget https://github.com/sosedoff/pgweb/releases/download/v0.14.1/pgweb_linux_amd64.zip \
11
+ && unzip pgweb_linux_amd64.zip \
12
+ && mv pgweb_linux_amd64 /usr/local/bin/pgweb \
13
+ && rm pgweb_linux_amd64.zip
14
+
15
+ # Create a directory for the custom start script
16
+ WORKDIR /app
17
+
18
+ # Copy the start script
19
+ COPY start.sh /app/start.sh
20
+ RUN chmod +x /app/start.sh
21
+
22
+ # Set the user to postgres (UID 999) which is the default for this image
23
+ # However, HF Spaces runs as UID 1000.
24
+ # We need to ensure permissions are correct for /var/lib/postgresql/data
25
+ # The official image creates a volume at /var/lib/postgresql/data
26
+ # We will let the start script handle the user switching or just run as the default user if possible.
27
+ # HF Spaces overrides the user to 1000.
28
+ # We need to make sure the data directory is writable by UID 1000.
29
+
30
+ # Create the data directory and give permissions to UID 1000
31
+ RUN mkdir -p /var/lib/postgresql/data && \
32
+ chown -R 1000:1000 /var/lib/postgresql/data && \
33
+ chmod 700 /var/lib/postgresql/data
34
+
35
+ # Expose the port for pgweb (HF Spaces expects 7860)
36
+ EXPOSE 7860
37
+
38
+ # Set the entrypoint to our custom script
39
+ CMD ["/app/start.sh"]
README.md CHANGED
@@ -1,10 +1,39 @@
1
  ---
2
- title: Postgres Db
3
- emoji: 📚
4
- colorFrom: gray
5
- colorTo: yellow
6
  sdk: docker
 
7
  pinned: false
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Postgres Database
3
+ emoji: 🐘
4
+ colorFrom: blue
5
+ colorTo: indigo
6
  sdk: docker
7
+ app_port: 7860
8
  pinned: false
9
  ---
10
 
11
+ # PostgreSQL on Hugging Face Spaces
12
+
13
+ This Space runs a PostgreSQL database instance along with [pgweb](https://github.com/sosedoff/pgweb), a lightweight web-based database browser.
14
+
15
+ ## ⚠️ Important: Persistence
16
+
17
+ To ensure your data is saved between restarts, you **MUST** enable Persistent Storage in the Space Settings.
18
+
19
+ 1. Go to **Settings**.
20
+ 2. Scroll to **Persistent Storage**.
21
+ 3. Select a tier (e.g., Small).
22
+ 4. The storage will be mounted at `/data` (which is mapped to `/var/lib/postgresql/data` in the Dockerfile).
23
+
24
+ ## Accessing the Database
25
+
26
+ The database is accessible via the web interface provided by `pgweb` at the main URL of this Space.
27
+
28
+ **Default Credentials:**
29
+ - **Host**: `127.0.0.1`
30
+ - **Port**: `5432`
31
+ - **User**: `app_user`
32
+ - **Password**: `app_password`
33
+ - **Database**: `app_db`
34
+
35
+ ## Connecting from other Spaces
36
+
37
+ You cannot connect to this database from the outside world (e.g., your laptop), but you *might* be able to connect from other Spaces if they are in the same organization/user scope, though typically Spaces are isolated.
38
+
39
+ For a production setup, consider using an external database provider (Supabase, Neon, AWS RDS, etc.). This Space is intended for testing, prototyping, or self-contained applications.
start.sh ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ # Define data directory
4
+ PGDATA="/var/lib/postgresql/data/pgdata"
5
+ export PGDATA
6
+
7
+ # Create the data directory if it doesn't exist
8
+ if [ ! -d "$PGDATA" ]; then
9
+ echo "Initializing database..."
10
+ mkdir -p "$PGDATA"
11
+ initdb -D "$PGDATA"
12
+
13
+ # Configure Postgres to listen on localhost only (since we only access via pgweb)
14
+ # or listen on all interfaces but block external access via firewall/network (which HF does automatically)
15
+ echo "listen_addresses = '127.0.0.1'" >> "$PGDATA/postgresql.conf"
16
+
17
+ # Create a default user and database
18
+ # We start postgres in the background to run setup commands
19
+ pg_ctl -D "$PGDATA" -w start
20
+
21
+ echo "Creating default user and database..."
22
+ psql -d postgres -c "CREATE USER app_user WITH PASSWORD 'app_password';"
23
+ psql -d postgres -c "CREATE DATABASE app_db OWNER app_user;"
24
+ psql -d postgres -c "GRANT ALL PRIVILEGES ON DATABASE app_db TO app_user;"
25
+
26
+ pg_ctl -D "$PGDATA" stop
27
+ fi
28
+
29
+ # Start Postgres in the background
30
+ echo "Starting PostgreSQL..."
31
+ pg_ctl -D "$PGDATA" -w start &
32
+
33
+ # Wait for Postgres to be ready
34
+ until pg_isready; do
35
+ echo "Waiting for PostgreSQL to be ready..."
36
+ sleep 2
37
+ done
38
+
39
+ # Start pgweb
40
+ echo "Starting pgweb on port 7860..."
41
+ # We connect to the local postgres instance
42
+ # --bind=0.0.0.0 is required for HF Spaces to expose the port
43
+ pgweb --bind=0.0.0.0 --listen=7860 --url="postgres://app_user:app_password@127.0.0.1:5432/app_db?sslmode=disable"