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


当前回答

我为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;

其他回答

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

\d+ tablename or \d tablename

除了PostgreSQL方式(\d“something”或\dt“table”或\ds“sequence”等)

SQL标准方式,如下所示:

select column_name, data_type, character_maximum_length, column_default, is_nullable
from INFORMATION_SCHEMA.COLUMNS where table_name = '<name of table>';

它受到许多db引擎的支持。

您也可以使用以下查询进行检查

Select * from schema_name.table_name limit 0;

示例:我的表有两列名称和pwd。下面是截图。

*使用PG admin3

In postgres\d用于描述表结构。

例如\d schema_name.table_name

该命令将为您提供表的基本信息,如列、类型和修饰符。

如果您想了解更多有关表格使用的信息

\d+ schema_name.table_name

这将为您提供额外的信息,如存储、统计目标和描述

这应该是解决方案:

SELECT * FROM information_schema.columns
WHERE table_schema = 'your_schema'
   AND table_name   = 'your_table'