X-Git-Url: https://git.cyclocoop.org/?a=blobdiff_plain;f=etc%2Fpostgresql%2Fbin%2Fcreatedb;fp=etc%2Fpostgresql%2Fbin%2Fcreatedb;h=d26b80db0c87990554fd0a8369ea1eab86e7febd;hb=2e5bb0ff5df2b7a5d527a4f896b802df31d3c6bc;hp=0000000000000000000000000000000000000000;hpb=1b63122dd097c575896b4c4158ed5439eeca1563;p=lhc%2Fateliers.git diff --git a/etc/postgresql/bin/createdb b/etc/postgresql/bin/createdb new file mode 100755 index 0000000..d26b80d --- /dev/null +++ b/etc/postgresql/bin/createdb @@ -0,0 +1,41 @@ +#!/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 + (" $db") true;; + (*) + sudo -u postgres psql template1 -a -f - <<-EOF + \set ON_ERROR_STOP on + CREATE DATABASE $db WITH OWNER=$owner; + EOF + ;; + esac +sudo -u postgres psql template1 -a -f - <<-EOF + \set ON_ERROR_STOP on + REVOKE ALL ON DATABASE $db FROM public; + EOF +sudo -u postgres psql "$db" -a -f - <<-EOF + \set ON_ERROR_STOP on + GRANT ALL ON SCHEMA public TO $owner WITH GRANT OPTION; + EOF