April 3, 2019
Fill Gitlab-CI Postgres Database Service with SQL
We all love the Gitlab-CI and it’s docker support, also the services are a nice feature to get a database into your pipeline task.
But recently we had the issue that we need to prefill the given postgres:latest service with some sql data for testing.
For ubuntu based docker images, there is a easy solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
image: ubuntu:18.04 stages: - test test-task: stage: test services: - postgres:latest script: - apt update - apt install -y postgresql-client - env PGPASSWORD=yourPassword psql -h postgres -U postgres -w postgres < path/to/your/initial.sql variables: POSTGRES_DB: postgres POSTGRES_USER: postgres POSTGRES_PASSWORD: "yourPassword" |
For other base images based on debian or alpine, you mabye need apk add as an apt install equivalent to get the correct postgres-client.
Beware the variables for the postgres db service and the values in the psql call need to match.
For the psql reference see: https://www.postgresql.org/docs/current/app-psql.html