PostgreSQL in GitHub Actions for testing

Set up PostgreSQL in your GitHub Actions workflow by using the "services" section in a job:

jobs:
  build:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:17
        env:
          POSTGRES_USER: user
          POSTGRES_PASSWORD: password
          POSTGRES_DB: database
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        volumes:
        - "/var/run/postgresql:/var/run/postgresql"
    steps:
    - uses: actions/checkout@v7
    # More steps ...

Of note is the volume mount for "/var/run/postgresql", which lets us use a Unix-domain socket to connect to PostgreSQL with. Instead of messing with container networking and figuring out which IP to use.

For further info see GitHub's documentation. They mention that the job also needs to run in a container, but that isn't necessary.

See also nbsp's Rust workflow for a full working example. In it Rust with SQLx connects to PostgreSQL during testing.