ArunKr commited on
Commit
dd55fa1
·
verified ·
1 Parent(s): 05b84e9

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. Dockerfile +10 -1
  2. README.md +18 -16
  3. start.sh +29 -14
  4. workflow.json +48 -0
Dockerfile CHANGED
@@ -27,6 +27,15 @@ RUN chmod +x /app/start.sh
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
  # We create a user with UID 1000 to match HF Spaces default and avoid root errors
32
  RUN useradd -m -u 1000 hf_user
@@ -38,7 +47,7 @@ RUN mkdir -p /var/lib/postgresql/data && \
38
  # Switch to the non-root user
39
  USER hf_user
40
 
41
- # Expose the port for Postgres
42
  EXPOSE 7860
43
 
44
  # Set the entrypoint to our custom script
 
27
  # HF Spaces overrides the user to 1000.
28
  # We need to make sure the data directory is writable by UID 1000.
29
 
30
+ # Install Node.js and n8n
31
+ RUN apt-get update && apt-get install -y \
32
+ curl \
33
+ gnupg \
34
+ && curl -fsSL https://deb.nodesource.com/setup_18.x | bash - \
35
+ && apt-get install -y nodejs \
36
+ && npm install -g n8n \
37
+ && rm -rf /var/lib/apt/lists/*
38
+
39
  # Create the data directory and give permissions to UID 1000
40
  # We create a user with UID 1000 to match HF Spaces default and avoid root errors
41
  RUN useradd -m -u 1000 hf_user
 
47
  # Switch to the non-root user
48
  USER hf_user
49
 
50
+ # Expose the port for n8n (HF Spaces expects 7860)
51
  EXPOSE 7860
52
 
53
  # Set the entrypoint to our custom script
README.md CHANGED
@@ -8,32 +8,34 @@ app_port: 7860
8
  pinned: false
9
  ---
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
 
17
  You have chosen to use the container's built-in storage.
18
  **Warning**: This storage is **ephemeral**.
19
- - If the Space restarts (which happens after 48 hours of inactivity or during updates), **ALL DATA WILL BE LOST**.
20
  - This setup is suitable for testing, demos, or temporary data.
21
- - If you need to save data permanently, you must enable Persistent Storage in Settings later.
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
 
 
8
  pinned: false
9
  ---
10
 
11
+ # n8n + PostgreSQL on Hugging Face Spaces
12
 
13
+ This Space runs **n8n** (Workflow Automation) and **PostgreSQL** together in a single container.
14
+
15
+ - **n8n**: Accessible via the Space URL (Port 7860).
16
+ - **PostgreSQL**: Running internally on Port 5432.
17
 
18
  ## ⚠️ Note: Ephemeral Storage
19
 
20
  You have chosen to use the container's built-in storage.
21
  **Warning**: This storage is **ephemeral**.
22
+ - If the Space restarts (which happens after 48 hours of inactivity or during updates), **ALL DATA (Workflows + DB)** will be lost.
23
  - This setup is suitable for testing, demos, or temporary data.
 
 
 
24
 
25
+ ## Getting Started
26
 
27
+ 1. Wait for the Space to build.
28
+ 2. Open the Space URL.
29
+ 3. Set up your n8n owner account.
30
+ 4. To connect to the database in n8n, use:
31
+ - **Host**: `localhost`
32
+ - **Port**: `5432`
33
+ - **Database**: `app_db`
34
+ - **User**: `app_user`
35
+ - **Password**: `app_password`
36
 
37
+ ## Sample Workflow
 
 
 
38
 
39
+ A sample workflow file `workflow.json` is included in the files. You can import it to test the connection.
40
 
 
 
 
41
 
start.sh CHANGED
@@ -7,28 +7,43 @@ export PGDATA
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 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"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  # Create the data directory if it doesn't exist
8
  if [ ! -d "$PGDATA" ]; then
9
  echo "Initializing database..."
10
+ # Configure Postgres to listen on localhost only (internal access for n8n)
11
+ echo "listen_addresses = '127.0.0.1'" >> "$PGDATA/postgresql.conf"
12
+ # Default port is 5432, so we don't need to specify it explicitly if we removed the override
13
+ # But let's be explicit to be safe, reverting previous change
14
+ # echo "port = 5432" >> "$PGDATA/postgresql.conf"
15
 
16
+ # Allow local connections
17
+ echo "host all all 127.0.0.1/32 trust" >> "$PGDATA/pg_hba.conf"
 
 
 
 
18
 
19
  # Create a default user and database
20
  # We start postgres in the background to run setup commands
21
  pg_ctl -D "$PGDATA" -w start
22
 
23
  echo "Creating default user and database..."
24
+ psql -d postgres -h 127.0.0.1 -c "CREATE USER app_user WITH PASSWORD 'app_password';"
25
+ psql -d postgres -h 127.0.0.1 -c "CREATE DATABASE app_db OWNER app_user;"
26
+ psql -d postgres -h 127.0.0.1 -c "GRANT ALL PRIVILEGES ON DATABASE app_db TO app_user;"
27
 
28
  pg_ctl -D "$PGDATA" stop
29
  fi
30
 
31
+ # Start Postgres in the background
32
+ echo "Starting PostgreSQL on port 5432..."
33
+ pg_ctl -D "$PGDATA" -w start &
34
+
35
+ # Wait for Postgres to be ready
36
+ until pg_isready; do
37
+ echo "Waiting for PostgreSQL to be ready..."
38
+ sleep 2
39
+ done
40
+
41
+ # Start n8n
42
+ echo "Starting n8n on port 7860..."
43
+ export N8N_PORT=7860
44
+ export N8N_HOST=0.0.0.0
45
+ export N8N_PROTOCOL=https
46
+ # Disable n8n basic auth for simplicity in this demo (or set it up if needed)
47
+ # For Spaces, we might need to handle auth carefully.
48
+ # n8n usually asks for setup on first run.
49
+ n8n start
workflow.json ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nodes": [
3
+ {
4
+ "parameters": {},
5
+ "id": "d0b0c6c0-0b0c-4b0c-8b0c-0b0c0b0c0b0c",
6
+ "name": "When clicking \"Execute Workflow\"",
7
+ "type": "n8n-nodes-base.manualTrigger",
8
+ "typeVersion": 1,
9
+ "position": [
10
+ 820,
11
+ 360
12
+ ]
13
+ },
14
+ {
15
+ "parameters": {
16
+ "operation": "executeQuery",
17
+ "query": "SELECT version();"
18
+ },
19
+ "id": "e1b1c7c1-1b1c-5b1c-9b1c-1b1c1b1c1b1c",
20
+ "name": "Postgres",
21
+ "type": "n8n-nodes-base.postgres",
22
+ "typeVersion": 1,
23
+ "position": [
24
+ 1040,
25
+ 360
26
+ ],
27
+ "credentials": {
28
+ "postgres": {
29
+ "id": "postgres-creds",
30
+ "name": "Local Postgres"
31
+ }
32
+ }
33
+ }
34
+ ],
35
+ "connections": {
36
+ "When clicking \"Execute Workflow\"": {
37
+ "main": [
38
+ [
39
+ {
40
+ "node": "Postgres",
41
+ "type": "main",
42
+ "index": 0
43
+ }
44
+ ]
45
+ ]
46
+ }
47
+ }
48
+ }