#!/bin/bash # # HR Portal Database Setup Script # Run this script on Ubuntu Server (10.1.0.254) # # Usage: bash setup-hr-portal-db.sh # set -e echo "========================================" echo " HR Portal Database Setup" echo "========================================" echo "" # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' CYAN='\033[0;36m' NC='\033[0m' # No Color # Configuration DB_NAME="hr_portal" DB_USER="hr_user" DB_PASSWORD="" POSTGRES_CONTAINER="postgres" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # Step 1: Check PostgreSQL container echo -e "${YELLOW}[1/6] Checking PostgreSQL container...${NC}" if docker ps | grep -q "$POSTGRES_CONTAINER"; then echo -e "${GREEN} ✓ PostgreSQL container is running${NC}" docker ps --filter "name=$POSTGRES_CONTAINER" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" else echo -e "${RED} ✗ PostgreSQL container not found or not running${NC}" echo "" echo "Available containers:" docker ps -a | grep -i postgres || echo "No PostgreSQL containers found" echo "" read -p "Enter PostgreSQL container name: " POSTGRES_CONTAINER fi echo "" # Step 2: Prompt for passwords echo -e "${YELLOW}[2/6] Setting up credentials...${NC}" read -sp "Enter password for database user 'hr_user': " DB_PASSWORD echo "" if [ -z "$DB_PASSWORD" ]; then echo -e "${RED} ✗ Password cannot be empty${NC}" exit 1 fi echo -e "${GREEN} ✓ Credentials configured${NC}" echo "" # Step 3: Create database user echo -e "${YELLOW}[3/6] Creating database user '$DB_USER'...${NC}" # Check if user exists USER_EXISTS=$(docker exec -i $POSTGRES_CONTAINER psql -U postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname='$DB_USER'" 2>&1) if echo "$USER_EXISTS" | grep -q "1"; then echo -e "${YELLOW} ⚠ User '$DB_USER' already exists, skipping...${NC}" else docker exec -i $POSTGRES_CONTAINER psql -U postgres <