查询信息架构的麻烦

时间:2018-02-14 14:43:25

标签: postgresql information-schema

  

postgresql server 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的使用权授予用户

以及

  

将info_schema.constraint_column_usage的select选择授予用户

     

(和其他两个使用)

1 个答案:

答案 0 :(得分:1)

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

  • 您无法看到其他用户'临时对象。

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

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

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

然后从EXECUTE撤消该函数的PUBLIC,并将其授予需要它的用户。

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;