skip to Main Content

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


  1. I’m guessing this error wasn’t caused by the exact version of the configuration that you’re showing.

    # - 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;"
    

    This would fail on an attempt to tell psql to connect to a db named logbook, and then create that db from inside it. If logbook doesn’t yet exist, the connection will fail. If it already exists, the CREATE 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 to CREATE the db. Otherwise, restarting this will result in a conflict.

    Login or Signup to reply.
  2. When you define POSTGRES_DB, the database (logbook in this case) is created for you already.

    From the image documentation for POSTGRES_DB:

    This optional environment variable can be used to define a different name for the default database that is created when the image is first started. If it is not specified, then the value of POSTGRES_USER will be used.

    Therefore, this line (which is problematic for other reasons) is not needed and can be removed:

        - psql -h postgres -U $POSTGRES_USER -d $POSTGRES_DB -c "CREATE DATABASE $POSTGRES_DB;"
    

    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:

        # ...
        - extra_db_name="foobar"
        - psql -h postgres -U $POSTGRES_USER -d template1 -c "CREATE DATABASE $extra_db_name;"
    

    Though, you should probably prefer use of commands like createdb or createuser instead.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search