106 lines
3.6 KiB
Bash
Executable File
106 lines
3.6 KiB
Bash
Executable File
#!/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
|
|
|