#!/bin/bash # # 建立 HR Portal 專用的 PostgreSQL 容器 # 遵循微服務架構原則: Database per Service # set -e echo "==========================================" echo " Setup HR Portal PostgreSQL" echo " Microservice Architecture" echo "==========================================" echo "" DB_PASSWORD="DC1qaz2wsx" CONTAINER_NAME="hr-postgres" PORT="5432" # Step 1: 建立容器 echo "[1/5] Creating hr-postgres container..." docker run -d \ --name ${CONTAINER_NAME} \ --restart unless-stopped \ -e POSTGRES_PASSWORD="${DB_PASSWORD}" \ -e POSTGRES_INITDB_ARGS="--encoding=UTF-8" \ -e TZ=Asia/Taipei \ -p 0.0.0.0:${PORT}:5432 \ -v hr-postgres-data:/var/lib/postgresql/data \ --health-cmd="pg_isready -U postgres" \ --health-interval=10s \ --health-timeout=5s \ --health-retries=5 \ postgres:16-alpine echo " ✓ Container created" echo "" # Step 2: 等待啟動 echo "[2/5] Waiting for PostgreSQL to be ready..." for i in {1..30}; do if docker exec ${CONTAINER_NAME} pg_isready -U postgres >/dev/null 2>&1; then echo " ✓ PostgreSQL is ready!" break fi echo " Waiting... ($i/30)" sleep 1 done echo "" # Step 3: 建立用戶和資料庫 echo "[3/5] Creating hr_user and hr_portal database..." docker exec -i ${CONTAINER_NAME} psql -U postgres <