diff --git a/docker-compose.yml b/docker-compose.yml index 8c199d7..6ddaf9b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,18 +1,18 @@ -version: "3" services: - database-flashcard: - image: postgres:alpine + database: + image: pgvector/pgvector:pg16 restart: always expose: - - 6032 + - 5432 ports: - - "6032:5432" + - "5432:5432" volumes: - - flashcarddata:/var/lib/postgresql/data + - dbdata:/var/lib/postgresql/data + - ./../flashcard_service/pg-init-scripts:/docker-entrypoint-initdb.d environment: - POSTGRES_USER=root - POSTGRES_PASSWORD=root - - POSTGRES_DB=flashcard_service + - POSTGRES_CREATE_DB_FLASHCARD_SERVICE=flashcard_service app-flashcard: build: context: ./../flashcard_service # necessary because otherwise docker-compose overrides the context path when merging multiple docker-compose.yml files @@ -22,9 +22,9 @@ services: - "6000:6000" - "6001:6001" depends_on: - - database-flashcard + - database environment: - SPRING_DATASOURCE_URL: jdbc:postgresql://database-flashcard:5432/flashcard_service + SPRING_DATASOURCE_URL: jdbc:postgresql://database:5432/flashcard_service SPRING_DATASOURCE_USERNAME: root SPRING_DATASOURCE_PASSWORD: root dapr-flashcard: @@ -47,7 +47,7 @@ services: expose: - "6379" volumes: - flashcarddata: + dbdata: testdata: networks: diff --git a/pg-init-scripts/create-multiple-databases.sh b/pg-init-scripts/create-multiple-databases.sh new file mode 100644 index 0000000..9cec938 --- /dev/null +++ b/pg-init-scripts/create-multiple-databases.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +# Script taken with modifications from https://dev.to/bgord/multiple-postgres-databases-in-a-single-docker-container-417l + +set -e +set -u + +function create_user_and_database() { + local database=$1 + echo " Creating user and database '$database'" + psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL + CREATE DATABASE "$database"; + GRANT ALL PRIVILEGES ON DATABASE "$database" TO "$POSTGRES_USER"; +EOSQL +} + +for var in $(env | grep '^POSTGRES_CREATE_DB_' | awk -F '=' '{print $2}'); do + echo "Creating database: $var" + create_user_and_database $var +done \ No newline at end of file