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


当前回答

尝试以下操作(在psql命令行工具中):

\d+ tablename

有关更多信息,请参阅手册。

其他回答

即使请求了psql命令,我也会添加pg_dump命令。因为它生成了以前的MySQl用户更常见的输出。

#sudo-u postgres pg_dump--table=my_table_name--仅模式mydb

这应该是解决方案:

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

除了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引擎的支持。

Use this command 

\d table name

like 

\d queuerecords

             Table "public.queuerecords"
  Column   |            Type             | Modifiers
-----------+-----------------------------+-----------
 id        | uuid                        | not null
 endtime   | timestamp without time zone |
 payload   | text                        |
 queueid   | text                        |
 starttime | timestamp without time zone |
 status    | text                        |

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