从 PostgreSQL 数据库检索评论
我正在 Postgres 数据库上运行一个项目,需要检索数据库中列的注释以用作表标题等。 我看到有几个内置函数(pg_description< /a> 和 col_description),但我无法事实证明,寻找如何使用它们并尝试使用它们的示例是相当徒劳的。
所以我想知道以前是否有人能够做到这一点,如果可以的话,是如何做到的?
I'm running a project on a Postgres database and need to retrieve the comments on columns within the DB to be used as table headings and such. I have seen that there are a couple of built in functions (pg_description and col_description) but i haven't been able to find examples on how to use them and playing around with them has proved pretty futile.
So I was wondering if any has been able to do this before and if so, how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
只要有人需要就在这里。
这里有很多答案,但没有一个像我希望的那么简单。 因此,根据以前的答案和当前的 postgres 9.4,我创建了这个查询:
它获取表和列描述,没有任何令人困惑的连接和丑陋的字符串连接。
Just to be here if somebody will need it.
There are many answers here, but none of them was as simple as I would like it to be. So, based on previous answers and current postgres 9.4, I have created this query:
It fetches table and column descriptions, without any confusing joins and ugly string concatenations.
这一切都是通过 oid 进行的,
现在,我有了该表的 oid,所以我可以询问:
然后,我可以询问第四列的描述:
如果您想知道
psql
执行哪些查询当您执行\dt+
或\d+customers
时运行,只需使用-E
运行即可。It all works by oid,
Now, I have the oid for that table, so I can ask :
Then, I can ask for the description of the fourth column :
If you want to know which queries does
psql
run when you do\dt+
or\d+ customers
, just run it with-E
.请注意模式,此代码考虑了它们:
参考文献:
Postgresql文档表和列描述表和列的注释
确定 Postgres 9.1 中表的 OID?
Take care with schemas, this code considers them:
References:
Postgresql Document Table and Column Description Comments on Table and Column
Determining the OID of a table in Postgres 9.1?
对其他答案之一稍作更改,它只为您提供有评论的列,这将为您提供所有列,无论它们是否有评论。
A slight change to one of the other answers which only gives you columns that have comments on them, this gives you all columns whether they have a comment or not.
如果您只需要在其他数据中显示列的
注释
,您还可以使用:If you just need to show the
comments
for your columns among other data, you can also use:这对我使用 PostBooks 3.2.2 DB 有效:
问候,
西林斯尔
This works for me using the PostBooks 3.2.2 DB:
Regards,
Sylnsr
增强@Nick 和@mat 建议:使用
SELECT obj_description('schemaName.tableName'::regclass, 'pg_class');
当你有字符串名称(不是 oid)时。
避免记住“pg_class”参数,并避免在函数调用时出现丑陋的串联,如
(tname||'.'||schema)::regclass
,这是 obj_description 的有用重载:现在很容易使用,因为表名称(
rname
参数)是一个varchar,可以用模式名称的分隔字段来表示,如主表和查询中一样。另请参阅“获取 PostgreSQL 中的表注释列表”或 新 pg9.3 指南
Enhance for @Nick and @mat suggestions: use
SELECT obj_description('schemaName.tableName'::regclass, 'pg_class');
when you have string name (not oid).
To avoid to remember 'pg_class' parameter, and to avoid ugly concatenations at the function calls, as
(tname||'.'||schema)::regclass
, an useful overload forobj_description
:Now is easy to use, because the table name (
rname
parameter) is a varchar and can be expressed with a separated field for schema name, as in the main tables and queries.See also "Getting list of table comments in PostgreSQL" or the new pg9.3 Guide
这个答案有点晚了,但它出现在我为研究这个问题所做的谷歌搜索中。 我们只需要表描述,但列的方法是相同的。
列描述也位于 pg_description 表中,由 objoid 引用。
添加此视图:
然后运行:
该视图是 pg_tables 视图的修改版本,添加了描述列。
您还可以修改视图定义以使其成为单个查询。
This answer is a little late, but it popped up on a google search I did to research this problem. We only needed Table descriptions, but the method would be the same for columns.
The column descriptions are in the pg_description table also, referenced by objoid.
Add this view:
Then run:
The view is a modified version of the pg_tables view which adds in the description column.
You could also monkey around with the view definition to make it a single query.
我访问了这样的表注释:
和列注释:
I accessed table comments like this:
and column comments thusly:
我问了一个类似的问题上个月 Postgresql 评论。 如果您深入研究,您会在我的博客上发现一些 Perl 代码,它可以自动执行提取评论的过程。
要提取表的列名,您可以使用如下所示的内容:
然后您可以使用 tableoid,columnoid 元组来提取每列的注释(请参阅我的问题)。
I asked a similar question about Postgresql comments last month. If you dig through that, you'll come across some Perl code over on my blog that automates the process of extracting a comment.
To pull out the column names of a table, you can use something like the following:
You can then use the tableoid,columnoid tuple to extract the comment of each column (see my question).
我刚在这里找到这个。 它将为您提供一个特定表的所有类型的元数据(类型、默认值、非空标志、长度、注释、外键名称、主键名称)。 看起来效果不错。
来源:http://golden13.blogspot.de /2012/08/how-to-get-some-information-about_7.html
I just found this here. It will provide you with all kind of metadata on one specific table (type, default value, not null flag, length, comment, foreign key name, primary key name). It seems to work well.
Source: http://golden13.blogspot.de/2012/08/how-to-get-some-information-about_7.html
好的,所以我在某种程度上解决了这个问题...
select col_description(table id, columns number)...
即: select col_description(36698,2);
这可行,但是有没有更简单的方法来做到这一点,也许可以将所有注释都放在所有列上,并使用表名而不是 oid???
Ok, so i worked it out to degree...
select col_description(table id, column number)...
ie: select col_description(36698,2);
That worked, but is there an easier way to do this maybe bringing all the comments on all the columns and using the table name instead of the oid???
要显示所有表的所有列的注释:
您需要在任何模式/目录/数据库之外执行此查询
此查询基于此问题中的另一个答案,该答案仅显示来自一个表的注释
To display comments from all columns of all table :
You need to execute this query outside any schema/catalog/db
This query is based on another answer in this question which display comments from one table only
扩展@amxy 提供的响应; 我发现添加架构过滤器在某些环境中会有所帮助。 我发现 @amxy 的解决方案在我通过架构过滤器添加之前不起作用
结果:
To extend on the response provided by @amxy; I found that adding a schema filter can help in some environments. As I found @amxy's solution didn't work until I added by schema filters
RESULTS:
SELECT sc.table_schema、sc.table_name、sc.column_name、col_description(pc."oid"、sc.ordinal_position) col_description FROM pg_class pc
INNER JOIN pg_namespace ns ON ns."oid" =pc.relnamespace
内连接information_schema。列sc ON sc.table_name=pc.relname AND sc.table_schema=ns.nspname
哪里1=1
AND upper(ns.nspname) = 'TABLE_SCHEMA'
和 上层(pc.relname) = 'TABLE_NAME'
从 PostgreSQL 数据库检索评论
SELECT sc.table_schema , sc.table_name, sc.column_name, col_description(pc."oid" , sc.ordinal_position) col_description FROM pg_class pc
INNER JOIN pg_namespace ns ON ns."oid" =pc.relnamespace
INNER JOIN information_schema.COLUMNS sc ON sc.table_name=pc.relname AND sc.table_schema=ns.nspname
WHERE 1=1
AND upper(ns.nspname) = 'TABLE_SCHEMA'
AND upper(pc.relname) = 'TABLE_NAME'
Retrieving Comments from a PostgreSQL DB