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


当前回答

与DESCRIBE TABLE等效的psql是\d TABLE。

有关详细信息,请参阅PostgreSQL手册的psql部分。

其他回答

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

您可以使用此选项:

SELECT attname 
FROM pg_attribute,pg_class 
WHERE attrelid=pg_class.oid 
AND relname='TableName' 
AND attstattarget <>0; 

1) 使用psql的PostgreSQL描述表

在psql命令行工具中,使用\d table_name或\d+table_name查找表的列信息

2) PostgreSQL使用information_schema描述表

SELECT语句查询information_schema数据库中列表的column_name、数据类型、字符最大长度;

选择列名称、数据类型、字符最大长度来自INFORMATION_SCHEMA.COLUMNS,其中table_name=“tablename”;

有关详细信息https://www.postgresqltutorial.com/postgresql-describe-table/

您可以使用psql斜杠命令执行此操作:

 \d myTable describe table

它也适用于其他对象:

 \d myView describe view
 \d myIndex describe index
 \d mySequence describe sequence

来源:faqs.org

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

Select * from schema_name.table_name limit 0;

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

*使用PG admin3