diff --git a/monorepo/create_database.sh b/monorepo/create_database.sh index 652eb250..a5c24ad9 100755 --- a/monorepo/create_database.sh +++ b/monorepo/create_database.sh @@ -1,22 +1,46 @@ #!/bin/bash -# Define variables +# Define variables (consider using a config file for flexibility) DB_USER="postgres" -DB_PASSWORD="password" +DB_PASSWORD="password" DB_NAME="mydb" DB_SCHEMA="public" DB_PORT="5432" -# Update the system and install PostgreSQL -sudo pacman -Syu --noconfirm -sudo pacman -S --noconfirm postgresql +# Detect Linux distribution +if [[ -f /etc/arch-release ]]; then + # Arch Linux + echo "Detected Arch Linux" + INSTALL_CMD="sudo pacman -S --noconfirm" + SERVICE_CMD="sudo systemctl" + CONFIG_DIR="/var/lib/postgres/data" +elif [[ -f /etc/debian_version ]]; then + # Debian-based (Debian, Ubuntu, etc.) + echo "Detected Debian-based Linux" + INSTALL_CMD="sudo apt-get install -y" + SERVICE_CMD="sudo service" + CONFIG_DIR="/etc/postgresql" +else + echo "Unsupported Linux distribution. Exiting." + exit 1 +fi -# Initialize the database cluster -sudo -iu postgres initdb --locale $LANG -E UTF8 -D '/var/lib/postgres/data' +# Update package lists (important for Debian-based) +if [[ $INSTALL_CMD == *"apt-get"* ]]; then + sudo apt-get update -y +fi + +# Install PostgreSQL +$INSTALL_CMD postgresql + +# Initialize the database cluster (Arch only) +if [[ $INSTALL_CMD == *"pacman"* ]]; then + sudo -iu postgres initdb --locale $LANG -E UTF8 -D "$CONFIG_DIR" +fi # Start and enable PostgreSQL service -sudo systemctl start postgresql -sudo systemctl enable postgresql +$SERVICE_CMD start postgresql +$SERVICE_CMD enable postgresql # Switch to the postgres user to set up the database and user sudo -iu postgres psql <