aki-008 commited on
Commit
d430e0d
Β·
1 Parent(s): 1954de6

fix: force postgres socket to /tmp and print logs on failure

Browse files
Files changed (1) hide show
  1. start.sh +9 -13
start.sh CHANGED
@@ -2,7 +2,6 @@
2
  set -e # Exit immediately if any command fails
3
 
4
  # --- 1. Dynamic PostgreSQL Path Detection ---
5
- # Find the directory containing pg_ctl (works for any postgres version)
6
  PG_BIN_DIR=$(find /usr/lib/postgresql -name pg_ctl | head -n 1 | xargs dirname)
7
 
8
  if [ -z "$PG_BIN_DIR" ]; then
@@ -23,7 +22,6 @@ export chroma_port="8080"
23
  export chroma_collection="prepai_collection"
24
 
25
  # --- 3. Database Initialization ---
26
- # If PGDATA exists but is missing the version file, it's corrupt. Wipe it.
27
  if [ -d "$PGDATA" ] && [ ! -f "$PGDATA/PG_VERSION" ]; then
28
  echo "⚠️ $PGDATA exists but is not a valid cluster. Wiping to start fresh..."
29
  rm -rf "$PGDATA"
@@ -31,15 +29,19 @@ fi
31
 
32
  if [ ! -d "$PGDATA" ]; then
33
  echo "βš™οΈ Initializing database..."
34
- # Initialize with trust auth for local connections to avoid password issues during setup
35
  initdb -D "$PGDATA" --auth-local=trust --no-locale --encoding=UTF8
36
  fi
37
 
38
- # --- 4. Start PostgreSQL ---
39
  echo "πŸš€ Starting PostgreSQL..."
40
- pg_ctl -D "$PGDATA" -l /home/user/postgres.log start
 
 
 
 
 
41
 
42
- # CRITICAL: Wait for Postgres to be ready before continuing
43
  echo "⏳ Waiting for PostgreSQL to accept connections..."
44
  until pg_isready -h 127.0.0.1 -p 5432; do
45
  echo " ...waiting for DB..."
@@ -49,7 +51,6 @@ echo "βœ… PostgreSQL is up!"
49
 
50
  # --- 5. User & DB Setup ---
51
  echo "πŸ› οΈ Configuring Database..."
52
- # Idempotent creation: Only create if they don't exist
53
  psql -h 127.0.0.1 -d postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname='prepuser'" | grep -q 1 || \
54
  psql -h 127.0.0.1 -d postgres -c "CREATE USER prepuser WITH PASSWORD 'password';"
55
 
@@ -58,21 +59,17 @@ createdb -h 127.0.0.1 -O prepuser studentdb
58
 
59
  # --- 6. ChromaDB Setup ---
60
  echo "🎨 Setting up ChromaDB..."
61
- # Ensure directory exists and we own it
62
  mkdir -p ./chroma_store
63
- # Start Chroma in the background
64
  chroma run --host 0.0.0.0 --port 8080 --path ./chroma_store &
65
 
66
- # --- 7. Nginx Setup (Non-root Mode) ---
67
  echo "🌐 Starting Nginx..."
68
- # Create temp directories for Nginx to write to (avoids permission errors)
69
  mkdir -p /tmp/nginx/body \
70
  /tmp/nginx/proxy \
71
  /tmp/nginx/fastcgi \
72
  /tmp/nginx/uwsgi \
73
  /tmp/nginx/scgi
74
 
75
- # Generate a temporary nginx.conf that listens on port 7860
76
  cat <<EOF > /tmp/nginx.conf
77
  worker_processes 1;
78
  daemon off;
@@ -108,7 +105,6 @@ http {
108
  }
109
  EOF
110
 
111
- # Start Nginx using the temp config
112
  nginx -c /tmp/nginx.conf &
113
 
114
  # --- 8. Start Backend ---
 
2
  set -e # Exit immediately if any command fails
3
 
4
  # --- 1. Dynamic PostgreSQL Path Detection ---
 
5
  PG_BIN_DIR=$(find /usr/lib/postgresql -name pg_ctl | head -n 1 | xargs dirname)
6
 
7
  if [ -z "$PG_BIN_DIR" ]; then
 
22
  export chroma_collection="prepai_collection"
23
 
24
  # --- 3. Database Initialization ---
 
25
  if [ -d "$PGDATA" ] && [ ! -f "$PGDATA/PG_VERSION" ]; then
26
  echo "⚠️ $PGDATA exists but is not a valid cluster. Wiping to start fresh..."
27
  rm -rf "$PGDATA"
 
29
 
30
  if [ ! -d "$PGDATA" ]; then
31
  echo "βš™οΈ Initializing database..."
 
32
  initdb -D "$PGDATA" --auth-local=trust --no-locale --encoding=UTF8
33
  fi
34
 
35
+ # --- 4. Start PostgreSQL (With Fixes) ---
36
  echo "πŸš€ Starting PostgreSQL..."
37
+ # FIX: Use -o "-k /tmp" to force socket creation in /tmp instead of protected /var/run
38
+ if ! pg_ctl -D "$PGDATA" -l /home/user/postgres.log -o "-k /tmp" start; then
39
+ echo "❌ PostgreSQL failed to start. Printing logs:"
40
+ cat /home/user/postgres.log
41
+ exit 1
42
+ fi
43
 
44
+ # Wait for Postgres to be ready
45
  echo "⏳ Waiting for PostgreSQL to accept connections..."
46
  until pg_isready -h 127.0.0.1 -p 5432; do
47
  echo " ...waiting for DB..."
 
51
 
52
  # --- 5. User & DB Setup ---
53
  echo "πŸ› οΈ Configuring Database..."
 
54
  psql -h 127.0.0.1 -d postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname='prepuser'" | grep -q 1 || \
55
  psql -h 127.0.0.1 -d postgres -c "CREATE USER prepuser WITH PASSWORD 'password';"
56
 
 
59
 
60
  # --- 6. ChromaDB Setup ---
61
  echo "🎨 Setting up ChromaDB..."
 
62
  mkdir -p ./chroma_store
 
63
  chroma run --host 0.0.0.0 --port 8080 --path ./chroma_store &
64
 
65
+ # --- 7. Nginx Setup ---
66
  echo "🌐 Starting Nginx..."
 
67
  mkdir -p /tmp/nginx/body \
68
  /tmp/nginx/proxy \
69
  /tmp/nginx/fastcgi \
70
  /tmp/nginx/uwsgi \
71
  /tmp/nginx/scgi
72
 
 
73
  cat <<EOF > /tmp/nginx.conf
74
  worker_processes 1;
75
  daemon off;
 
105
  }
106
  EOF
107
 
 
108
  nginx -c /tmp/nginx.conf &
109
 
110
  # --- 8. Start Backend ---