PostgreSQL“DESCRIBE TABLE”

如何在PostgreSQL中执行相当于OracleDESCRIBE TABLE的操作(使用psql命令)?

1355454 次浏览

DESCRIBE TABLE的psql等价物是\d table

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

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

 \d myTable describe table

它也适用于其他对象:

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

来源:faqs.org

试试这个(在psql命令行工具中):

\d+ tablename

查看该手册了解更多信息。

除了PostgreSQL的方式(\d'某物'或\dt'表'或\ds'序列'等)

SQL标准方式,如图这里所示:

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

它被许多数据库引擎支持。

如果您想从查询而不是psql获取它,您可以查询曲库模式。这是一个复杂的查询:

SELECTf.attnum AS number,f.attname AS name,f.attnum,f.attnotnull AS notnull,pg_catalog.format_type(f.atttypid,f.atttypmod) AS type,CASEWHEN p.contype = 'p' THEN 't'ELSE 'f'END AS primarykey,CASEWHEN p.contype = 'u' THEN 't'ELSE 'f'END AS uniquekey,CASEWHEN p.contype = 'f' THEN g.relnameEND AS foreignkey,CASEWHEN p.contype = 'f' THEN p.confkeyEND AS foreignkey_fieldnum,CASEWHEN p.contype = 'f' THEN g.relnameEND AS foreignkey,CASEWHEN p.contype = 'f' THEN p.conkeyEND AS foreignkey_connnum,CASEWHEN f.atthasdef = 't' THEN d.adsrcEND AS defaultFROM pg_attribute fJOIN pg_class c ON c.oid = f.attrelidJOIN pg_type t ON t.oid = f.atttypidLEFT JOIN pg_attrdef d ON d.adrelid = c.oid AND d.adnum = f.attnumLEFT JOIN pg_namespace n ON n.oid = c.relnamespaceLEFT JOIN pg_constraint p ON p.conrelid = c.oid AND f.attnum = ANY (p.conkey)LEFT JOIN pg_class AS g ON p.confrelid = g.oidWHERE c.relkind = 'r'::charAND n.nspname = '%s'  -- Replace with Schema nameAND c.relname = '%s'  -- Replace with table nameAND f.attnum > 0 ORDER BY number;

它非常复杂,但它确实向您展示了PostgreSQL系统曲库的强大功能和灵活性,并且应该让您pg_catalog掌握;-)。请务必更改查询中的%s。第一个是Schema,第二个是表名。

您可以执行\d *search pattern *带星号来查找与您感兴趣的搜索模式匹配的表。

你可以使用这个:

SELECT attnameFROM pg_attribute,pg_classWHERE attrelid=pg_class.oidAND relname='TableName'AND attstattarget <>0;

除了您已经找到的命令行\d+ <table_name>之外,您还可以使用信息图式查找列数据,使用info_schema.columns

SELECT *FROM info_schema.columnsWHERE table_schema = 'your_schema'AND table_name   = 'your_table'

使用以下SQL语句

SELECT DATA_TYPEFROM INFORMATION_SCHEMA.COLUMNSWHERE table_name = 'tbl_name'AND COLUMN_NAME = 'col_name'

如果您替换tbl_name和col_name,它会显示您要查找的特定颜色的数据类型。

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

Select * from schema_name.table_name limit 0;

扩展:我的表有2列名称和pwd。给出下面的屏幕截图。

添加图像

*使用PG admin3

Use this command
\d table name
like
\d queuerecords
Table "public.queuerecords"Column   |            Type             | Modifiers-----------+-----------------------------+-----------id        | uuid                        | not nullendtime   | timestamp without time zone |payload   | text                        |queueid   | text                        |starttime | timestamp without time zone |status    | text                        |

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

\d+ tablename or \d tablename

/dt是列出数据库中存在的所有表的逗号。使用
/d命令和 /d+我们可以得到一个表的详细信息* /dtable_name(或)\d+table_name

这种查询的变体(如其他答案所解释的)对我有效。

SELECTCOLUMN_NAMEFROMinformation_schema.COLUMNSWHERETABLE_NAME = 'city';

详细介绍如下:http://www.postgresqltutorial.com/postgresql-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_exprFROM information_schema.columnsWHERE table_schema || '.' || table_name = 'yourschema.yourtable'ORDER BY ordinal_position) column_list;

