HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

How to grant the USAGE privilege on all types?

Submitted by: @import:stackexchange-dba··
0
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 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 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

\gexec


You can also run this in a do block with PERFORM

Code 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.