#!/bin/bash

set -e

SUBJECT=$1
PGPASSWORD=$2

PASSWORD=$(openssl rand -hex 20)
ID=$(openssl rand -hex 5)

kubectl run "postgres-client-$ID" --rm -i --image "bitnami/postgresql" -n postgres --env="PGPASSWORD=$PGPASSWORD" --command -- psql --host postgres-postgresql -U postgres <<SQL
CREATE DATABASE $SUBJECT;
CREATE USER $SUBJECT with encrypted password '$PASSWORD';
grant all privileges on database $SUBJECT to $SUBJECT;
SQL

echo "$PASSWORD"