Skip to content

Instantly share code, notes, and snippets.

@rocksfrow
Last active August 29, 2015 14:04
Show Gist options
  • Save rocksfrow/af751f120c7e2c713ca5 to your computer and use it in GitHub Desktop.
Save rocksfrow/af751f120c7e2c713ca5 to your computer and use it in GitHub Desktop.
plPgsql function to output grant SQL to give user access to entire schema
create or replace function fetch_schema_grants(_user text, _schema text) RETURNS TABLE(query text) as
$func$
begin
RETURN QUERY select 'grant all on schema '||_schema||' to '||_user||';' union all select 'grant all on sequence '||sequence_schema||'.'||sequence_name||' to '||_user||';' from information_schema.sequences where sequence_schema=_schema union all select 'grant all on '||schemaname||'.'||tablename||' to '||_user||';' from pg_tables where schemaname=_schema;
end
$func$ language plpgsql;
@rocksfrow
Copy link
Author

ex)

db1=> select * from fetch_schema_grants('roundcube','roundcube');
                              query                              
-----------------------------------------------------------------
 grant all on schema roundcube to roundcube;
 grant all on sequence roundcube.cache_ids to roundcube;
 grant all on sequence roundcube.contact_ids to roundcube;
 grant all on sequence roundcube.contactgroups_ids to roundcube;
 grant all on sequence roundcube.identity_ids to roundcube;
 grant all on sequence roundcube.search_ids to roundcube;
 grant all on sequence roundcube.user_ids to roundcube;
 grant all on roundcube.cache to roundcube;
 grant all on roundcube.cache_index to roundcube;
 grant all on roundcube.cache_messages to roundcube;
 grant all on roundcube.cache_thread to roundcube;
 grant all on roundcube.contactgroupmembers to roundcube;
 grant all on roundcube.contactgroups to roundcube;
 grant all on roundcube.contacts to roundcube;
 grant all on roundcube.dictionary to roundcube;
 grant all on roundcube.identities to roundcube;
 grant all on roundcube.identities_bkup to roundcube;
 grant all on roundcube.searches to roundcube;
 grant all on roundcube.session to roundcube;
 grant all on roundcube.users to roundcube;
(20 rows)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment