#!/bin/bash # # HR Portal Complete Database Setup # 完整的一鍵設定腳本 - 包含所有 SQL # # 使用方法: # 1. 複製此腳本內容 # 2. SSH 登入 Ubuntu Server: ssh ubuntu@10.1.0.254 # 3. 建立檔案: nano ~/setup-hr-portal.sh # 4. 貼上內容並儲存 (Ctrl+X, Y, Enter) # 5. 執行: bash ~/setup-hr-portal.sh # set -e # 顏色定義 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' CYAN='\033[0;36m' NC='\033[0m' echo -e "${CYAN}========================================" echo " HR Portal Database Setup" echo "========================================${NC}" echo "" # 配置 DB_NAME="hr_portal" DB_USER="hr_user" POSTGRES_CONTAINER="postgres" # 提示輸入密碼 echo -e "${YELLOW}Please enter database password:${NC}" read -sp "Password for 'hr_user': " DB_PASSWORD echo "" echo "" if [ -z "$DB_PASSWORD" ]; then echo -e "${RED}Password cannot be empty!${NC}" exit 1 fi # ==================================== # Step 1: 檢查 PostgreSQL # ==================================== 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}" else echo -e "${RED}✗ PostgreSQL container not found${NC}" echo "Available containers:" docker ps -a | grep -i postgres || echo "No postgres containers" exit 1 fi echo "" # ==================================== # Step 2: 建立資料庫用戶 # ==================================== echo -e "${YELLOW}[2/6] Creating database user...${NC}" # 檢查用戶是否存在 USER_EXISTS=$(docker exec -i $POSTGRES_CONTAINER psql -U postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname='$DB_USER'" 2>&1 || echo "0") if echo "$USER_EXISTS" | grep -q "1"; then echo -e "${YELLOW}⚠ User '$DB_USER' already exists${NC}" read -p "Drop and recreate? (yes/no): " RECREATE_USER if [ "$RECREATE_USER" = "yes" ]; then docker exec -i $POSTGRES_CONTAINER psql -U postgres -c "DROP USER IF EXISTS $DB_USER CASCADE;" docker exec -i $POSTGRES_CONTAINER psql -U postgres <