GrowthPanelBackend / start.sh
hisham-cmd's picture
Upload start.sh
04f985b verified
Raw
History Blame Contribute Delete
2.33 kB
#!/bin/bash
set -e
# Initialize PostgreSQL data directory if it's empty
if [ -z "$(ls -A /var/lib/postgresql/data)" ]; then
echo "Initializing PostgreSQL database..."
su - postgres -c "/usr/lib/postgresql/15/bin/initdb -D /var/lib/postgresql/data"
# Start PostgreSQL locally to create user and db
su - postgres -c "/usr/lib/postgresql/15/bin/pg_ctl -D /var/lib/postgresql/data -o '-c listen_addresses=localhost' -w start"
# Create the user and database using environment variables or defaults
DB_USER=${MONGO_USER:-admin}
DB_PASSWORD=${MONGO_PASSWORD:-admin123}
DB_NAME=${DB_NAME:-growthpanel}
echo "Creating user $DB_USER and database $DB_NAME..."
su - postgres -c "psql -c \"CREATE USER $DB_USER WITH PASSWORD '$DB_PASSWORD';\""
su - postgres -c "psql -c \"CREATE DATABASE $DB_NAME OWNER $DB_USER;\""
su - postgres -c "psql -c \"ALTER ROLE $DB_USER SET client_encoding TO 'utf8';\""
su - postgres -c "psql -c \"ALTER ROLE $DB_USER SET default_transaction_isolation TO 'read committed';\""
su - postgres -c "psql -c \"ALTER ROLE $DB_USER SET timezone TO 'UTC';\""
su - postgres -c "psql -c \"GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;\""
# Stop the local instance
su - postgres -c "/usr/lib/postgresql/15/bin/pg_ctl -D /var/lib/postgresql/data -m fast -w stop"
# Allow local connections with password
echo "host all all 127.0.0.1/32 md5" >> /var/lib/postgresql/data/pg_hba.conf
echo "host all all ::1/128 md5" >> /var/lib/postgresql/data/pg_hba.conf
fi
# Apply Alembic Migrations if they exist
echo "Running database migrations..."
# Make sure to wait a couple of seconds for supervisor to start Postgres if running via supervisor,
# but we are in start.sh before supervisor. So we will start postgres in background, run migrations, stop, then run supervisor.
# Alternatively, since we use supervisor, we can let supervisor start both, but we need to run migrations.
# To be safe, we will just start supervisor, and let FastAPI or a separate script handle migrations.
# Actually, the best way in Docker is to run FastAPI and let it connect when DB is ready.
# Start Supervisor which runs both PostgreSQL and FastAPI
echo "Starting Supervisor..."
exec /usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf