ArunKr commited on
Commit
d3d1d61
·
verified ·
1 Parent(s): 6298fab

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. README.md +8 -9
  2. start.sh +12 -21
README.md CHANGED
@@ -10,7 +10,7 @@ pinned: false
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
  ## ⚠️ Note: Ephemeral Storage
16
 
@@ -22,19 +22,18 @@ You have chosen to use the container's built-in storage.
22
 
23
  The database files are stored at `/var/lib/postgresql/data/pgdata`.
24
 
25
- ## Accessing the Database
26
 
27
- The database is accessible via the web interface provided by `pgweb` at the main URL of this Space.
28
 
29
- **Default Credentials:**
30
- - **Host**: `127.0.0.1`
31
- - **Port**: `5432`
32
  - **User**: `app_user`
33
  - **Password**: `app_password`
34
  - **Database**: `app_db`
35
 
36
- ## Connecting from other Spaces
37
 
38
- 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.
 
 
39
 
40
- 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.
 
10
 
11
  # PostgreSQL on Hugging Face Spaces
12
 
13
+ This Space runs a PostgreSQL database instance listening directly on port **7860**.
14
 
15
  ## ⚠️ Note: Ephemeral Storage
16
 
 
22
 
23
  The database files are stored at `/var/lib/postgresql/data/pgdata`.
24
 
25
+ ## Connection Details
26
 
27
+ The database is listening on port `7860`.
28
 
29
+ **Credentials:**
 
 
30
  - **User**: `app_user`
31
  - **Password**: `app_password`
32
  - **Database**: `app_db`
33
 
34
+ ## ⚠️ Important: Connectivity
35
 
36
+ Hugging Face Spaces expose port 7860 over **HTTP/HTTPS**.
37
+ - You **CANNOT** connect to this database using a standard PostgreSQL client (like `psql` or DBeaver) via the Space's public URL (`https://...`). The load balancer expects HTTP traffic.
38
+ - This setup is primarily useful if you are connecting from **within** the Space (e.g., if you add your app code to this container) or potentially from other Spaces in the same private cluster (if applicable).
39
 
 
start.sh CHANGED
@@ -10,34 +10,25 @@ if [ ! -d "$PGDATA" ]; then
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"
 
10
  mkdir -p "$PGDATA"
11
  initdb -D "$PGDATA"
12
 
13
+ # Configure Postgres to listen on all interfaces on port 7860
14
+ echo "listen_addresses = '*'" >> "$PGDATA/postgresql.conf"
15
+ echo "port = 7860" >> "$PGDATA/postgresql.conf"
16
+
17
+ # Allow all connections (be careful with this in production!)
18
+ echo "host all all 0.0.0.0/0 trust" >> "$PGDATA/pg_hba.conf"
19
 
20
  # Create a default user and database
21
  # We start postgres in the background to run setup commands
22
  pg_ctl -D "$PGDATA" -w start
23
 
24
  echo "Creating default user and database..."
25
+ psql -d postgres -h 127.0.0.1 -p 7860 -c "CREATE USER app_user WITH PASSWORD 'app_password';"
26
+ psql -d postgres -h 127.0.0.1 -p 7860 -c "CREATE DATABASE app_db OWNER app_user;"
27
+ psql -d postgres -h 127.0.0.1 -p 7860 -c "GRANT ALL PRIVILEGES ON DATABASE app_db TO app_user;"
28
 
29
  pg_ctl -D "$PGDATA" stop
30
  fi
31
 
32
+ # Start Postgres in the foreground
33
+ echo "Starting PostgreSQL on port 7860..."
34
+ postgres -D "$PGDATA"