Skip to content

Instantly share code, notes, and snippets.

@jms
Created July 13, 2017 15:31
Show Gist options
  • Select an option

  • Save jms/a6cf657c35c314a717ce7040a97071c3 to your computer and use it in GitHub Desktop.

Select an option

Save jms/a6cf657c35c314a717ce7040a97071c3 to your computer and use it in GitHub Desktop.
validate if a role name exists on postgresql , then execute statement
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_catalago.pg_roles WHERE rolname = 'reader') THEN
CREATE ROLE reader;
END IF;
END
$$;
GRANT SELECT ON tasks TO reader;
--
DO $$
BEGIN
IF EXISTS(SELECT count(*) FROM pg_catalago.pg_roles WHERE rolename = 'adm') THEN
GRANT SELECT ON testview TO adm;
END IF;
END
$$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment