user3472360
user3472360

Reputation: 2095

Using psql in a github action

I am trying to use psql in a github action but am seeing the following error:

psql: error: could not connect to server: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

where my github action yml file is shown below (the run_all_tests.sh file just calls a subprocess that tries to run the command psql). Does anyone know why this could be happening?

name: Python application

on:
  push:
    branches: [master]
  pull_request:
    branches: [master]

jobs:
  build:
    runs-on: ubuntu-latest

    # Service containers to run with `container-job`
    services:
      # Label used to access the service container
      postgres:
        # Docker Hub image
        image: postgres
        # Provide the password for postgres
        env:
          POSTGRES_PASSWORD: postgres
        # Set health checks to wait until postgres has started
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
      - name: Copy the code
        uses: actions/checkout@v2
      - name: Set up Python 3.8
        uses: actions/setup-python@v2
        with:
          python-version: 3.8
      - name: Install dependencies
        run: |
          python3 setup.py install
      - name: Test with unittest
        run: |
          cd backend/py
          source run_all_tests.sh
        env:
          # The hostname used to communicate with the PostgreSQL service container
          POSTGRES_HOST: postgres
          # The default PostgreSQL port
          POSTGRES_PORT: 5432

Upvotes: 9

Views: 13486

Answers (2)

CarlV
CarlV

Reputation: 31

Since the job is running directly on a runner machine (not within a docker container), you need to connect to "localhost" instead of "postgres". It should work if you change POSTGRES_HOST: postgres to POSTGRES_HOST: localhost.

This is described in detail in the docs: https://docs.github.com/en/actions/using-containerized-services/creating-postgresql-service-containers

Upvotes: 3

Sergi Hernando
Sergi Hernando

Reputation: 139

Since I was having the same issue, I tried a different approach that worked for me.

In the first place, I run the job within a container:

jobs:
  build:
    container: gradle:jdk11

That won't make the psql command available so you need to add a run step to install it. The particular installation method may differ depending on the Docker image you choose:

jobs:
  build:
    container: gradle:jdk11
    ...
    steps:
      - run: |
          apt-get update
          apt-get install --yes --no-install-recommends postgresql-client

Please note you may have different steps above or below.

Now it's time to execute all these SQL you need. The most important thing here: database hostname is postgres which is the id of the service container.


jobs:
  build:
    container: gradle:jdk11
    ...
    steps:
      - run: |
          apt-get update
          apt-get install --yes --no-install-recommends postgresql-client
      - run: |
          psql -h postgres -U postgres -c 'CREATE DATABASE ...'
          psql -h postgres -U postgres -c 'CREATE ROLE ...'

Upvotes: 10

Related Questions