Forem

Cover image for TIL Using Postgres in GitHub Actions
Mario
Mario

Posted on

3

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.

env:
  # ...
  POSTGRES_PASSWORD: mysecretpassword

jobs:
  a-job-that-uses-postgres:
    # ...
    services:
      postgres:
        image: postgres:latest
        env:
          POSTGRES_PASSWORD: ${{ env.POSTGRES_PASSWORD }}
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 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 GITHUB_ACTIONS=true\
  -e CI=true\
  postgres:latest
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)

Image of Timescale

πŸš€ pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applicationsβ€”without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post β†’

Top comments (1)

Collapse
 
cescquintero profile image
Francisco Quintero πŸ‡¨πŸ‡΄ β€’

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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay