postgresql - 列出 PostgreSQL 中具有不同所有者的所有表的约束

标签 postgresql postgresql-9.1

我必须是关系的所有者才能访问信息模式中与约束相关的数据吗?我已经测试了以下内容,看来我必须是所有者。

create schema rights_test;

create table rights_test.t1 (id int primary key);
create table rights_test.t2 (id int references rights_test.t1(id));

select  
        tc.constraint_name, 
        tc.constraint_schema || '.' || tc.table_name || '.' || kcu.column_name as physical_full_name,  
        tc.constraint_schema,
        tc.table_name, 
        kcu.column_name, 
        ccu.table_name as foreign_table_name, 
        ccu.column_name as foreign_column_name,
        tc.constraint_type
    from 
        information_schema.table_constraints as tc  
        join information_schema.key_column_usage as kcu on (tc.constraint_name = kcu.constraint_name and tc.table_name = kcu.table_name)
        join information_schema.constraint_column_usage as ccu on ccu.constraint_name = tc.constraint_name
    where 
        constraint_type in ('PRIMARY KEY','FOREIGN KEY')
        and tc.constraint_schema = 'rights_test'

/*
This will produce desired output:
t1_pkey;rights_test.t1.id;rights_test;t1;id;t1;id;PRIMARY KEY
t2_id_fkey;rights_test.t2.id;rights_test;t2;id;t1;id;FOREIGN KEY
*/

create user rights_test_role with password 'password';

grant all on rights_test.t1 to rights_test_role;
grant all on rights_test.t2 to rights_test_role;

/* Now login as rights_test_role and try the same constraint select.
   For rights_test_role it returns nothing although I've added ALL privileges
*/

如果我不是关系的所有者,是否有其他方法如何获取相同的信息?

最佳答案

尝试使用这个.. 给出所有的约束名称和约束描述。

  • 外键
  • 检查
  • 主键
  • 独一无二

喜欢:

select conrelid::regclass AS table_from, conname, pg_get_constraintdef(c.oid)
from   pg_constraint c
join   pg_namespace n ON n.oid = c.connamespace
where  contype in ('f', 'p','c','u') order by contype

关于postgresql - 列出 PostgreSQL 中具有不同所有者的所有表的约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16830740/

相关文章:

php - 我的清理 $_POST[] 函数也清理了土耳其字符

linux - 安装 plpythonu 时出错(postresql 和 linux)

sql - 从列表中选择未出现在查询结果中的条目

sql - INT 列的 Postgres 数据加密

sql - CTE SQL查询在两个方向上递归

postgresql - 使用 Postgresql 和 hibernate 进行全文搜索 - 缺少 tsvector 和运算符 @@ 的类型

postgresql - 使用 postgres 表序列而不是共享 hibernate_sequence

postgresql - Postgres 不使用索引对数据进行排序

sql - 2 单表功能依赖

sql - 在 group by 子句中连接数组