mysql中,描述table_name


PostgreSQL中,\dtable_name


或者,您可以使用这个长命令:

SELECTa.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), '\'(.*)\'')FROMpg_catalog.pg_attrdef dWHEREd.adrelid = a.attrelidAND d.adnum = a.attnumAND a.atthasdef) AS Default,'' as ExtrasFROMpg_class cJOIN pg_attribute a ON a.attrelid = c.oidJOIN pg_type t ON a.atttypid = t.oidLEFT JOIN pg_catalog.pg_constraint r ON c.oid = r.conrelidAND r.conname = a.attnameWHEREc.relname = 'tablename'AND a.attnum > 0
ORDER BY a.attnum

在postgres中,d用于描述表结构。

例如\d schema_name.table_name

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

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

\d+ schema_name.table_name

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

为了改进另一个答案的SQL查询(太棒了!),这是一个修订后的查询。它还包括约束名称、继承信息以及分解为其组成部分(类型、长度、精度、缩放)的数据类型。它还过滤掉已删除的列(仍然存在于数据库中)。

SELECTn.nspname as schema,c.relname as table,f.attname as column,f.attnum as column_id,f.attnotnull as not_null,f.attislocal not_inherited,f.attinhcount inheritance_count,pg_catalog.format_type(f.atttypid,f.atttypmod) AS data_type_full,t.typname AS data_type_name,CASEWHEN f.atttypmod >= 0 AND t.typname <> 'numeric'THEN (f.atttypmod - 4) --first 4 bytes are for storing actual length of dataEND AS data_type_length,CASEWHEN t.typname = 'numeric' THEN (((f.atttypmod - 4) >> 16) & 65535)END AS numeric_precision,CASEWHEN t.typname = 'numeric' THEN ((f.atttypmod - 4)& 65535 )END AS numeric_scale,CASEWHEN p.contype = 'p' THEN 't'ELSE 'f'END AS is_primary_key,CASEWHEN p.contype = 'p' THEN p.connameEND AS primary_key_name,CASEWHEN p.contype = 'u' THEN 't'ELSE 'f'END AS is_unique_key,CASEWHEN p.contype = 'u' THEN p.connameEND AS unique_key_name,CASEWHEN p.contype = 'f' THEN 't'ELSE 'f'END AS is_foreign_key,CASEWHEN p.contype = 'f' THEN p.connameEND AS foreignkey_name,CASEWHEN p.contype = 'f' THEN p.confkeyEND AS foreign_key_columnid,CASEWHEN p.contype = 'f' THEN g.relnameEND AS foreign_key_table,CASEWHEN p.contype = 'f' THEN p.conkeyEND AS foreign_key_local_column_id,CASEWHEN f.atthasdef = 't' THEN d.adsrcEND AS default_valueFROM pg_attribute fJOIN pg_class c ON c.oid = f.attrelidJOIN pg_type t ON t.oid = f.atttypidLEFT JOIN pg_attrdef d ON d.adrelid = c.oid AND d.adnum = f.attnumLEFT JOIN pg_namespace n ON n.oid = c.relnamespaceLEFT JOIN pg_constraint p ON p.conrelid = c.oid AND f.attnum = ANY (p.conkey)LEFT JOIN pg_class AS g ON p.confrelid = g.oidWHERE c.relkind = 'r'::charAND f.attisdropped = falseAND n.nspname = '%s'  -- Replace with Schema nameAND c.relname = '%s'  -- Replace with table nameAND f.attnum > 0ORDER BY f.attnum;

这应该是解决方案:

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

1)使用psql的PostgreSQL DESCRIBE TABLE

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

2)PostgreSQL DESCRIBE TABLE使用information_schema

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

选择COLUMN_NAMEDATA_TYPECHARACTER_MAXIMUM_LENGTH从INFORMATION_SCHEMA.COLUMNStable_name='tablename';

更多信息https://www.postgresqltutorial.com/postgresql-describe-table/

当你的表不是默认模式的一部分时,你应该写:

\d+ schema_name.table_name

否则,您将收到错误消息“关系不存在”。

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

# sudo -u postgres pg_dump --table=my_table_name --schema-only mydb

当你的表名以大写字母开头时,你应该把你的表名放在引号中。

示例:\d "Users"