feat: create database hnow installs for debian

This commit is contained in:
Krzysztof Rudnicki 2024-06-17 20:42:51 +02:00
parent e5a98255cb
commit 60b30c7af8

View File

@ -1,22 +1,46 @@
#!/bin/bash #!/bin/bash
# Define variables # Define variables (consider using a config file for flexibility)
DB_USER="postgres" DB_USER="postgres"
DB_PASSWORD="password" DB_PASSWORD="password"
DB_NAME="mydb" DB_NAME="mydb"
DB_SCHEMA="public" DB_SCHEMA="public"
DB_PORT="5432" DB_PORT="5432"
# Update the system and install PostgreSQL # Detect Linux distribution
sudo pacman -Syu --noconfirm if [[ -f /etc/arch-release ]]; then
sudo pacman -S --noconfirm postgresql # 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 # Update package lists (important for Debian-based)
sudo -iu postgres initdb --locale $LANG -E UTF8 -D '/var/lib/postgres/data' 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 # Start and enable PostgreSQL service
sudo systemctl start postgresql $SERVICE_CMD start postgresql
sudo systemctl enable postgresql $SERVICE_CMD enable postgresql
# Switch to the postgres user to set up the database and user # Switch to the postgres user to set up the database and user
sudo -iu postgres psql <<EOF sudo -iu postgres psql <<EOF
@ -24,18 +48,19 @@ ALTER USER $DB_USER WITH PASSWORD '$DB_PASSWORD';
CREATE DATABASE $DB_NAME; CREATE DATABASE $DB_NAME;
EOF EOF
# Modify pg_hba.conf to allow password authentication # Modify configuration files to allow password authentication (Arch only)
sudo sed -i "s/#listen_addresses = 'localhost'/listen_addresses = 'localhost'/g" /var/lib/postgres/data/postgresql.conf if [[ $INSTALL_CMD == *"pacman"* ]]; then
sudo sed -i "s/peer/md5/g" /var/lib/postgres/data/pg_hba.conf sudo sed -i "s/#listen_addresses = 'localhost'/listen_addresses = 'localhost'/g" "$CONFIG_DIR/postgresql.conf"
sudo sed -i "s/ident/md5/g" /var/lib/postgres/data/pg_hba.conf sudo sed -i "s/peer/md5/g" "$CONFIG_DIR/pg_hba.conf"
sudo sed -i "s/ident/md5/g" "$CONFIG_DIR/pg_hba.conf"
fi
# Restart PostgreSQL to apply changes # Restart PostgreSQL to apply changes
sudo systemctl restart postgresql $SERVICE_CMD restart postgresql
# Export the DATABASE_URL # Export the DATABASE_URL
export DATABASE_URL="postgresql://$DB_USER:$DB_PASSWORD@localhost:$DB_PORT/$DB_NAME?schema=$DB_SCHEMA" export DATABASE_URL="postgresql://$DB_USER:$DB_PASSWORD@localhost:$DB_PORT/$DB_NAME?schema=$DB_SCHEMA"
# Print the DATABASE_URL to verify # Print the DATABASE_URL to verify
echo "DATABASE_URL=\"$DATABASE_URL\"" echo "DATABASE_URL="$DATABASE_URL""
echo "PostgreSQL setup is complete. The database is accessible at the specified URL." echo "PostgreSQL setup is complete. The database is accessible at the specified URL."