How to initialize GitLab docker postgres service with schema and data

How do you initialize the postgres service on GitLab Docker CI?

In a Dockerfile you copy .sql files to docker-entrypoint-initdb.d


FROM postgres:9.5.9

... (other stuff here)

COPY schemas.sql /docker-entrypoint-initdb.d/1.schemas.sql
COPY test-data.sql /docker-entrypoint-initdb.d/2.test-data.sql

WORKDIR /docker-entrypoint-initdb.d

Is there an equivalent in GitLab Docker CI? How can I initialize and setup my postgres database service so that I can have the correct database to test against in my different CI builds/stages.

image: node:latest

services: ["postgres:latest"]

# Needs correct schema to test against, but uses base clean postgres service instead
  stage: test
    - npm i
    - npm test

Did you figure out a solution for this? This is currently not possible afaik.

1 Like

Yes since you have access to that service at the time of the job you initialize in the script section of the job

Hello, but then you need to write specific code for the initialization or have psql installed in the build image.


@alallier how did you figure out how to initialize your db? I added postgres as a service, but when I try to find it using which psql it’s not available and my build fails.

What exactly did you do?

I know this is an old one, but for anyone out there still facing this problem, the workaround I used for this was to install the psql tool in a ‘before_script’ block and execute my init script there.

- yum update -y && yum install postgresql -y
- psql -h “postgres” -U “$POSTGRES_USER” -d “$POSTGRES_DB” -f ./docker/postgres/init.sql

The postgres variables are defined in a block “variables” in the beginning of my yaml, like:

POSTGRES_DB: nice_marmot