What am I doing wrong in below code, as I am still getting error:
ERROR: database "postgres" already exists
ERROR: Job failed: exit code 1
App is using a PostgreSQL and I would like to run it, and then run the tests.
stages:
- pre-commit
- test
pre-commit:
stage: pre-commit
image: python:latest
before_script:
- cd logbook
- python --version
- pip install poetry
- poetry install
- poetry update
script:
- poetry run black --diff --check -q . || true
- poetry run flake8 . || true
- poetry run pylint src tests --recursive y || true
- poetry run mypy . --explicit-package-bases || true
only:
- merge_requests
- push
sast:
stage: test
include:
- template: Security/SAST.gitlab-ci.yml
logbook_test_job:
stage: test
image: python:latest
services:
- postgres:latest
variables:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: mysecretpassword
POSTGRES_HOST: postgres
POSTGRES_PORT: "5432"
POSTGRES_DB: logbook
before_script:
- apt-get update -y
- apt-get install -y postgresql-client
- export PGPASSWORD=$POSTGRES_PASSWORD
# - psql -h postgres -U $POSTGRES_USER -d postgres -c "DROP DATABASE IF EXISTS $POSTGRES_DB;"
- psql -h postgres -U $POSTGRES_USER -d $POSTGRES_DB -c "CREATE DATABASE $POSTGRES_DB;"
- cd logbook
- python --version
- pip install poetry
- poetry install
- poetry update
script:
- poetry run alembic upgrade head
- poetry run pytest tests
2
Answers
I’m guessing this error wasn’t caused by the exact version of the configuration that you’re showing.
This would fail on an attempt to tell
psql
to connect to a db namedlogbook
, and then create that db from inside it. Iflogbook
doesn’t yet exist, the connection will fail. If it already exists, theCREATE
statement will fail due to a conflicting name. You need both psql calls to target-d postgres
instead of-d $POSTGRES_DB
.You might also want to uncomment the
DROP
statement before the attempt toCREATE
the db. Otherwise, restarting this will result in a conflict.When you define
POSTGRES_DB
, the database (logbook
in this case) is created for you already.From the image documentation for
POSTGRES_DB
:Therefore, this line (which is problematic for other reasons) is not needed and can be removed:
This line doesn’t make any sense anyhow because trying to connect with
-d $POSTGRES_DB
requires that the database already exist — therefore if the connection succeeds, this command will always fail.You generally don’t need to do anything with
psql
unless you need to load some mock data, create additional users, or something like that.But, in the offhand case that you do need to run commands to create additional databases, users, or something like that, you can connect to the builtin
template1
db which will always exist:Though, you should probably prefer use of commands like
createdb
orcreateuser
instead.