X-Git-Url: https://git.cyclocoop.org/?p=lhc%2Fateliers.git;a=blobdiff_plain;f=local%2Fpostgresql-database-create;h=6512a16eedc7cf28684a4bf4e864d5d8b948b04c;hp=d26b80db0c87990554fd0a8369ea1eab86e7febd;hb=b75d4503ef9c919231c0c02daf5a1ed1e57c73af;hpb=4c57b350e0265cfadb9bbf95cd55ae491ee66820 diff --git a/local/postgresql-database-create b/local/postgresql-database-create index d26b80d..6512a16 100755 --- a/local/postgresql-database-create +++ b/local/postgresql-database-create @@ -1,41 +1,22 @@ #!/bin/sh -eux db="$1" owner="${2:-$db}" -sudo -u postgres psql template1 -a -f - <<-EOF - \set ON_ERROR_STOP on - DO LANGUAGE plpgsql \$\$ - BEGIN - IF NOT EXISTS ( - SELECT * - FROM pg_catalog.pg_user - WHERE usename = '$owner' - LIMIT 1 - ) THEN - CREATE ROLE $owner - LOGIN - NOCREATEDB - NOCREATEROLE - NOINHERIT - NOSUPERUSER; - END IF; - END; - \$\$; - EOF case $(sudo -u postgres psql template1 -t -c \ - "SELECT datname FROM pg_catalog.pg_database WHERE datname = '$db' LIMIT 1") in + "SELECT datname + FROM pg_catalog.pg_database + WHERE datname = '$db' + LIMIT 1" + ) in (" $db") true;; (*) - sudo -u postgres psql template1 -a -f - <<-EOF - \set ON_ERROR_STOP on + sudo -u postgres psql template1 -a -f - --set ON_ERROR_STOP=1 <<-EOF CREATE DATABASE $db WITH OWNER=$owner; EOF ;; esac -sudo -u postgres psql template1 -a -f - <<-EOF - \set ON_ERROR_STOP on +sudo -u postgres psql template1 -a -f - --set ON_ERROR_STOP=1 <<-EOF REVOKE ALL ON DATABASE $db FROM public; EOF -sudo -u postgres psql "$db" -a -f - <<-EOF - \set ON_ERROR_STOP on +sudo -u postgres psql "$db" -a -f - --set ON_ERROR_STOP=1 <<-EOF GRANT ALL ON SCHEMA public TO $owner WITH GRANT OPTION; EOF