[英]Is there a way to show a user-defined postgresql enumerated type definition?
Let's say we've defined a postgresql type:假设我们已经定义了一个 postgresql 类型:
CREATE TYPE my_type AS ENUM('foo', 'bar');
Is there any way to show the type definition after creation ?有没有办法在创建后显示类型定义?
I would expect "\\d my_type" to show me "ENUM('foo', 'bar')", but it says :我希望“\\d my_type”向我展示“ENUM('foo', 'bar')”,但它说:
Did not find any relation named "my_type"
The pg_type table doesn't seem to give enough information. pg_type 表似乎没有提供足够的信息。
Check this: 检查一下:
select enum_range(null::my_type)
I think this is a much simpler solution :). 我认为这是一个更简单的解决方案:)。
It's \\dT you're after, but it doesn't give it as a "CREATE" statement. 它是你所追求的,但并没有将它作为“创建”声明。 You use \\dD for domains.
您对域使用\\ dD。
\dT+ action.action_status
List of data types
Schema | Name | Internal name | Size | Elements | Description
--------+----------------------+---------------+------+----------+-------------
action | action.action_status | action_status | 4 | pending +|
| | | | live +|
| | | | done +|
| | | | notdone |
(1 row)
If you just want the full name (type name and schema) and a sorted list of all enum
labels, this query will do: 如果您只需要全名(类型名称和架构)以及所有
enum
标签的排序列表,则此查询将执行以下操作:
SELECT n.nspname AS "schema", t.typname
, string_agg(e.enumlabel, '|' ORDER BY e.enumsortorder) AS enum_labels
FROM pg_catalog.pg_type t
JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
JOIN pg_catalog.pg_enum e ON t.oid = e.enumtypid
WHERE t.typname = 'my_enum_type'
GROUP BY 1,2;
Returns: 返回:
schema | typname | enum_labels
--------+--------------+-------------
public | my_enum_type | foo|bar
string_agg()
requires Postgres 9.0 or later, replace with array_agg()
for older versions. string_agg()
需要Postgres 9.0或更高版本,替换为旧版本的array_agg()
。
To get the SQL CREATE
statement, you could use pg_dump
and look at the dump file. 要获取SQL
CREATE
语句,可以使用pg_dump
并查看转储文件。
Or, much more practically, use pgAdmin which displays reverse engineered SQL create scripts for any object in the database. 或者,实际上,使用pgAdmin为数据库中的任何对象显示反向工程SQL创建脚本。 Select it in the
object browser
and its create script is displayed in the SQL pane
. 在
object browser
选择它,其创建脚本将显示在SQL pane
。 There is even an option to copy the script to a newly opened window of the SQL editor
automatically, where you can edit and execute it. 甚至可以选择将脚本自动复制到新打开的
SQL editor
窗口,您可以在其中编辑和执行它。
SELECT t.typname
FROM pg_class c JOIN pg_attribute a ON c.oid = a.attrelid JOIN pg_type t ON a.atttypid = t.oid
WHERE c.relname = 'your_type';
The tricky part was that simply selecting * from these views one does not get OIDs in the results. 棘手的部分是,只需从这些视图中选择*,就不会在结果中获得OID。
using this post , I've archived the goal to mimic the 'CREATE TYPE' in PgAdmin & PgBackup使用这篇文章,我已经归档了在 PgAdmin 和 PgBackup 中模仿“CREATE TYPE”的目标
WITH types AS (
SELECT n.nspname,
pg_catalog.format_type ( t.oid, NULL ) AS obj_name,
CASE
WHEN t.typrelid != 0 THEN CAST ( 'tuple' AS pg_catalog.text )
WHEN t.typlen < 0 THEN CAST ( 'var' AS pg_catalog.text )
ELSE CAST ( t.typlen AS pg_catalog.text )
END AS obj_type,
coalesce ( pg_catalog.obj_description ( t.oid, 'pg_type' ), '' ) AS description
FROM pg_catalog.pg_type t
JOIN pg_catalog.pg_namespace n
ON n.oid = t.typnamespace
WHERE ( t.typrelid = 0
OR ( SELECT c.relkind = 'c'
FROM pg_catalog.pg_class c
WHERE c.oid = t.typrelid ) )
AND NOT EXISTS (
SELECT 1
FROM pg_catalog.pg_type el
WHERE el.oid = t.typelem
AND el.typarray = t.oid )
AND n.nspname <> 'pg_catalog'
AND n.nspname <> 'information_schema'
AND n.nspname !~ '^pg_toast'
),
cols AS (
SELECT n.nspname::text AS schema_name,
pg_catalog.format_type ( t.oid, NULL ) AS obj_name,
a.attname::text AS column_name,
pg_catalog.format_type ( a.atttypid, a.atttypmod ) AS data_type,
a.attnotnull AS is_required,
a.attnum AS ordinal_position,
pg_catalog.col_description ( a.attrelid, a.attnum ) AS description
FROM pg_catalog.pg_attribute a
JOIN pg_catalog.pg_type t
ON a.attrelid = t.typrelid
JOIN pg_catalog.pg_namespace n
ON ( n.oid = t.typnamespace )
JOIN types
ON ( types.nspname = n.nspname
AND types.obj_name = pg_catalog.format_type ( t.oid, NULL ) )
WHERE a.attnum > 0
AND NOT a.attisdropped
)
SELECT 'CREATE TYPE ' || cols.schema_name || '.' || cols.obj_name || E' AS (\n ' ||
pg_catalog.array_to_string (ARRAY(
SELECT cols.column_name || ' ' || cols.data_type AS col_num_typ
FROM cols
WHERE cols.obj_name='my_user_data_type'
ORDER BY cols.schema_name,
cols.obj_name,
cols.ordinal_position ), E',\n '
) || E'\n);'
AS cre_typ
FROM cols
WHERE cols.obj_name='my_user_data_type'
LIMIT 1
and run it under psql with this command to have only the SQL code :并使用此命令在 psql 下运行它以只有 SQL 代码:
\\t\\a\\g\\a\\t
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.