如何使用psql命令在PostgreSQL中执行Oracle的DESCRIBE TABLE?


当前回答

/dt是列出数据库中所有表的命令。使用/d命令和/d+,我们可以获得表的详细信息。sysntax将如下所示*/d table_name(或)\d+table_name

其他回答

描述表的最佳方式,如列、类型、列的修饰符等。

\d+ tablename or \d tablename

您可以使用星号执行\d*搜索模式*,以查找与您感兴趣的搜索模式匹配的表。

当您的表不是默认模式的一部分时,您应该写:

\d+ schema_name.table_name

否则,您将得到错误消息,即“关系不存在”

当表名以大写字母开头时,应将表名放在引号中。

示例:\d“用户”

我为get表模式编写了以下脚本。

'CREATE TABLE ' || 'yourschema.yourtable' || E'\n(\n' ||
array_to_string(
array_agg(
'    ' || column_expr
)
, E',\n'
) || E'\n);\n'
from
(
SELECT '    ' || column_name || ' ' || data_type || 
coalesce('(' || character_maximum_length || ')', '') || 
case when is_nullable = 'YES' then ' NULL' else ' NOT NULL' end as column_expr
FROM information_schema.columns
WHERE table_schema || '.' || table_name = 'yourschema.yourtable'
ORDER BY ordinal_position
) column_list;