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


当前回答

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

其他回答

您可以使用此选项:

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

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

 \d myTable describe table

它也适用于其他对象:

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

来源:faqs.org

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;

这个问题的变体(如其他答案中所解释的)对我很有用。

SELECT
 COLUMN_NAME
FROM
 information_schema.COLUMNS
WHERE
 TABLE_NAME = 'city';

详细描述如下:http://www.postgresqltutorial.com/postgresql-describe-table/