somratpro Claude Sonnet 4.6 commited on
Commit
0c60c15
·
1 Parent(s): 4bc33bf

fix: initialize PostgreSQL cluster before starting

Browse files

Use pg_createcluster + pg_ctlcluster (Debian way) instead of
raw postgres -D. Auto-detect installed PG version. Initialize
data dir on first boot, start cluster on every restart.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

Files changed (1) hide show
  1. start.sh +28 -14
start.sh CHANGED
@@ -65,26 +65,40 @@ echo -e "${GREEN}✓ Environment validated${NC}\n"
65
  # ============================================================================
66
  echo -e "${BLUE}[2/8] Setting up PostgreSQL database...${NC}"
67
 
68
- # Start PostgreSQL if not running
69
- if ! pgrep -x "postgres" > /dev/null; then
70
- echo "Starting PostgreSQL daemon..."
71
- su - postgres -c "/usr/lib/postgresql/*/bin/postgres -D /var/lib/postgresql/data" &
72
- POSTGRES_PID=$!
73
-
74
- # Wait for PostgreSQL to start
75
- sleep 3
76
- until pg_isready -h localhost -U postgres 2>/dev/null; do
77
- sleep 1
78
- done
79
  fi
 
 
80
 
81
- # Create paperclip user and database if they don't exist
82
- psql -U postgres -tc "SELECT 1 FROM pg_user WHERE usename = 'postgres'" | grep -q 1 || \
83
- psql -U postgres -c "CREATE USER postgres WITH PASSWORD 'paperclip' CREATEDB;" 2>/dev/null || true
 
 
 
 
 
 
 
 
84
 
 
 
 
 
 
 
 
85
  psql -U postgres -tc "SELECT 1 FROM pg_database WHERE datname = 'paperclip'" | grep -q 1 || \
86
  psql -U postgres -c "CREATE DATABASE paperclip OWNER postgres;" 2>/dev/null || true
87
 
 
 
 
88
  echo -e "${GREEN}✓ PostgreSQL ready${NC}\n"
89
 
90
  # ============================================================================
 
65
  # ============================================================================
66
  echo -e "${BLUE}[2/8] Setting up PostgreSQL database...${NC}"
67
 
68
+ # Detect installed PostgreSQL version
69
+ PG_VERSION=$(ls /usr/lib/postgresql/ 2>/dev/null | sort -V | tail -1)
70
+ if [ -z "$PG_VERSION" ]; then
71
+ echo -e "${RED}ERROR: PostgreSQL not found${NC}"
72
+ exit 1
 
 
 
 
 
 
73
  fi
74
+ PG_DATA="/var/lib/postgresql/${PG_VERSION}/main"
75
+ echo "PostgreSQL version: ${PG_VERSION}, data dir: ${PG_DATA}"
76
 
77
+ # Initialize cluster if it doesn't exist yet
78
+ if [ ! -f "${PG_DATA}/PG_VERSION" ]; then
79
+ echo "Initializing PostgreSQL cluster..."
80
+ pg_createcluster "${PG_VERSION}" main --locale=C.UTF-8
81
+ fi
82
+
83
+ # Start cluster if not running
84
+ if ! pg_ctlcluster "${PG_VERSION}" main status 2>/dev/null | grep -q "online"; then
85
+ echo "Starting PostgreSQL cluster..."
86
+ pg_ctlcluster "${PG_VERSION}" main start
87
+ fi
88
 
89
+ # Wait until ready
90
+ until pg_isready -h localhost -U postgres 2>/dev/null; do
91
+ sleep 1
92
+ done
93
+
94
+ # Set postgres password and create paperclip DB
95
+ psql -U postgres -c "ALTER USER postgres WITH PASSWORD 'paperclip';" 2>/dev/null || true
96
  psql -U postgres -tc "SELECT 1 FROM pg_database WHERE datname = 'paperclip'" | grep -q 1 || \
97
  psql -U postgres -c "CREATE DATABASE paperclip OWNER postgres;" 2>/dev/null || true
98
 
99
+ # Export correct DATABASE_URL with detected version credentials
100
+ export DATABASE_URL="${DATABASE_URL:-postgres://postgres:paperclip@localhost:5432/paperclip}"
101
+
102
  echo -e "${GREEN}✓ PostgreSQL ready${NC}\n"
103
 
104
  # ============================================================================