From 2ea481e2817c7d617e37571e2a6c0f11b231155e Mon Sep 17 00:00:00 2001 From: Chloe Jung Date: Tue, 31 Jan 2023 16:50:55 +1300 Subject: [PATCH] Separate out the "DROP" sql code. In docker, it results an error "cannot drop the currently open database". --- create_db.sql | 6 ------ create_db_init.sql | 2 ++ create_db_user.sql | 2 ++ s_create_db | 2 ++ select_db.sql | 1 + 5 files changed, 7 insertions(+), 6 deletions(-) create mode 100644 create_db_init.sql create mode 100644 create_db_user.sql create mode 100644 select_db.sql diff --git a/create_db.sql b/create_db.sql index 7bc9842..f50285b 100644 --- a/create_db.sql +++ b/create_db.sql @@ -1,9 +1,3 @@ -DROP DATABASE IF EXISTS card_db; -CREATE DATABASE card_db; - -DROP USER IF EXISTS cardapp; -CREATE USER cardapp WITH PASSWORD 'database_password'; - \c card_db; CREATE TABLE settings ( diff --git a/create_db_init.sql b/create_db_init.sql new file mode 100644 index 0000000..2a65afb --- /dev/null +++ b/create_db_init.sql @@ -0,0 +1,2 @@ +DROP DATABASE IF EXISTS card_db; +CREATE DATABASE card_db; diff --git a/create_db_user.sql b/create_db_user.sql new file mode 100644 index 0000000..678c8b1 --- /dev/null +++ b/create_db_user.sql @@ -0,0 +1,2 @@ +DROP USER cardapp; +CREATE USER cardapp WITH PASSWORD 'database_password'; \ No newline at end of file diff --git a/s_create_db b/s_create_db index bf7f18c..f5d85f6 100755 --- a/s_create_db +++ b/s_create_db @@ -12,7 +12,9 @@ echo Continue? "(y or n)" read x if [ "$x" = "y" ]; then + psql postgres -f create_db_init.sql psql postgres -f create_db.sql + psql postgres -f create_db_user.sql psql postgres -f settings.sql echo Database created else diff --git a/select_db.sql b/select_db.sql new file mode 100644 index 0000000..340c842 --- /dev/null +++ b/select_db.sql @@ -0,0 +1 @@ +SELECT 'CREATE DATABASE card_db' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'card_db'); \ No newline at end of file