snippetsqlMinor
How to grant the USAGE privilege on all types?
Viewed 0 times
typestheprivilegeallgrantusagehow
Problem
How can I grant the USAGE privilege on ALL TYPES in a schema? For tables and functions we can use
I know I can set a default privilege, but in this case I'd already installed an extension with many types before doing that.
ALL TABLES IN SCHEMA schema_name, but this isn't supported for types:GRANT { USAGE | ALL [ PRIVILEGES ] }
ON TYPE type_name [, ...]
TO role_specification [, ...] [ WITH GRANT OPTION ]I know I can set a default privilege, but in this case I'd already installed an extension with many types before doing that.
Solution
We can do this with dynamic sql and psql, Sub out
Then run
You can also run this in a
user1 and user2 with the name of the user. Feel free to delete either. You may also want only DOMAIN or USER-DEFINED TYPE adapting this should be pretty simple.SELECT format(
$GRANT USAGE ON TYPE %s TO %s;$,
format('%I.%I.%I', object_catalog, object_schema, object_name ),
grantUser
)
FROM information_schema.element_types
CROSS JOIN (VALUES ('user1'),('user2')) AS t(grantUser)
WHERE object_type IN ('USER-DEFINED TYPE', 'DOMAIN');Then run
\gexecYou can also run this in a
do block with PERFORMCode Snippets
SELECT format(
$$GRANT USAGE ON TYPE %s TO %s;$$,
format('%I.%I.%I', object_catalog, object_schema, object_name ),
grantUser
)
FROM information_schema.element_types
CROSS JOIN (VALUES ('user1'),('user2')) AS t(grantUser)
WHERE object_type IN ('USER-DEFINED TYPE', 'DOMAIN');Context
StackExchange Database Administrators Q#175657, answer score: 3
Revisions (0)
No revisions yet.