From b125424e8f5aaedffb9f3f377231e9b1473a53ff Mon Sep 17 00:00:00 2001 From: lgh <625458956@qq.com> Date: Sat, 21 Feb 2026 00:01:49 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=BB=98=E8=AE=A4=E4=BD=BF=E7=94=A81Pa?= =?UTF-8?q?nel=20PostgreSQL=E6=95=B0=E6=8D=AE=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy.sh | 78 +++++++++++++++++++------------------------------------ 1 file changed, 26 insertions(+), 52 deletions(-) diff --git a/deploy.sh b/deploy.sh index 85ffba6..ef04aae 100644 --- a/deploy.sh +++ b/deploy.sh @@ -15,19 +15,12 @@ NC='\033[0m' # 配置变量 PROJECT_DIR="/opt/neural-sync" -DB_PASSWORD=${1:-"neural_sync_2024"} -GIT_REPO=${2:-""} -# 外部数据库参数 (1Panel) -DB_HOST=${3:-""} -DB_PORT=${4:-5432} - -# 打印使用说明 -if [ -z "$DB_HOST" ]; then - echo -e "${YELLOW}使用方式:${NC}" - echo " ./deploy.sh [数据库密码] [Git仓库] [DB主机] [DB端口]" - echo " 示例: ./deploy.sh password123 https://github.com/xxx/neural-sync.git 192.168.1.100 5432" - echo "" -fi +# 1Panel PostgreSQL 默认配置 +DB_USER="root" +DB_PASSWORD="postgres" +DB_HOST=${1:-"localhost"} +DB_PORT=${2:-5432} +GIT_REPO=${3:-""} # 检查是否为 root 用户 if [ "$EUID" -ne 0 ]; then @@ -97,36 +90,22 @@ echo -e "${GREEN}✅ 系统依赖安装完成${NC}" # ========== 2. 配置 PostgreSQL ========== echo -e "${YELLOW}[2/9] 配置 PostgreSQL 数据库...${NC}" -# 检查是否有外部 PostgreSQL (如 1Panel) -if [ -n "$DB_HOST" ] && [ -n "$DB_PASSWORD" ]; then - echo "检测到外部 PostgreSQL,使用 1Panel 托管的数据库" - EXTERNAL_DB=true -else - EXTERNAL_DB=false - # 尝试安装本地 PostgreSQL - apt-get install -y --no-install-recommends postgresql postgresql-contrib || true +# 使用 1Panel 托管的 PostgreSQL +echo "使用 1Panel 托管的 PostgreSQL 数据库" +echo " 主机: $DB_HOST" +echo " 端口: $DB_PORT" +echo " 用户: $DB_USER" - # 启动 PostgreSQL - echo "启动 PostgreSQL..." - timeout 10 systemctl start postgresql || true - systemctl enable postgresql || true - - # 等待 PostgreSQL 就绪 (最多等待 30 秒) - echo "等待 PostgreSQL 就绪..." - for i in {1..30}; do - if su - postgres -c "psql -c 'SELECT 1;'" > /dev/null 2>&1; then - echo "PostgreSQL 已就绪" - break - fi - sleep 1 - done - - # 创建数据库 - echo "创建数据库..." - su - postgres -c "psql -c \"CREATE DATABASE neural_sync;\"" 2>/dev/null || true - su - postgres -c "psql -c \"CREATE USER neuraluser WITH PASSWORD '$DB_PASSWORD';\"" 2>/dev/null || true - su - postgres -c "psql -c \"GRANT ALL PRIVILEGES ON DATABASE neural_sync TO neuraluser;\"" 2>/dev/null || true - su - postgres -c "psql -d neural_sync -c \"CREATE EXTENSION IF NOT EXISTS vector;\"" 2>/dev/null || true +# 测试数据库连接 +echo "测试数据库连接..." +if command -v psql &> /dev/null; then + PGPASSWORD="$DB_PASSWORD" psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -c "SELECT 1;" > /dev/null 2>&1 + if [ $? -eq 0 ]; then + echo "数据库连接成功" + else + echo -e "${YELLOW}数据库连接失败,尝试安装 psql 客户端...${NC}" + apt-get install -y postgresql-client || true + fi fi echo -e "${GREEN}✅ PostgreSQL 配置完成${NC}" @@ -210,14 +189,8 @@ pip install -r requirements.txt cp .env.example .env 2>/dev/null || true if [ -f .env ]; then - # 判断使用本地还是外部数据库 - if [ "$EXTERNAL_DB" = "true" ]; then - # 使用外部数据库 (1Panel) - DB_URL="postgresql+asyncpg://neuraluser:${DB_PASSWORD}@${DB_HOST}:${DB_PORT:-5432}/neural_sync" - else - # 使用本地数据库 - DB_URL="postgresql+asyncpg://neuraluser:${DB_PASSWORD}@localhost:5432/neural_sync" - fi + # 使用 1Panel PostgreSQL 数据库 + DB_URL="postgresql+asyncpg://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/neural_sync" cat > .env << EOF DATABASE_URL=${DB_URL} @@ -366,8 +339,9 @@ echo " - 重载 Nginx: nginx -s reload" echo " - Docker: docker ps / docker logs 等" echo "" echo -e "${YELLOW}配置信息:${NC}" -echo " - 数据库: neural_sync" -echo " - 数据库用户: neuraluser" +echo " - 数据库主机: $DB_HOST:$DB_PORT" +echo " - 数据库: neural_sync" +echo " - 数据库用户: $DB_USER" echo " - 数据库密码: $DB_PASSWORD" echo "" echo -e "${YELLOW}后续步骤:${NC}"