查询 PostgreSQL 中表的架构详细信息?

发布于 2024-10-06 00:57:58 字数 136 浏览 7 评论 0原文

我需要知道 PostgreSQL 中的列类型(即 varchar(20))。我知道我可能可以使用 psql 中的 \d 找到它,但我需要它通过选择查询来完成。
这在 PostgreSQL 中可能吗?

I need to know the column type in PostgreSQL (i.e. varchar(20)). I know that I could probably find this using \d something in psql, but I need it to be done with a select query.
Is this possible in PostgreSQL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

奶茶白久 2024-10-13 00:57:58

PostgreSQL 中有一种更简单的方法来获取列的类型。

SELECT pg_typeof(col)::text FROM tbl LIMIT 1

当然,该表必须至少包含一行。并且您只能获得没有类型修饰符(如果有)的基本类型。如果您也需要,请使用下面的替代方案。
您也可以将该函数用于常量。 关于 pg_typeof() 的手册

对于空(或任何)表,您可以使用查询系统目录 pg_attribute 来按顺序获取列的完整列表及其各自的类型:

SELECT attnum, attname AS column, format_type(atttypid, atttypmod) AS type
FROM   pg_attribute
WHERE  attrelid = 'myschema.mytbl'::regclass   -- optionally schema-qualified
AND    NOT attisdropped
AND    attnum > 0
ORDER  BY attnum;

format_type()对象标识符类型,例如regclass

There is a much simpler way in PostgreSQL to get the type of a column.

SELECT pg_typeof(col)::text FROM tbl LIMIT 1

The table must hold at least one row, of course. And you only get the base type without type modifiers (if any). Use the alternative below if you need that, too.
You can use the function for constants as well. The manual on pg_typeof().

For an empty (or any) table you can use query the system catalog pg_attribute to get the full list of columns and their respective type in order:

SELECT attnum, attname AS column, format_type(atttypid, atttypmod) AS type
FROM   pg_attribute
WHERE  attrelid = 'myschema.mytbl'::regclass   -- optionally schema-qualified
AND    NOT attisdropped
AND    attnum > 0
ORDER  BY attnum;

The manual on format_type() and on object identifier types like regclass.

悸初 2024-10-13 00:57:58

您可以使用 postgres 和以下查询来完整描述一个表:

SELECT
  a.attname as Column,
  pg_catalog.format_type(a.atttypid, a.atttypmod) as Datatype
  FROM
  pg_catalog.pg_attribute a
  WHERE
    a.attnum > 0
  AND NOT a.attisdropped
  AND a.attrelid = (
    SELECT c.oid
    FROM pg_catalog.pg_class c
    LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname ~ '^(TABLENAME)

通过此您将检索列名和数据类型。

也可以使用 -E 选项启动 psql 客户端

$ psql -E

,然后一个简单的 \d mytable 将输出 postgres 用于描述表的查询。它适用于每个 psql 描述命令。

AND pg_catalog.pg_table_is_visible(c.oid) )

通过此您将检索列名和数据类型。

也可以使用 -E 选项启动 psql 客户端

,然后一个简单的 \d mytable 将输出 postgres 用于描述表的查询。它适用于每个 psql 描述命令。

You can fully describe a table using postgres with the following query:

SELECT
  a.attname as Column,
  pg_catalog.format_type(a.atttypid, a.atttypmod) as Datatype
  FROM
  pg_catalog.pg_attribute a
  WHERE
    a.attnum > 0
  AND NOT a.attisdropped
  AND a.attrelid = (
    SELECT c.oid
    FROM pg_catalog.pg_class c
    LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname ~ '^(TABLENAME)

Tith this you will retrieve column names and data type.

It is also possible to start psql client using the -E option

$ psql -E

And then a simple \d mytable will output the queries used by postgres to describe the table. It work for every psql describe commands.

AND pg_catalog.pg_table_is_visible(c.oid) )

Tith this you will retrieve column names and data type.

It is also possible to start psql client using the -E option

And then a simple \d mytable will output the queries used by postgres to describe the table. It work for every psql describe commands.

百善笑为先 2024-10-13 00:57:58

是的,请查看information_schema

Yes, look at the information_schema.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文