#!/bin/bash
set -e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" --port "${PGPORT:-5432}" <<-EOSQL
    CREATE DATABASE datastore;
    GRANT ALL PRIVILEGES ON DATABASE datastore TO "user";
EOSQL


if test -f "/docker-entrypoint-initdb.d/dumps/datastore.sql"; then
    psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname datastore --port "${PGPORT:-5432}" < /docker-entrypoint-initdb.d/dumps/datastore.sql
else
    echo "can not find datastore db dump to load"
fi
