# Create the folder in docker compose file locationmkdir<location_of_docker_compose_file>/db# Create the scripttouch<location_of_docker_compose_file>/db/01-init.sh
#!/bin/bashset-eexport PGPASSWORD=$POSTGRES_PASSWORD;psql-vON_ERROR_STOP=1--username"$POSTGRES_USER"--dbname"$POSTGRES_DB"<<-EOSQL CREATE USER $APP_DB_USER WITH PASSWORD '$APP_DB_PASS'; CREATE DATABASE $APP_DB_NAME; GRANT ALL PRIVILEGES ON DATABASE $APP_DB_NAME TO $APP_DB_USER; \connect $APP_DB_NAME $APP_DB_USER BEGIN; CREATE TABLE IF NOT EXISTS event ( id CHAR(26) NOT NULL CHECK (CHAR_LENGTH(id) = 26) PRIMARY KEY, aggregate_id CHAR(26) NOT NULL CHECK (CHAR_LENGTH(aggregate_id) = 26), event_data JSON NOT NULL, version INT, UNIQUE(aggregate_id, version) ); CREATE INDEX idx_event_aggregate_id ON event (aggregate_id); COMMIT;EOSQL
The script will:
create a new user and password assigned in the variables in the composed file
create the App database
grant the priviilages for the user on the database
connect to the database cand create a table called event
Only thing left is to run the docker composed script:
docker-composeup-d
Note:
This should run the script automatically but if it doesn't run it with bash 01-init.sh
This should now be reachable via DBeaver or pgAdmin