#!/bin/bash # Ubuntu PostgreSQL Setup Script for CyberHybridHub # Installs PostgreSQL and creates database with specified credentials set -e # Exit on any error # Database configuration DB_USER="postgres" DB_PASSWORD="" DB_NAME="cyberhybridhub-db" DB_PORT="5432" echo "๐Ÿš€ Setting up PostgreSQL for CyberHybridHub..." # Update system packages echo "๐Ÿ“ฆ Updating system packages..." sudo apt update && sudo apt upgrade -y # Install PostgreSQL and dependencies echo "๐Ÿ“ฆ Installing PostgreSQL..." sudo apt install -y postgresql postgresql-contrib postgresql-client # Ensure PostgreSQL service is running echo "๐Ÿ”„ Starting PostgreSQL service..." sudo systemctl start postgresql sudo systemctl enable postgresql # Wait for PostgreSQL to be ready echo "โณ Waiting for PostgreSQL to be ready..." sleep 5 # Test PostgreSQL connection sudo -u postgres psql -c "SELECT version();" > /dev/null 2>&1 if [ $? -ne 0 ]; then echo "โŒ PostgreSQL is not responding. Please check the installation." exit 1 fi # Set password for postgres user echo "๐Ÿ” Setting postgres user password..." sudo -u postgres psql -c "ALTER USER postgres PASSWORD '$DB_PASSWORD';" # Create the database echo "๐Ÿ—„๏ธ Creating database '$DB_NAME'..." sudo -u postgres psql -c "CREATE DATABASE \"$DB_NAME\";" 2>/dev/null || echo "Database '$DB_NAME' already exists" # Grant privileges echo "๐Ÿ‘ฅ Granting privileges..." sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE \"$DB_NAME\" TO postgres;" # Configure PostgreSQL for local connections echo "โš™๏ธ Configuring PostgreSQL..." PG_CONF="/etc/postgresql/*/main/postgresql.conf" PG_HBA="/etc/postgresql/*/main/pg_hba.conf" # Allow local connections sudo sed -i "s/#listen_addresses = 'localhost'/listen_addresses = 'localhost'/" $PG_CONF sudo sed -i "s/local all postgres peer/local all postgres md5/" $PG_HBA sudo sed -i "s/local all all peer/local all all md5/" $PG_HBA # Restart PostgreSQL to apply changes echo "๐Ÿ”„ Restarting PostgreSQL..." sudo systemctl restart postgresql # Test connection with new credentials echo "๐Ÿงช Testing database connection..." export PGPASSWORD="$DB_PASSWORD" psql -h localhost -U "$DB_USER" -d "$DB_NAME" -c "SELECT current_database(), current_user;" > /dev/null 2>&1 if [ $? -eq 0 ]; then echo "โœ… Database connection test successful!" else echo "โŒ Database connection test failed!" exit 1 fi # Display connection information echo "" echo "๐ŸŽ‰ PostgreSQL setup completed successfully!" echo "" echo "๐Ÿ“‹ Connection Details:" echo " Host: localhost" echo " Port: $DB_PORT" echo " Database: $DB_NAME" echo " User: $DB_USER" echo " Password: $DB_PASSWORD" echo "" echo "๐Ÿ”— Connection String for .env file:" echo " POSTGRES_OPTIONS=\"postgresql://$DB_USER:$DB_PASSWORD@localhost:$DB_PORT/$DB_NAME\"" echo "" echo "๐Ÿ“ Add this to your .env file:" echo " POSTGRES_OPTIONS=\"postgresql://postgres:9UMrd6hKBho4RhoyP47t@localhost:5432/cyberhybridhub-db\"" echo "" echo "๐Ÿ”ง Useful commands:" echo " Start service: sudo systemctl start postgresql" echo " Stop service: sudo systemctl stop postgresql" echo " Status: sudo systemctl status postgresql" echo " Connect: psql -h localhost -U postgres -d cyberhybridhub-db" echo "" # Optional: Create a systemd service for auto-start on boot (already enabled above) echo "๐Ÿ”’ PostgreSQL is configured to start automatically on system boot." unset PGPASSWORD