Files
st/deploy/start-postgres.sh

110 lines
3.0 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# ====================================================
# PostgreSQL Docker 快速启动脚本
# ====================================================
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
echo "=========================================="
echo "云酒馆 PostgreSQL 启动脚本"
echo "=========================================="
echo ""
# 检查 Docker 是否安装
if ! command -v docker &> /dev/null; then
echo "❌ 错误: 未检测到 Docker请先安装 Docker"
echo " 安装地址: https://www.docker.com/get-started"
exit 1
fi
# 检查 Docker Compose 是否安装
if ! command -v docker-compose &> /dev/null; then
echo "❌ 错误: 未检测到 Docker Compose请先安装"
exit 1
fi
# 检查 Docker 是否运行
if ! docker info &> /dev/null; then
echo "❌ 错误: Docker 服务未运行,请先启动 Docker"
exit 1
fi
echo "✅ Docker 环境检查通过"
echo ""
# 创建必要的目录
mkdir -p postgres
# 检查容器是否已经运行
if docker ps -a --format '{{.Names}}' | grep -q '^st-postgres$'; then
echo "📦 检测到已存在的容器"
if docker ps --format '{{.Names}}' | grep -q '^st-postgres$'; then
echo " 容器正在运行中..."
echo ""
echo "如需重启,请先执行: $0 stop"
exit 0
else
echo " 正在启动已存在的容器..."
docker-compose -f docker-compose.postgres.yml start
fi
else
echo "🚀 首次启动,正在创建容器..."
docker-compose -f docker-compose.postgres.yml up -d
fi
echo ""
echo "⏳ 等待数据库启动..."
sleep 3
# 检查数据库是否就绪
MAX_RETRIES=30
RETRY_COUNT=0
while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
if docker exec st-postgres pg_isready -U st_user -d st_dev &> /dev/null; then
echo "✅ 数据库已就绪!"
break
fi
RETRY_COUNT=$((RETRY_COUNT + 1))
echo " 等待中... ($RETRY_COUNT/$MAX_RETRIES)"
sleep 2
done
if [ $RETRY_COUNT -eq $MAX_RETRIES ]; then
echo "❌ 数据库启动超时,请检查日志:"
echo " docker-compose -f docker-compose.postgres.yml logs postgres"
exit 1
fi
echo ""
echo "=========================================="
echo "✨ PostgreSQL 启动成功!"
echo "=========================================="
echo ""
echo "📝 数据库连接信息:"
echo " Host: localhost"
echo " Port: 5432"
echo " Database: st_dev"
echo " Username: st_user"
echo " Password: st_password"
echo ""
echo "🌐 pgAdmin 管理界面:"
echo " URL: http://localhost:5050"
echo " Email: admin@st.local"
echo " Pass: admin123"
echo ""
echo "📊 常用命令:"
echo " 查看日志: docker-compose -f docker-compose.postgres.yml logs -f"
echo " 停止服务: $0 stop"
echo " 重启服务: $0 restart"
echo " 查看状态: docker-compose -f docker-compose.postgres.yml ps"
echo " 进入数据库: docker exec -it st-postgres psql -U st_user -d st_dev"
echo ""
echo "=========================================="