Last active
January 10, 2018 22:22
-
-
Save justinlewis/5247914 to your computer and use it in GitHub Desktop.
A simple function and some commands to grant privileges to every table in a PostgreSQL database. * each file is a different method. You don't need need to use them all in conjunction.
This file contains 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
Use the commands below to set privileges using the PSQL terminal commands. | |
TABLES: | |
for tbl in `psql -qAt -c "select tablename from pg_tables where schemaname = 'public';" YOUR_DB` ; do psql -c "alter table $tbl owner to NEW_OWNER" YOUR_DB ; done | |
SEQUENCES: | |
for tbl in `psql -qAt -c "select sequence_name from information_schema.sequences where sequence_schema = 'public';" YOUR_DB` ; do psql -c "alter table $tbl owner to NEW_OWNER" YOUR_DB ; done | |
VIEWS: | |
for tbl in `psql -qAt -c "select table_name from information_schema.views where table_schema = 'public';" YOUR_DB` ; do psql -c "alter table $tbl owner to NEW_OWNER" YOUR_DB ; done |
This file contains 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
-- Function: user_management(text) | |
-- DROP FUNCTION user_management(text); | |
CREATE OR REPLACE FUNCTION user_management(text) | |
RETURNS void AS | |
'BEGIN execute $1; END;' | |
LANGUAGE plpgsql VOLATILE | |
COST 100; | |
ALTER FUNCTION user_management(text) | |
OWNER TO postgres; | |
COMMENT ON FUNCTION user_management(text) IS 'run this command to grant SELECT on all tables in the db. | |
select user_management('grant select on ' || tablename || ' to reader;') from pg_tables where schemaname = 'public';'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment