diff options
author | Igor Pashev <pashev.igor@gmail.com> | 2016-09-23 12:41:01 +0300 |
---|---|---|
committer | Igor Pashev <pashev.igor@gmail.com> | 2016-09-23 12:41:49 +0300 |
commit | af337a12e6f084556400fa93c71304ad63f1efa6 (patch) | |
tree | ad5125cbfb2e812f4a507b182b875526b2a2d0e9 /apps/postgresql/functions.pgsql | |
download | nixsap-af337a12e6f084556400fa93c71304ad63f1efa6.tar.gz |
Initial commit
Diffstat (limited to 'apps/postgresql/functions.pgsql')
-rw-r--r-- | apps/postgresql/functions.pgsql | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/apps/postgresql/functions.pgsql b/apps/postgresql/functions.pgsql new file mode 100644 index 0000000..085cc5d --- /dev/null +++ b/apps/postgresql/functions.pgsql @@ -0,0 +1,25 @@ +CREATE EXTENSION IF NOT EXISTS dblink; + +DROP FUNCTION IF EXISTS create_role_if_not_exists(TEXT); +CREATE FUNCTION create_role_if_not_exists(IN name TEXT) +RETURNS VOID AS $$ +BEGIN +IF NOT EXISTS (SELECT 1 FROM pg_catalog.pg_roles WHERE rolname = name) THEN + EXECUTE format('CREATE ROLE %I', name); +END IF; +END; +$$ LANGUAGE PLPGSQL; + +DROP FUNCTION IF EXISTS create_db_if_not_exists(TEXT); +CREATE FUNCTION create_db_if_not_exists(IN dbname TEXT) +RETURNS VOID AS $$ +DECLARE port INT; +DECLARE junk TEXT; +BEGIN +IF NOT EXISTS (SELECT 1 FROM pg_catalog.pg_database WHERE datname = dbname) THEN + SELECT setting FROM pg_settings WHERE name = 'port' INTO port; + SELECT dblink_exec('user=postgres dbname=postgres port=' || port, 'CREATE DATABASE ' || quote_ident(dbname)) INTO junk; +END IF; +END; +$$ LANGUAGE PLPGSQL; + |