#!/bin/bash # Define data directory PGDATA="/var/lib/postgresql/data/pgdata" export PGDATA # Create the data directory if it doesn't exist if [ ! -d "$PGDATA" ]; then echo "Initializing database..." # Configure Postgres to listen on localhost only (internal access for n8n) echo "listen_addresses = '127.0.0.1'" >> "$PGDATA/postgresql.conf" # Default port is 5432, so we don't need to specify it explicitly if we removed the override # But let's be explicit to be safe, reverting previous change # echo "port = 5432" >> "$PGDATA/postgresql.conf" # Allow local connections echo "host all all 127.0.0.1/32 trust" >> "$PGDATA/pg_hba.conf" # Create a default user and database # We start postgres in the background to run setup commands pg_ctl -D "$PGDATA" -w start echo "Creating default user and database..." psql -d postgres -h 127.0.0.1 -c "CREATE USER app_user WITH PASSWORD 'app_password';" psql -d postgres -h 127.0.0.1 -c "CREATE DATABASE app_db OWNER app_user;" psql -d postgres -h 127.0.0.1 -c "GRANT ALL PRIVILEGES ON DATABASE app_db TO app_user;" pg_ctl -D "$PGDATA" stop fi # Start Postgres in the background echo "Starting PostgreSQL on port 5432..." pg_ctl -D "$PGDATA" -w start & # Wait for Postgres to be ready until pg_isready; do echo "Waiting for PostgreSQL to be ready..." sleep 2 done # Start n8n echo "Starting n8n on port 7860..." export N8N_PORT=7860 export N8N_HOST=0.0.0.0 export N8N_PROTOCOL=https # Disable n8n basic auth for simplicity in this demo (or set it up if needed) # For Spaces, we might need to handle auth carefully. # n8n usually asks for setup on first run. n8n start