匿名用户pg如何查看某个表的建表语句,类似 show create table t 这样
pg没有类似语句,可以采用以下方法替代:
1. pg_dump -s -t
2. 自定义函数获取建表DDL,可参考:https://github.com/MichaelDBA/pg_get_tabledef/tree/main
示例:
-- 获取建表DDL,适用pg12及以上版本
-- DDL语句没有主键、唯一约束、索引、check约束,有not null约束,default值,序列;表有with存储参数
CREATE OR REPLACE FUNCTION ddl_versioning_get_tabledef(oid) RETURNS text
LANGUAGE sql STRICT AS $$
WITH attrdef AS (
SELECT
n.nspname,
c.relname,
pg_catalog.array_to_string(c.reloptions || array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ') as relopts,
c.relpersistence,
a.attnum,
a.attname,
pg_catalog.format_type(a.atttypid, a.atttypmod) as atttype,
(SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid, true) for 128) FROM pg_catalog.pg_attrdef d
WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef) as attdefault,
a.attnotnull,
(SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type t
WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation) as attcollation,
a.attidentity
,a.attgenerated --pg12系统表新增字段pg_attribute.attgenerated,pg11及以下版本注释掉该行
FROM pg_catalog.pg_attribute a
JOIN pg_catalog.pg_class c ON a.attrelid = c.oid
JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid
LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)
WHERE a.attrelid = $1
AND a.attnum > 0
AND NOT a.attisdropped
ORDER BY a.attnum
),
coldef AS (
SELECT
attrdef.nspname,
attrdef.relname,
attrdef.relopts,
attrdef.relpersistence,
pg_catalog.format(
'%I %s%s%s%s%s',
attrdef.attname,
attrdef.atttype,
case when attrdef.attcollation is null then '' else pg_catalog.format(' COLLATE %I', attrdef.attcollation) end,
case when attrdef.attnotnull then ' NOT NULL' else '' end,
case when attrdef.attdefault is null then ''
else case when attrdef.attgenerated = 's' then pg_catalog.format(' GENERATED ALWAYS AS (%s) STORED', attrdef.attdefault) --pg12系统表新增字段pg_attribute.attgenerated,pg11及以下版本注释掉该行
when attrdef.attgenerated <> '' then ' GENERATED AS NOT_IMPLEMENTED' --pg12系统表新增字段pg_attribute.attgenerated,pg11及以下版本注释掉该行
else pg_catalog.format(' DEFAULT %s', attrdef.attdefault)
end --pg12系统表新增字段pg_attribute.attgenerated,pg11及以下版本注释掉该行
end,
case when attrdef.attidentity<>'' then pg_catalog.format(' GENERATED %s AS IDENTITY',
case attrdef.attidentity when 'd' then 'BY DEFAULT' when 'a' then 'ALWAYS' else 'NOT_IMPLEMENTED' end)
else '' end
) as col_create_sql
FROM attrdef
ORDER BY attrdef.attnum
),
tabdef AS (
SELECT
coldef.nspname,
coldef.relname,
coldef.relopts,
coldef.relpersistence,
string_agg(coldef.col_create_sql, E',\n ') as cols_create_sql
FROM coldef
GROUP BY
coldef.nspname, coldef.relname, coldef.relopts, coldef.relpersistence
)
SELECT
format(
'CREATE%s TABLE %I.%I%s%s%s;',
case tabdef.relpersistence when 't' then ' TEMP' when 'u' then ' UNLOGGED' else '' end,
tabdef.nspname,
tabdef.relname,
coalesce(
(SELECT format(E'\n PARTITION OF %I.%I %s\n', pn.nspname, pc.relname,
pg_get_expr(c.relpartbound, c.oid))
FROM pg_class c JOIN pg_inherits i ON c.oid = i.inhrelid
JOIN pg_class pc ON pc.oid = i.inhparent
JOIN pg_namespace pn ON pn.oid = pc.relnamespace
WHERE c.oid = $1),
format(E' (\n %s\n)', tabdef.cols_create_sql)
),
case when tabdef.relopts <> '' then format(' WITH (%s)', tabdef.relopts) else '' end,
coalesce(E'\nPARTITION BY '||pg_get_partkeydef($1), '')
) as table_create_sql
FROM tabdef
$$;
评论
有用 3\d 表名
\dt+ 表名
pg_dump -U 用户名 -d 数据库名 -t 表名 -s
eg:
postgres=# create table xx1(i int not null );
CREATE TABLE
postgres=# \d xx1
Table "public.xx1"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
i | integer | | not null |
postgres=# \dt+ xx1
List of relations
Schema | Name | Type | Owner | Persistence | Access method | Size | Descript
ion
--------+------+-------+----------+-------------+---------------+---------+---------
----
public | xx1 | table | postgres | permanent | heap | 0 bytes |
(1 row)
postgres=# \q
[postgres@halo125 ~]$ pg_dump -U postgres -d postgres -t xx1 -s
--
CREATE TABLE public.xx1 (
i integer NOT NULL
);
ALTER TABLE public.xx1 OWNER TO postgres;
--
评论
有用 1还有个函数pg_get_tabledef()
评论
有用 0在 PostgreSQL 中,没有直接类似于 MySQL 的 SHOW CREATE TABLE 命令来查看某个表的建表语句。但是,你可以通过查询 PostgreSQL 的系统目录或使用一些工具来间接获取这些信息。
方法 1: 使用 \d+ 表名 在 psql 命令行工具中
如果你正在使用 psql(PostgreSQL 的命令行工具),你可以使用 \d+ 表名 命令来获取关于表的详细信息,包括列定义、索引、约束等。虽然这不会直接给出 SQL 建表语句,但它提供了足够的信息来手动重构它。
方法 2: 使用 pg_dump
另一个选项是使用 pg_dump 工具,它是 PostgreSQL 自带的一个用于备份数据库的工具。你可以使用它来导出特定表的 SQL 定义,包括建表语句。
例如,要导出名为 your_table 的表的建表语句,你可以使用以下命令:
pg_dump -U 用户名 -d 数据库名 -t your_table --schema-only
这个命令会输出包含 your_table 建表语句的 SQL 脚本,但不会包括数据。
方法 3: 查询系统目录
PostgreSQL 的系统目录(如 information_schema 和 pg_catalog)包含了关于数据库结构的信息。你可以通过查询这些目录来手动构建建表语句。
例如,以下 SQL 查询可以获取表的基本结构(列名、数据类型等):
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_name = 'your_table' AND table_schema = 'public';
但是,请注意,这种方法需要你手动处理索引、约束等其他表元素。
方法 4: 使用第三方工具
还有一些第三方工具(如 DBeaver、pgAdmin 等)提供了图形界面来查看和导出表的建表语句。这些工具通常通过查询系统目录并格式化输出来实现这一功能,但它们为用户提供了更友好的界面。
结论
虽然 PostgreSQL 没有直接类似于 MySQL 的 SHOW CREATE TABLE 命令,但你可以通过上述方法中的任何一种来获取或重建表的建表语句。对于大多数情况,使用 pg_dump 或第三方工具是最简单且最直接的方法。
评论
有用 0
墨值悬赏

