Modification : vm_hosted -> etc/sv/*/{,log/}run .
[lhc/ateliers.git] / etc / postgresql / bin / createuser
diff --git a/etc/postgresql/bin/createuser b/etc/postgresql/bin/createuser
new file mode 100755 (executable)
index 0000000..16e5e04
--- /dev/null
@@ -0,0 +1,25 @@
+#!/bin/sh -eux
+db="$1"
+owner="${2:-$db}"
+sudo -u postgres psql "$db" -a -f - <<-EOF
+       \set ON_ERROR_STOP on
+       DO LANGUAGE plpgsql \$\$
+       BEGIN
+               IF NOT EXISTS (
+                SELECT *
+                FROM pg_catalog.pg_user
+                WHERE usename = '$user'
+                LIMIT 1
+               ) THEN
+                       CREATE ROLE $user
+                        LOGIN
+                        NOCREATEDB
+                        NOCREATEROLE
+                        NOINHERIT
+                        NOSUPERUSER;
+               END IF;
+       END;
+       \$\$;
+       GRANT USAGE ON SCHEMA public TO $user;
+       GRANT CONNECT,TEMPORARY ON DATABASE $db TO $user;
+       EOF