首页 > 解决方案 > 如何在 Postgres 中授予以 parcel 开头的表的权限?

问题描述

GRANT UPDATE 
ON (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE 'parcel%') 
TO police;

标签: sqlpostgresqldatetimestamp

解决方案


GRANT只接受固定的表名(或表列表)。您想要的需要动态 SQL,即动态构建语句并在匿名块中执行它。

考虑:

do
$$
begin
    execute (
        select 
            'grant all on table '
            || string_agg (format('%I.%I', table_schema, table_name), ', ')
            || ' to police'
        from information_schema.tables
        where table_name like 'parcel%'
   )    ;
end
$$;

推荐阅读