【问题标题】:troubles querying information schema查询信息模式的麻烦
【发布时间】:2018-07-25 04:25:30
【问题描述】:

postgresql 服务器 8.4

对于具有“超级用户”属性的用户,我可以执行此查询:

SELECT
        ccu.table_name AS master_table, ccu.column_name AS master_column,
        tc.table_name AS child_table, kcu.column_name AS child_column
FROM 
        information_schema.table_constraints AS tc 
        JOIN information_schema.key_column_usage AS kcu
          ON tc.constraint_name = kcu.constraint_name
        JOIN information_schema.constraint_column_usage AS ccu
          ON ccu.constraint_name = tc.constraint_name
WHERE constraint_type = 'FOREIGN KEY'
ORDER BY master_table, master_column

对于普通用户,我没有错误但也没有结果。 允许普通用户查询信息架构的最小权限是什么……授予……?

我没有成功

在 SCHEMA information_schema 上向用户授予使用权限

还有

向用户授予对 information_schema.constraint_column_usage 的选择

(和其他两个使用)

【问题讨论】:

    标签: postgresql information-schema


    【解决方案1】:

    您只会看到您拥有某些权限的对象:

    • 您看不到其他用户的临时对象。

    • 您可以查看所有者是您所属角色的对象。

    • 如果您对表或其列具有任何权限,则可以查看对象。

    要绕过这些限制,您可以使用SECURITY DEFINER 创建一个属于超级用户的函数并为您运行查询。

    然后从PUBLIC 撤消该功能上的EXECUTE 并将其授予需要它的用户。

    CREATE FUNCTION info_schema_query()
       RETURNS TABLE (
          master_table  information_schema.sql_identifier,
          master_column information_schema.sql_identifier,
          child_table   information_schema.sql_identifier,
          child_column  information_schema.sql_identifier
       )
       LANGUAGE sql STABLE SECURITY DEFINER
       SET search_path = information_schema
    AS $$SELECT ...$$;
    
    REVOKE EXECUTE ON FUNCTION info_schema_query() FROM PUBLIC;
    GRANT  EXECUTE ON FUNCTION info_schema_query() TO j_random_user;
    

    【讨论】:

    • 我知道......但我没有看到任何解决方案。
    • 我已经扩展了答案 - 也许这对你来说是一个解决方案。
    • 劳伦兹,感谢您的努力。我不明白:这是只向普通用户授予该信息的读取权限的永久解决方案吗?我必须创建的“带有 SECURITY DEFINER 的功能”是什么?
    • 我添加了一个示例,这样您就不必查看文档(尽管您仍然应该这样做)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    • 2018-11-04
    相关资源
    最近更新 更多