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


当前回答

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

示例:\d“用户”

其他回答

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

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

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

\d+ tablename or \d tablename
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                        |

在MySQL中,DESCRIBE table_name


在PostgreSQL中,\d table_name


或者,可以使用以下长命令:

SELECT
        a.attname AS Field,
        t.typname || '(' || a.atttypmod || ')' AS Type,
        CASE WHEN a.attnotnull = 't' THEN 'YES' ELSE 'NO' END AS Null,
        CASE WHEN r.contype = 'p' THEN 'PRI' ELSE '' END AS Key,
        (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid), '\'(.*)\'')
                FROM
                        pg_catalog.pg_attrdef d
                WHERE
                        d.adrelid = a.attrelid
                        AND d.adnum = a.attnum
                        AND a.atthasdef) AS Default,
        '' as Extras
FROM
        pg_class c 
        JOIN pg_attribute a ON a.attrelid = c.oid
        JOIN pg_type t ON a.atttypid = t.oid
        LEFT JOIN pg_catalog.pg_constraint r ON c.oid = r.conrelid 
                AND r.conname = a.attname
WHERE
        c.relname = 'tablename'
        AND a.attnum > 0

ORDER BY a.attnum

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

示例:\d“用户”