Skip to content

Commit 03e7695

Browse files
authored
Merge pull request #801 from infosiftr/db-exists
Do not try to create databases that already exist
2 parents eaeb73d + 11e397d commit 03e7695

13 files changed

+91
-13
lines changed

10/alpine/docker-entrypoint.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,13 @@ docker_process_sql() {
189189
# create initial database
190190
# uses environment variables for input: POSTGRES_DB
191191
docker_setup_db() {
192-
if [ "$POSTGRES_DB" != 'postgres' ]; then
192+
local dbAlreadyExists
193+
dbAlreadyExists="$(
194+
POSTGRES_DB= docker_process_sql --dbname postgres --set db="$POSTGRES_DB" --tuples-only <<-'EOSQL'
195+
SELECT 1 FROM pg_database WHERE datname = :'db' ;
196+
EOSQL
197+
)"
198+
if [ -z "$dbAlreadyExists" ]; then
193199
POSTGRES_DB= docker_process_sql --dbname postgres --set db="$POSTGRES_DB" <<-'EOSQL'
194200
CREATE DATABASE :"db" ;
195201
EOSQL

10/docker-entrypoint.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,13 @@ docker_process_sql() {
189189
# create initial database
190190
# uses environment variables for input: POSTGRES_DB
191191
docker_setup_db() {
192-
if [ "$POSTGRES_DB" != 'postgres' ]; then
192+
local dbAlreadyExists
193+
dbAlreadyExists="$(
194+
POSTGRES_DB= docker_process_sql --dbname postgres --set db="$POSTGRES_DB" --tuples-only <<-'EOSQL'
195+
SELECT 1 FROM pg_database WHERE datname = :'db' ;
196+
EOSQL
197+
)"
198+
if [ -z "$dbAlreadyExists" ]; then
193199
POSTGRES_DB= docker_process_sql --dbname postgres --set db="$POSTGRES_DB" <<-'EOSQL'
194200
CREATE DATABASE :"db" ;
195201
EOSQL

11/alpine/docker-entrypoint.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,13 @@ docker_process_sql() {
189189
# create initial database
190190
# uses environment variables for input: POSTGRES_DB
191191
docker_setup_db() {
192-
if [ "$POSTGRES_DB" != 'postgres' ]; then
192+
local dbAlreadyExists
193+
dbAlreadyExists="$(
194+
POSTGRES_DB= docker_process_sql --dbname postgres --set db="$POSTGRES_DB" --tuples-only <<-'EOSQL'
195+
SELECT 1 FROM pg_database WHERE datname = :'db' ;
196+
EOSQL
197+
)"
198+
if [ -z "$dbAlreadyExists" ]; then
193199
POSTGRES_DB= docker_process_sql --dbname postgres --set db="$POSTGRES_DB" <<-'EOSQL'
194200
CREATE DATABASE :"db" ;
195201
EOSQL

11/docker-entrypoint.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,13 @@ docker_process_sql() {
189189
# create initial database
190190
# uses environment variables for input: POSTGRES_DB
191191
docker_setup_db() {
192-
if [ "$POSTGRES_DB" != 'postgres' ]; then
192+
local dbAlreadyExists
193+
dbAlreadyExists="$(
194+
POSTGRES_DB= docker_process_sql --dbname postgres --set db="$POSTGRES_DB" --tuples-only <<-'EOSQL'
195+
SELECT 1 FROM pg_database WHERE datname = :'db' ;
196+
EOSQL
197+
)"
198+
if [ -z "$dbAlreadyExists" ]; then
193199
POSTGRES_DB= docker_process_sql --dbname postgres --set db="$POSTGRES_DB" <<-'EOSQL'
194200
CREATE DATABASE :"db" ;
195201
EOSQL

12/alpine/docker-entrypoint.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,13 @@ docker_process_sql() {
189189
# create initial database
190190
# uses environment variables for input: POSTGRES_DB
191191
docker_setup_db() {
192-
if [ "$POSTGRES_DB" != 'postgres' ]; then
192+
local dbAlreadyExists
193+
dbAlreadyExists="$(
194+
POSTGRES_DB= docker_process_sql --dbname postgres --set db="$POSTGRES_DB" --tuples-only <<-'EOSQL'
195+
SELECT 1 FROM pg_database WHERE datname = :'db' ;
196+
EOSQL
197+
)"
198+
if [ -z "$dbAlreadyExists" ]; then
193199
POSTGRES_DB= docker_process_sql --dbname postgres --set db="$POSTGRES_DB" <<-'EOSQL'
194200
CREATE DATABASE :"db" ;
195201
EOSQL

12/docker-entrypoint.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,13 @@ docker_process_sql() {
189189
# create initial database
190190
# uses environment variables for input: POSTGRES_DB
191191
docker_setup_db() {
192-
if [ "$POSTGRES_DB" != 'postgres' ]; then
192+
local dbAlreadyExists
193+
dbAlreadyExists="$(
194+
POSTGRES_DB= docker_process_sql --dbname postgres --set db="$POSTGRES_DB" --tuples-only <<-'EOSQL'
195+
SELECT 1 FROM pg_database WHERE datname = :'db' ;
196+
EOSQL
197+
)"
198+
if [ -z "$dbAlreadyExists" ]; then
193199
POSTGRES_DB= docker_process_sql --dbname postgres --set db="$POSTGRES_DB" <<-'EOSQL'
194200
CREATE DATABASE :"db" ;
195201
EOSQL

13/alpine/docker-entrypoint.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,13 @@ docker_process_sql() {
189189
# create initial database
190190
# uses environment variables for input: POSTGRES_DB
191191
docker_setup_db() {
192-
if [ "$POSTGRES_DB" != 'postgres' ]; then
192+
local dbAlreadyExists
193+
dbAlreadyExists="$(
194+
POSTGRES_DB= docker_process_sql --dbname postgres --set db="$POSTGRES_DB" --tuples-only <<-'EOSQL'
195+
SELECT 1 FROM pg_database WHERE datname = :'db' ;
196+
EOSQL
197+
)"
198+
if [ -z "$dbAlreadyExists" ]; then
193199
POSTGRES_DB= docker_process_sql --dbname postgres --set db="$POSTGRES_DB" <<-'EOSQL'
194200
CREATE DATABASE :"db" ;
195201
EOSQL

13/docker-entrypoint.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,13 @@ docker_process_sql() {
189189
# create initial database
190190
# uses environment variables for input: POSTGRES_DB
191191
docker_setup_db() {
192-
if [ "$POSTGRES_DB" != 'postgres' ]; then
192+
local dbAlreadyExists
193+
dbAlreadyExists="$(
194+
POSTGRES_DB= docker_process_sql --dbname postgres --set db="$POSTGRES_DB" --tuples-only <<-'EOSQL'
195+
SELECT 1 FROM pg_database WHERE datname = :'db' ;
196+
EOSQL
197+
)"
198+
if [ -z "$dbAlreadyExists" ]; then
193199
POSTGRES_DB= docker_process_sql --dbname postgres --set db="$POSTGRES_DB" <<-'EOSQL'
194200
CREATE DATABASE :"db" ;
195201
EOSQL

9.5/alpine/docker-entrypoint.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,13 @@ docker_process_sql() {
189189
# create initial database
190190
# uses environment variables for input: POSTGRES_DB
191191
docker_setup_db() {
192-
if [ "$POSTGRES_DB" != 'postgres' ]; then
192+
local dbAlreadyExists
193+
dbAlreadyExists="$(
194+
POSTGRES_DB= docker_process_sql --dbname postgres --set db="$POSTGRES_DB" --tuples-only <<-'EOSQL'
195+
SELECT 1 FROM pg_database WHERE datname = :'db' ;
196+
EOSQL
197+
)"
198+
if [ -z "$dbAlreadyExists" ]; then
193199
POSTGRES_DB= docker_process_sql --dbname postgres --set db="$POSTGRES_DB" <<-'EOSQL'
194200
CREATE DATABASE :"db" ;
195201
EOSQL

9.5/docker-entrypoint.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,13 @@ docker_process_sql() {
189189
# create initial database
190190
# uses environment variables for input: POSTGRES_DB
191191
docker_setup_db() {
192-
if [ "$POSTGRES_DB" != 'postgres' ]; then
192+
local dbAlreadyExists
193+
dbAlreadyExists="$(
194+
POSTGRES_DB= docker_process_sql --dbname postgres --set db="$POSTGRES_DB" --tuples-only <<-'EOSQL'
195+
SELECT 1 FROM pg_database WHERE datname = :'db' ;
196+
EOSQL
197+
)"
198+
if [ -z "$dbAlreadyExists" ]; then
193199
POSTGRES_DB= docker_process_sql --dbname postgres --set db="$POSTGRES_DB" <<-'EOSQL'
194200
CREATE DATABASE :"db" ;
195201
EOSQL

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy