1
0
mirror of https://github.com/docker-library/postgres.git synced 2025-07-28 10:42:06 +03:00

Do not try to create databases that already exist

We were already checking for whether `POSTGRES_DB` was set to `postgres`, but this was the underlying motivation for that check (and it turns out that this applies for values of at least `template0` and `template1` as well).
This commit is contained in:
Tianon Gravi
2020-12-21 12:39:23 -08:00
parent 3690694930
commit 11e397d86c
13 changed files with 91 additions and 13 deletions

View File

@ -188,7 +188,13 @@ docker_process_sql() {
# create initial database
# uses environment variables for input: POSTGRES_DB
docker_setup_db() {
if [ "$POSTGRES_DB" != 'postgres' ]; then
local dbAlreadyExists
dbAlreadyExists="$(
POSTGRES_DB= docker_process_sql --dbname postgres --set db="$POSTGRES_DB" --tuples-only <<-'EOSQL'
SELECT 1 FROM pg_database WHERE datname = :'db' ;
EOSQL
)"
if [ -z "$dbAlreadyExists" ]; then
POSTGRES_DB= docker_process_sql --dbname postgres --set db="$POSTGRES_DB" <<-'EOSQL'
CREATE DATABASE :"db" ;
EOSQL