DEV Community

Cover image for TIL Using Postgres in GitHub Actions

Posted on


TIL Using Postgres in GitHub Actions

I recently made my first experiences with GitHub Actions.

Setting up Postgres took me several attempts because of a misunderstanding with variables in the service's env block.

I thought that I could just define POSTGRES_PASSWORD in the global env block and this would then be available and used in Postgres' service. However, not having set POSTGRES_PASSWORD in services.postgres.env gave me this nice, quite unhelpful error message:

##[error]Failed to initialize, postgres service is unhealthy.
Enter fullscreen mode Exit fullscreen mode

After a while I learned that the environment variable POSTGRES_PASSWORD in services.postgres.env is mandatory and must not be blank.

Also, the variables defined in services.postgres.env are passed into the docker image, thus they are somewhat different compared to regular env blocks in Github Action Workflow syntax... I guess?

However, my final result looks like this.

  # ...
  POSTGRES_PASSWORD: mysecretpassword

    # ...
        image: postgres:latest
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
          - 5432:5432
    # ...
Enter fullscreen mode Exit fullscreen mode

Which is being translated into this command, which makes it clear what happens with the variables.

docker create\
  # many others\
  -p 5432:5432\
  --health-cmd pg_isready\
  --health-interval 10s\
  --health-timeout 5s\
  --health-retries 5\
  -e "POSTGRES_PASSWORD=mysecretpassword"\    # <= 💡
  -e CI=true\
Enter fullscreen mode Exit fullscreen mode

My learning may be trivial, but I hope that this helps someone having the same issue.

(Photo by Andrew Rice)

Top comments (1)

cescquintero profile image
Francisco Quintero 🇨🇴

Thanks, very useful the part of not leaving empty the POSTGRES_PASSWORD.

An Animated Guide to Node.js Event Loop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.