#!/bin/bash
set -e

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


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