feat: 默认使用1Panel PostgreSQL数据库

This commit is contained in:
Sens 2026-02-21 00:01:49 +08:00
parent 319a422362
commit b125424e8f
1 changed files with 26 additions and 52 deletions

View File

@ -15,19 +15,12 @@ NC='\033[0m'
# 配置变量 # 配置变量
PROJECT_DIR="/opt/neural-sync" PROJECT_DIR="/opt/neural-sync"
DB_PASSWORD=${1:-"neural_sync_2024"} # 1Panel PostgreSQL 默认配置
GIT_REPO=${2:-""} DB_USER="root"
# 外部数据库参数 (1Panel) DB_PASSWORD="postgres"
DB_HOST=${3:-""} DB_HOST=${1:-"localhost"}
DB_PORT=${4:-5432} DB_PORT=${2:-5432}
GIT_REPO=${3:-""}
# 打印使用说明
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
# 检查是否为 root 用户 # 检查是否为 root 用户
if [ "$EUID" -ne 0 ]; then if [ "$EUID" -ne 0 ]; then
@ -97,36 +90,22 @@ echo -e "${GREEN}✅ 系统依赖安装完成${NC}"
# ========== 2. 配置 PostgreSQL ========== # ========== 2. 配置 PostgreSQL ==========
echo -e "${YELLOW}[2/9] 配置 PostgreSQL 数据库...${NC}" echo -e "${YELLOW}[2/9] 配置 PostgreSQL 数据库...${NC}"
# 检查是否有外部 PostgreSQL (如 1Panel) # 使用 1Panel 托管的 PostgreSQL
if [ -n "$DB_HOST" ] && [ -n "$DB_PASSWORD" ]; then echo "使用 1Panel 托管的 PostgreSQL 数据库"
echo "检测到外部 PostgreSQL,使用 1Panel 托管的数据库" echo " 主机: $DB_HOST"
EXTERNAL_DB=true echo " 端口: $DB_PORT"
else echo " 用户: $DB_USER"
EXTERNAL_DB=false
# 尝试安装本地 PostgreSQL
apt-get install -y --no-install-recommends postgresql postgresql-contrib || true
# 启动 PostgreSQL # 测试数据库连接
echo "启动 PostgreSQL..." echo "测试数据库连接..."
timeout 10 systemctl start postgresql || true if command -v psql &> /dev/null; then
systemctl enable postgresql || true PGPASSWORD="$DB_PASSWORD" psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -c "SELECT 1;" > /dev/null 2>&1
if [ $? -eq 0 ]; then
# 等待 PostgreSQL 就绪 (最多等待 30 秒) echo "数据库连接成功"
echo "等待 PostgreSQL 就绪..." else
for i in {1..30}; do echo -e "${YELLOW}数据库连接失败,尝试安装 psql 客户端...${NC}"
if su - postgres -c "psql -c 'SELECT 1;'" > /dev/null 2>&1; then apt-get install -y postgresql-client || true
echo "PostgreSQL 已就绪" fi
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
fi fi
echo -e "${GREEN}✅ PostgreSQL 配置完成${NC}" echo -e "${GREEN}✅ PostgreSQL 配置完成${NC}"
@ -210,14 +189,8 @@ pip install -r requirements.txt
cp .env.example .env 2>/dev/null || true cp .env.example .env 2>/dev/null || true
if [ -f .env ]; then if [ -f .env ]; then
# 判断使用本地还是外部数据库 # 使用 1Panel PostgreSQL 数据库
if [ "$EXTERNAL_DB" = "true" ]; then DB_URL="postgresql+asyncpg://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/neural_sync"
# 使用外部数据库 (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
cat > .env << EOF cat > .env << EOF
DATABASE_URL=${DB_URL} DATABASE_URL=${DB_URL}
@ -366,8 +339,9 @@ echo " - 重载 Nginx: nginx -s reload"
echo " - Docker: docker ps / docker logs 等" echo " - Docker: docker ps / docker logs 等"
echo "" echo ""
echo -e "${YELLOW}配置信息:${NC}" echo -e "${YELLOW}配置信息:${NC}"
echo " - 数据库: neural_sync" echo " - 数据库主机: $DB_HOST:$DB_PORT"
echo " - 数据库用户: neuraluser" echo " - 数据库: neural_sync"
echo " - 数据库用户: $DB_USER"
echo " - 数据库密码: $DB_PASSWORD" echo " - 数据库密码: $DB_PASSWORD"
echo "" echo ""
echo -e "${YELLOW}后续步骤:${NC}" echo -e "${YELLOW}后续步骤:${NC}"