暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

PG 数据库结构迁移注意事项

原创 岳麓丹枫 2024-03-06
239

PG13 或者以上版本导出表结构然后去 PG13 以下版本来恢复记得 导出的时候添加上 --no-toast-compression

PG13 版本建表语法

[postgres@ ~/lxm/2024-03-06/test_data/public_schema]$ psql psql (13.2) Type "help" for help. [postgres@cqbm301017:5779/(rasesql)postgres][03-06.17:22:51]M=# \h create table Command: CREATE TABLE Description: define a new table Syntax: CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name ( [ { column_name data_type [ COLLATE collation ] [ COMPRESSION compression_method ] [ column_constraint [ ... ] ] | table_constraint | LIKE source_table [ like_option ... ] } [, ... ] ] ) 实例: CREATE TABLE public.test ( created_by character varying(100) COMPRESSION pglz, created_date timestamp without time zone, updated_by character varying(100) COMPRESSION pglz, updated_date timestamp without time zone, id_test_qual_leak character varying(32) COMPRESSION pglz, id_test_quality_case character varying(32) COMPRESSION pglz, test_qual_link character varying(2) COMPRESSION pglz, id_leak_qual_link character varying(32) COMPRESSION pglz, link_leak_total_amount bigint, link_project_type character varying(50) COMPRESSION pglz );

PG11 建表语法

[postgres@ ~/lxm/v2]$ psql psql (11.5) Type "help" for help. [postgres@cnsz383429:6472/postgres][03-06.17:29:10]M=# \h create table Command: CREATE TABLE Description: define a new table Syntax: CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name ( [ { column_name data_type [ COLLATE collation ] [ column_constraint [ ... ] ] | table_constraint | LIKE source_table [ like_option ... ] } [, ... ] ] )

结论

  1. pg_dump 导出表结构的时候, 如果不添加 --no-toast-compression 会在生成的建表语句中带有 COMPRESSION 关键字, 但是在 PG13 以下库中恢复的时候, 会报错
  2. pg_dump备份的时候, 如果只关注 public 模式下的对象, 记得要指定 -n publc
  3. 导入的时候 记得使用 -d <db_name> 来导入到指定库中, 否则会导入到 postgres 库中了
* 导出(备份) pg_dump -U postgres -d test -n public --no-toast-compression -s -f tbs_schema.sql -T pgawr* -T t11 -T t22 -T test* * 导入(恢复) psql -d test -f tbs_schema.sql
最后修改时间:2024-03-26 10:55:03
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论