Postgres service global variables

What am I doing wrong here with variables?

here’s my build file, which is about as simple as it gets:

image: python

default:
services:
- postgres:12.2-alpine

variables:
POSTGRES_HOST: postgres
POSTGRES_DB: $POSTGRES_DB
POSTGRES_USER: $POSTGRES_USER
POSTGRES_PASSWORD: $POSTGRES_PASSWORD
POSTGRES_HOST_AUTH_METHOD: trust

build:
script:
- echo “The job’s stage is ‘$CI_JOB_STAGE’”
- echo “POSTGRES ‘$POSTGRES_DB’”
- echo “POSTGRES ‘$POSTGRES_USER’”

Some output:

$ echo “The job’s stage is ‘$CI_JOB_STAGE’”

[29]The job’s stage is ‘test’

$ echo “POSTGRES ‘$POSTGRES_DB’”

POSTGRES ‘$POSTGRES_DB’

$ echo “POSTGRES ‘$POSTGRES_USER’”

POSTGRES ‘$POSTGRES_USER’

Seems like the environment variables are not picking up the postgres settings?

Hey,

Firstly, please use formatting when writing your configuration files. It’s more precise and easier to read.

Secondly:

I believe most of your questions will already be answered :slight_smile:

1 Like

Thanks for your help, , Paula!

this is my .gitlab-ci.yml file at the top of my project.

It’s basically a copy/paste from the postgres documentation. I am just trying to echo the variables right now since my actual database code isn’t able to use them, either.

image: python

default:
  services:
    - postgres:12.2-alpine

variables:
  POSTGRES_HOST: postgres
  POSTGRES_DB: $POSTGRES_DB
  POSTGRES_USER: $POSTGRES_USER
  POSTGRES_PASSWORD: $POSTGRES_PASSWORD
  POSTGRES_HOST_AUTH_METHOD: trust

build:
  script:
    - echo "The job's stage is '$CI_JOB_STAGE'"
    - echo "POSTGRES '$POSTGRES_DB'"
    - echo "POSTGRES '$POSTGRES_USER'"
    - echo "POSTGRES '$POSTGRES_PASSWORD'"

I get this output

e[0KRunning with gitlab-runner 16.1.0~beta.59.g83c66823 (83c66823)e[0;m
e[0K  on blue-3.saas-linux-small-amd64.runners-manager.gitlab.com/default zxwgkjAP, system ID: s_d5d3abbdfd0ae[0;m
e[0K  feature flags: FF_USE_IMPROVED_URL_MASKING:truee[0;m
section_start:1689167605:resolve_secrets
e[0Ke[0Ke[36;1mResolving secretse[0;me[0;m
section_end:1689167605:resolve_secrets
e[0Ksection_start:1689167605:prepare_executor
e[0Ke[0Ke[36;1mPreparing the "docker+machine" executore[0;me[0;m
e[0KUsing Docker executor with image python ...e[0;m
e[0KStarting service postgres:12.2-alpine ...e[0;m
e[0KPulling docker image postgres:12.2-alpine ...e[0;m
e[0KUsing docker image sha256:ae192c4d3adaebbbf2f023e1e50eaadfabccb6b08c855ac13d6ce2232381a58a for postgres:12.2-alpine with digest postgres@sha256:9ea72265275674225b1eaa2ae897dd244028af4ee7ef6e4e89fe474938e0992e ...e[0;m
e[0KWaiting for services to be up and running (timeout 30 seconds)...e[0;m
e[0KPulling docker image python ...e[0;m
e[0KUsing docker image sha256:c0e63845ae986c52da5cd6ac4d56eebf293439bb22a3cee198dd818fd12ba555 for python with digest python@sha256:b7a504dd0affeb20cf1ba1d3219f854c889c7ad557a2a5a4c4aba19cadd075f1 ...e[0;m
section_end:1689167629:prepare_executor
e[0Ksection_start:1689167629:prepare_script
e[0Ke[0Ke[36;1mPreparing environmente[0;me[0;m
Running on runner-zxwgkjap-project-47313164-concurrent-0 via runner-zxwgkjap-s-l-s-amd64-1689167569-6af69de3...
section_end:1689167634:prepare_script
e[0Ksection_start:1689167634:get_sources
e[0Ke[0Ke[36;1mGetting source from Git repositorye[0;me[0;m
e[32;1mFetching changes with git depth set to 20...e[0;m
Initialized empty Git repository in /builds/bedrock-energy/geo-data-tooling/.git/
e[32;1mCreated fresh repository.e[0;m
e[32;1mChecking out 701a915d as detached HEAD (ref is db)...e[0;m

e[32;1mSkipping Git submodules setupe[0;m
e[32;1m$ git remote set-url origin "${CI_REPOSITORY_URL}"e[0;m
section_end:1689167635:get_sources
e[0Ksection_start:1689167635:step_script
e[0Ke[0Ke[36;1mExecuting "step_script" stage of the job scripte[0;me[0;m
e[0KUsing docker image sha256:c0e63845ae986c52da5cd6ac4d56eebf293439bb22a3cee198dd818fd12ba555 for python with digest python@sha256:b7a504dd0affeb20cf1ba1d3219f854c889c7ad557a2a5a4c4aba19cadd075f1 ...e[0;m
e[32;1m$ echo "The job's stage is '$CI_JOB_STAGE'"e[0;m
The job's stage is 'test'
e[32;1m$ echo "POSTGRES '$POSTGRES_DB'"e[0;m
POSTGRES '$POSTGRES_DB'
e[32;1m$ echo "POSTGRES '$POSTGRES_USER'"e[0;m
POSTGRES '$POSTGRES_USER'
e[32;1m$ echo "POSTGRES '$POSTGRES_PASSWORD'"e[0;m
POSTGRES '$POSTGRES_PASSWORD'
section_end:1689167636:step_script
e[0Ksection_start:1689167636:cleanup_file_variables
e[0Ke[0Ke[36;1mCleaning up project directory and file based variablese[0;me[0;m
section_end:1689167636:cleanup_file_variables
e[0Ke[32;1mJob succeedede[0;m

To me, it looks like postgres service was successfully started.
The documentation looks like these environment variables should simply be exposed to my build process.

I’m not seeing this is some super secret content as an output, which seems to me like it’s just not working. Am I misunderstanding?

Hi,

So, those variables are probably not defined yet, they do not exist. You’re the boss, and the one to define $POSTGRES_DB, $POSTGRES_USER, etc… Your postgres won’t create them, it will just read from them. So, normally you would go to your project Settings > CI/CD > Variables. And here you will define a variable that is called POSTGRES_DB and fill in whatever database should be called, etc etc.

Secondly, you should use cat instead of echo to print variable content, as explained in the second URL I pasted above. Also, use it without single quotes. Like this, you are literally just printing $POSTGRES_DB. So, use it like:

cat $POSTGRES_DB

Hope this helps!

1 Like

Thanks Paula.

These are variables, so echo is the correct command.

For anyone who gets stuck: gitlab does not populate these variables for you. You must, as Paula said, populate them yourself in the Settings.
Make sure postgres user is ‘postgres’ for the standard postgres-alpine image.
I imagine the docker container is the one specifying, and the documentation is currently unclear that’s how it works!

1 Like