Following is the command I am using to create database in shell script
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" \
-c "CREATE DATABASE $database;"
It throws error
FATAL: database "POSTGRES_USER" does not exist
If I print $database and $POSTGRES_USER they show correct values but when this command runs with some reason the username is passed as the database name.
any clue what I might be missing?
======
function create_db() {
local database=$1
local query_databases="select datname from pg_database;"
local databases=$(echo "$query_databases" | psql -Aqt)
//this do not return any database but I have one db
database_exist=$(containsElement "$databases" "$database")
echo $database_exist;
if [[ "$database_exist" == 1 ]]; then
echo "Database '$database' exists. Skipping."
else
echo "Create Database '$database'"
psql -v ON_ERROR_STOP=1 --username=$POSTGRES_USER \
-c "CREATE DATABASE $database;"
fi
}
taking reference from - https://github.com/mrts/docker-postgresql-multiple-databases/pull/10/files
postgres,ortemplate1)postgresandtemplate1(almost) always exist)