Search code examples
postgresqlsqlalchemygithub-actions

Can't connect to PostgreSQL DB in a Github Action - Temporary failure in name resolution


I'm trying to connect to a Postgres DB in the Github workflow but I am getting connection is bad: Temporary failure in name resolution. My workflow config:

  test:
    runs-on: ubuntu-latest
    container: python:3.11-slim
    env:
      POSTGRES_HOST: postgres
      POSTGRES_DB: test
      POSTGRES_PASSWORD: postgres
    services:
      postgres:
        image: postgres
    needs: lint
    steps:
    - uses: actions/checkout@v3
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
        alembic upgrade head
    - name: Run tests
      run: pytest

and my code where I get the DB host:

user = os.getenv("POSTGRES_USER", "postgres")
password = os.getenv("POSTGRES_PASSWORD", "postgres")
host = os.getenv("POSTGRES_HOST", "localhost")
name = os.getenv("POSTGRES_DB", "test")

SQLALCHEMY_DATABASE_URL = f"postgresql+psycopg://{user}:{password}@{host}:5432/{name}"

Solution

  • Seems that you must add the health checks in order for Postgres to be ready:

        services:
          postgres:
            image: postgres
            env:
              POSTGRES_PASSWORD: postgres
              POSTGRES_DB: test
            options: >-
              --health-cmd pg_isready
              --health-interval 10s
              --health-timeout 5s
              --health-retries 5