postgresql 相当于 PgAdmin3 中的“描述表”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21442478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Equivalent of "describe table" in PgAdmin3
提问by user3112568
Question asked and answered:
提出并回答的问题:
As many of us know, PostgreSQL does not support describe table
or describe view
. As one might find from google, PostgreSQL uses \d+
instead.
众所周知,PostgreSQL 不支持describe table
或describe view
. 正如你可能从谷歌发现的那样,PostgreSQL 使用了\d+
。
However, if one accesses PostgreSQL using PgAdmin (I am actually using PgAdmin3) then \d+
does not work. What does one do instead?
但是,如果使用 PgAdmin(我实际上使用的是 PgAdmin3)访问 PostgreSQL,则\d+
它不起作用。一个人做什么?
I thought about this question when playing with the query tool in PgAdmin3. I had a "well, duh!" moment when I thought to look at the home window of PgAdmin3, and at the tree on the left side of that window. Under
我在玩PgAdmin3中的查询工具时想到了这个问题。我有一个“好吧,呃!” 当我想到查看 PgAdmin3 的主窗口以及该窗口左侧的树时。在下面
<servername>
-> <databasename>
-> Schemas
-> <schemaname>
-> Tables
was a list of my tables,
and clicking on the table name showed me text
very much like what \d+
would have showed me.
是我的表的列表,单击表名会显示与我显示的非常相似的文本\d+
。
So for the benefit of anyone else who did not discover this right away, here is an answer.
因此,为了其他没有立即发现这一点的人的利益,这里是一个答案。
回答by Gareth Flowers
PostgreSQL also supports the standard SQL information schema to retrieve details of objects in the database.
PostgreSQL 还支持标准 SQL 信息模式来检索数据库中对象的详细信息。
i.e. to get column information you can query the information_schema.columns
view:
即要获取列信息,您可以查询information_schema.columns
视图:
SELECT *
FROM information_schema.columns
WHERE table_name = '????';
Check herefor PostgreSQL specific details on the information schema.
请在这里为信息架构PostgreSQL的具体细节。
回答by Yordan Georgiev
and the straight from the bash shell:
和直接来自 bash shell:
psql -d "$db_name" -c '
SELECT
ordinal_position , table_name , column_name , data_type , is_nullable
FROM information_schema.columns
WHERE 1=1
AND table_name = '\''my_table'\''
;'
# or just the col names
psql -d "$my_db" -t -c \
"SELECT column_name FROM information_schema.columns
WHERE 1=1 AND table_name='my_table'"
回答by Nate
To get the full view that the describe query would return right click on the relation/table of interest and select Properties... then use the Columns tab in the window provided.
要获得描述查询将返回的完整视图,请右键单击感兴趣的关系/表并选择“属性...”,然后使用提供的窗口中的“列”选项卡。
The only difference is that the window does not give information about foreign key relation.
唯一的区别是该窗口不提供有关外键关系的信息。
回答by harmic
psql's \d command sends a set of queries to the database to interrogate the schema, then prints the result.
psql 的 \d 命令向数据库发送一组查询以询问模式,然后打印结果。
You can use the '-E' psql option to get it to display these queries, if you want to be able to extract similar information directly via SQL.
如果您希望能够直接通过 SQL 提取类似信息,您可以使用 '-E' psql 选项让它显示这些查询。
Having said that, psql uses the internal Postgresql catalog tables, instead of the standardized 'information_schema' schema (see answer from garethflowers). So if you care about portability, or even guaranteeing that it will continue to work from one release to the next, you probably should use information_schema.
话虽如此,psql 使用内部 Postgresql 目录表,而不是标准化的“information_schema”模式(参见 garethflowers 的回答)。因此,如果您关心可移植性,或者甚至保证它从一个版本到下一个版本都可以继续工作,您可能应该使用 information_schema。