homelab/roles/postgresql/files/create-user.sql

10 lines
384 B
PL/PgSQL

CREATE OR REPLACE FUNCTION create_user_if_not_exists(username TEXT, passwd TEXT)
RETURNS VOID AS \$\$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = username) THEN
EXECUTE format('CREATE ROLE %I LOGIN PASSWORD %L', username, passwd);
ELSE
EXECUTE format('ALTER ROLE %I WITH PASSWORD %L', username, passwd);
END IF;
END;
\$\$ LANGUAGE plpgsql;