如何查看PostgreSQL约束的注释?

如何查看PostgreSQL约束的注释?,postgresql,Postgresql,PostgreSQL有一个语法,用于根据约束: COMMENT ON CONSTRAINT` constraint_name ON table_name IS 'text'` 例如: COMMENT ON CONSTRAINT bar_col_cons ON bar IS 'Constrains column col'; 这告诉我如何定义约束的注释。但是我如何看待已经定义的注释呢 表上的\d+输出包括约束列表,但不显示注释。\dd应显示注释,但不过滤表名。您可以使用系统目录并查询约束注释 带

PostgreSQL有一个语法,用于根据约束:

COMMENT ON CONSTRAINT` constraint_name ON table_name IS 'text'`
例如:

COMMENT ON CONSTRAINT bar_col_cons ON bar IS 'Constrains column col';
这告诉我如何定义约束的注释。但是我如何看待已经定义的注释呢


表上的
\d+
输出包括约束列表,但不显示注释。

\dd
应显示注释,但不过滤表名。

您可以使用系统目录并查询约束注释

带有约束注释的示例表:

create table test(
    id int unique,
    str text check(str <> '')
);

comment on constraint test_id_key on test is 'my comment on test_id_key';
comment on constraint test_str_check on test is 'my comment on test_str_check';

另一种方法是查询和使用该函数,如下所示:

select conname, obj_description(oid) as description
from pg_constraint
where conrelid = '(myschema.)mytable'::regclass;

如果您使用运行
psql
,它将显示您运行的各种
\d
命令背后的所有SQL。对于系统表上的快速“n”脏教程非常方便。
select conname, obj_description(oid) as description
from pg_constraint
where conrelid = '(myschema.)mytable'::regclass;