fix: 支持1Panel外部PostgreSQL数据库

This commit is contained in:
Sens 2026-02-20 23:54:34 +08:00
parent 3376c224e3
commit e6ff0d5e54
1 changed files with 49 additions and 21 deletions

View File

@ -17,6 +17,17 @@ 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
# 检查是否为 root 用户
if [ "$EUID" -ne 0 ]; then
@ -86,29 +97,37 @@ echo -e "${GREEN}✅ 系统依赖安装完成${NC}"
# ========== 2. 配置 PostgreSQL ==========
echo -e "${YELLOW}[2/9] 配置 PostgreSQL 数据库...${NC}"
apt-get install -y --no-install-recommends postgresql postgresql-contrib || true
# 检查是否有外部 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
# 启动 PostgreSQL
echo "启动 PostgreSQL..."
systemctl start postgresql || true
systemctl enable postgresql || true
# 启动 PostgreSQL
echo "启动 PostgreSQL..."
timeout 10 systemctl start postgresql || true
systemctl enable postgresql || true
# 等待 PostgreSQL 就绪 (最多等待 30 秒)
echo "等待 PostgreSQL 就绪..."
for i in {1..30}; do
# 等待 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
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 "创建数据库..."
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
echo -e "${GREEN}✅ PostgreSQL 配置完成${NC}"
@ -191,8 +210,17 @@ 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
cat > .env << EOF
DATABASE_URL=postgresql+asyncpg://neuraluser:${DB_PASSWORD}@localhost:5432/neural_sync
DATABASE_URL=${DB_URL}
OLLAMA_HOST=http://127.0.0.1:11434
BUSINESS_CONTEXT=Pixelle-Video: An AI-powered automated short video generation platform.
EOF