aki-008 commited on
Commit
0aaef94
Β·
1 Parent(s): 531c962

fix: force postgres socket to /tmp to avoid permission denied

Browse files
Files changed (1) hide show
  1. start.sh +53 -9
start.sh CHANGED
@@ -1,20 +1,66 @@
1
  #!/bin/bash
2
- set -e
3
 
4
- # --- 1. Set Environment Variables ---
 
 
 
 
 
 
 
 
 
 
 
5
  export HOME=/home/user
6
- # Use the DATABASE_URL from Hugging Face Secrets
7
- # ChromaDB settings
8
  export chroma_host="127.0.0.1"
9
  export chroma_port="8080"
10
  export chroma_collection="prepai_collection"
11
 
12
- # --- 2. Start ChromaDB ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  echo "🎨 Setting up ChromaDB..."
14
  mkdir -p ./chroma_store
15
  chroma run --host 0.0.0.0 --port 8080 --path ./chroma_store &
16
 
17
- # --- 3. Start Nginx (Non-root Mode) ---
18
  echo "🌐 Starting Nginx..."
19
  mkdir -p /tmp/nginx/body /tmp/nginx/proxy /tmp/nginx/fastcgi /tmp/nginx/uwsgi /tmp/nginx/scgi
20
 
@@ -53,9 +99,7 @@ EOF
53
 
54
  nginx -c /tmp/nginx.conf &
55
 
56
- # --- 4. Start Backend ---
57
  echo "🐍 Starting Backend..."
58
  cd Backend
59
- # Ensure tables are created in the new remote DB
60
- # (Your main.py lifespan event handles this via Base.metadata.create_all)
61
  python run.py
 
1
  #!/bin/bash
 
2
 
3
+ # --- 1. Dynamic PostgreSQL Path Detection ---
4
+ PG_BIN_DIR=$(find /usr/lib/postgresql -name pg_ctl | head -n 1 | xargs dirname)
5
+ if [ -z "$PG_BIN_DIR" ]; then
6
+ echo "❌ Error: Could not find PostgreSQL binaries."
7
+ exit 1
8
+ fi
9
+ echo "βœ… Found PostgreSQL binaries at $PG_BIN_DIR"
10
+ export PATH="$PG_BIN_DIR:$PATH"
11
+
12
+ # --- 2. Set Critical Environment Variables ---
13
+ export DATABASE_URL="postgresql+asyncpg://prepuser:password@127.0.0.1:5432/studentdb"
14
+ export PGDATA=/home/user/postgres_data
15
  export HOME=/home/user
 
 
16
  export chroma_host="127.0.0.1"
17
  export chroma_port="8080"
18
  export chroma_collection="prepai_collection"
19
 
20
+ # --- 3. Database Initialization ---
21
+ if [ -d "$PGDATA" ] && [ ! -f "$PGDATA/PG_VERSION" ]; then
22
+ echo "⚠️ $PGDATA exists but is not a valid cluster. Wiping..."
23
+ rm -rf "$PGDATA"
24
+ fi
25
+
26
+ if [ ! -d "$PGDATA" ]; then
27
+ echo "βš™οΈ Initializing database..."
28
+ initdb -D "$PGDATA" --auth-local=trust --no-locale --encoding=UTF8
29
+ fi
30
+
31
+ # --- 4. Start PostgreSQL (With Socket Fix) ---
32
+ echo "πŸš€ Starting PostgreSQL..."
33
+ # FIX: -o "-k /tmp" forces the socket lock file to be in /tmp (writable) instead of /var/run (protected)
34
+ pg_ctl -D "$PGDATA" -l /home/user/postgres.log -o "-k /tmp" start
35
+
36
+ # Wait a moment for startup...
37
+ sleep 3
38
+
39
+ # Check if it is actually running
40
+ if ! pg_isready -h 127.0.0.1 -p 5432; then
41
+ echo "❌ PostgreSQL failed to start. Printing contents of postgres.log:"
42
+ echo "----------------------------------------------------------------"
43
+ cat /home/user/postgres.log
44
+ echo "----------------------------------------------------------------"
45
+ exit 1
46
+ fi
47
+
48
+ echo "βœ… PostgreSQL is up and accepting connections!"
49
+
50
+ # --- 5. User & DB Setup ---
51
+ echo "πŸ› οΈ Configuring Database..."
52
+ psql -h 127.0.0.1 -d postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname='prepuser'" | grep -q 1 || \
53
+ psql -h 127.0.0.1 -d postgres -c "CREATE USER prepuser WITH PASSWORD 'password';"
54
+
55
+ psql -h 127.0.0.1 -d postgres -tAc "SELECT 1 FROM pg_database WHERE datname='studentdb'" | grep -q 1 || \
56
+ createdb -h 127.0.0.1 -O prepuser studentdb
57
+
58
+ # --- 6. ChromaDB Setup ---
59
  echo "🎨 Setting up ChromaDB..."
60
  mkdir -p ./chroma_store
61
  chroma run --host 0.0.0.0 --port 8080 --path ./chroma_store &
62
 
63
+ # --- 7. Nginx Setup (Non-root) ---
64
  echo "🌐 Starting Nginx..."
65
  mkdir -p /tmp/nginx/body /tmp/nginx/proxy /tmp/nginx/fastcgi /tmp/nginx/uwsgi /tmp/nginx/scgi
66
 
 
99
 
100
  nginx -c /tmp/nginx.conf &
101
 
102
+ # --- 8. Start Backend ---
103
  echo "🐍 Starting Backend..."
104
  cd Backend
 
 
105
  python run.py