Last active
January 22, 2025 02:30
-
-
Save amzar96/505dec13a5d0ba2a4cde6211aed0b7ec to your computer and use it in GitHub Desktop.
Function to grant all in Redshift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE PROCEDURE grant_all_permissions(target_user TEXT) | |
AS $$ | |
DECLARE | |
schema_record RECORD; | |
BEGIN | |
IF target_user IS NULL OR target_user = '' THEN | |
RAISE EXCEPTION 'username cannot be null or empty'; | |
END IF; | |
FOR schema_record IN | |
SELECT nspname | |
FROM pg_catalog.pg_namespace | |
WHERE nspname NOT LIKE 'pg_%' | |
AND nspname not in ('information_schema', 'catalog_history') | |
LOOP | |
EXECUTE 'GRANT USAGE ON SCHEMA ' || quote_ident(schema_record.nspname) || ' TO ' || quote_ident(target_user); | |
EXECUTE 'GRANT SELECT ON ALL TABLES IN SCHEMA ' || quote_ident(schema_record.nspname) || ' TO ' || quote_ident(target_user); | |
EXECUTE 'ALTER DEFAULT PRIVILEGES IN SCHEMA ' || quote_ident(schema_record.nspname) | |
|| ' GRANT SELECT ON TABLES TO ' || quote_ident(target_user); | |
END LOOP; | |
END; | |
$$ LANGUAGE plpgsql; | |
CALL grant_all_permissions('user_datahub'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment