That's my variation, adapted from the other answers, for giving all privileges:
create or replace function grant_all_privileges_on_database_to_role(
role_name text,
database_name text
)
returns void
language plpgsql as
$function$
declare
row record;
begin
execute format('grant all on database %1$s to %2$s', database_name, role_name);
for row in (
select
nspname as name
from
pg_namespace
where
nspname not in (
'information_schema',
'pg_catalog',
'pg_toast'
)
) loop
execute format('grant all on schema %1$s to %2$s', row.name, role_name);
execute format('grant all on all tables in schema %1$s to %2$s', row.name, role_name);
execute format('grant all on all sequences in schema %1$s to %2$s', row.name, role_name);
execute format('grant all on all functions in schema %1$s to %2$s', row.name, role_name);
execute format('grant all on all procedures in schema %1$s to %2$s', row.name, role_name);
execute format('grant all on all routines in schema %1$s to %2$s', row.name, role_name);
end loop;
end;
$function$;
-- select grant_all_privileges_on_database_to_role('<my_user>', '<my_database>');
or, for a schema:
create or replace function grant_all_privileges_on_schema_to_role(
role_name text,
schema_name text
)
returns void
language plpgsql
as $function$
begin
execute format('grant all on schema %1$s to %2$s', schema_name, role_name);
execute format('grant all on all tables in schema %1$s to %2$s', schema_name, role_name);
execute format('grant all on all sequences in schema %1$s to %2$s', schema_name, role_name);
execute format('grant all on all functions in schema %1$s to %2$s', schema_name, role_name);
execute format('grant all on all procedures in schema %1$s to %2$s', schema_name, role_name);
execute format('grant all on all routines in schema %1$s to %2$s', schema_name, role_name);
end;
$function$;
-- select grant_all_privileges_on_schema_to_role('<my_user>', '<my_schema>');