How do I GRANT ALL PRIVILEGES on ALL VIEWS in one statement?
The only way to do this in a single statement (kind of) is to create a function, otherwise you are either specifically listing all views or you are granting to all tables, then revoking non-views.
I wrote this quickly, but tested it. You may need to tweak as needed:
CREATE OR REPLACE FUNCTION fn_grant_all_views(schema_name TEXT, role_name TEXT)RETURNS VOID AS $func$DECLARE view_name TEXT;BEGIN FOR view_name IN SELECT viewname FROM pg_views WHERE schemaname = schema_name LOOP EXECUTE 'GRANT ALL PRIVILEGES ON ' || schema_name || '.' || view_name || ' TO ' || role_name || ';'; END LOOP;END; $func$ LANGUAGE PLPGSQL
Usage:
SELECT fn_grant_all_views('my_schema','my_role');
You can also do this from the command line:
Login as postgres:
sudo -i -u postgres
Run this command:
psql -At -d [dbname] -c "SELECT 'GRANT ALL ON '||viewname||' TO [username];' FROM pg_views WHERE schemaname='public';" | psql -d [dbname]