You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitHub Actions中PostgreSQL+Diesel-rs多连接URL无效问题求助

问题

尝试搭建GitHub Action测试Rust应用时,执行diesel migration run持续报错:

无法通过postgresql://app_user@127.0.0.1:5432/app_testdb连接数据库:Invalid connection url for multiconnection

完整错误日志:

Run diesel migration run
  diesel migration run
  shell: /usr/bin/bash -e {0}
  env:
    DATABASE_URL: postgresql://app_user@127.0.0.1:5432/app_testdb
    CARGO_INCREMENTAL: 0
    CARGO_TERM_COLOR: always
Could not connect to database via `postgresql://app_user@127.0.0.1:5432/app_testdb`: Invalid connection url for multiconnection
Error: Process completed with exit code 1.

已完成的排查步骤:

  • 确认Postgresql.service正常运行
  • 添加pg_isready验证连接,步骤执行成功

使用的数据库URL:postgresql://test_user@127.0.0.1:5432/app_testdb

完整GitHub Action YAML配置:

name: App-Testing
on:
  push:
    branches:
      - "**/*"
  pull_request:

concurrency:
  group: "${{ github.ref }}"
  cancel-in-progress: true

jobs:
 
  Test-App-Build:
    runs-on: ubuntu-latest

    steps:
      - name: Start PostgreSQL
        run: |
          sudo systemctl enable postgresql.service
          sudo systemctl start postgresql.service
          
      # Create the app database
      - name: Create database
        run: |
          cd ~postgres/
          sudo -u postgres psql -c 'CREATE DATABASE app_testdb;'

      # Create the app database user
      - name: Create database user
        run: | 
          cd ~postgres/
          sudo sudo netstat -plunt |grep postgres
          sudo -u postgres psql -c "CREATE USER app_user WITH PASSWORD 'changeme';"
          sudo -u postgres psql -c "grant all privileges on database app_testdb to app_user;"

      # Set up environment variables for database URLs
      - name: Set up environment variables
        run: echo "DATABASE_URL=postgresql://app_user@127.0.0.1:5432/app_testdb" > "$GITHUB_ENV"

      - name: checkout
        uses: actions/checkout@v3.5.3
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt, clippy

      # Install diesel CLI if not already installed
      - name: Check diesel installation
        run: if which diesel; then echo "diesel already installed"; else cargo install diesel_cli --no-default-features --features=postgres; fi

      # Run database migrations
      - run: pushd app-models
      
      # Test the database connection as app_user
      - name: Test app_user database connection
        run: pg_isready

      - run: diesel migration run
      - run: popd

      - run: echo "SCRIPT"
      - run: cargo build --all --verbose
      - run: cargo test --all --verbose

备注:已知可使用Docker容器,但GitHub Runner的ubuntu-latest已预装PostgreSQL,不想通过外部数据库服务增加复杂度。

解决方案

1. 修复数据库URL格式

Diesel的PostgreSQL连接URL必须明确包含密码(即使密码为空),当前URL缺少密码字段导致解析失败。修改DATABASE_URL为:

postgresql://app_user:changeme@127.0.0.1:5432/app_testdb

确保密码和创建用户时设置的changeme一致。

2. 调整PostgreSQL身份验证配置

GitHub Runner预装的PostgreSQL默认使用ident验证方式,不允许密码登录。需要修改pg_hba.conf切换为md5验证:
在Action中添加以下步骤:

sudo sed -i '/host    all             all             127.0.0.1\/32            ident/c\host    all             all             127.0.0.1/32            md5' /etc/postgresql/*/main/pg_hba.conf
sudo systemctl restart postgresql.service

3. 验证Diesel CLI的PostgreSQL特性

安装Diesel CLI时确保postgres特性正确启用,执行以下命令检查:

diesel --version

若输出不含postgres,重新安装:

cargo uninstall diesel_cli
cargo install diesel_cli --no-default-features --features=postgres

4. 修正pg_isready的验证逻辑

当前pg_isready默认使用系统用户连接,无法验证app_user的权限。修改验证步骤为:

PGPASSWORD=changeme pg_isready -h 127.0.0.1 -p 5432 -d app_testdb -U app_user

内容的提问来源于stack exchange,提问作者BanjoFox

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 16:34:59