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

迁移opengauss数据库到PostgreSQL

SmallDB 2024-08-27
30

单表迁移

opengauss测试表

create table t_order( id serial primary key,cus_name varchar(30),order_code varchar(30));

opengauss导入数据

insert into t_order(cus_name,order_code) values('小A','OC0001'),('小B','OC0002'),('小C','OC0003'),('小D','OC0004');

opengauss查看表结构

gsql -d demo2 -p 5432 -r  -AXtq -c "select pg_get_tabledef('t_order')"

opengauss

备份单表

gs_dump -U opengauss -p 5432 demo2  -t public.t_order -f db_backup.sql -F p 

备份单表序列

gs_dump -U opengauss -p 5432 demo2 -s -t t_order_id_seq  -f t_order_id_seq.sql

拆看序列

SELECT sequence_schema,sequence_name FROM information_schema.sequences WHER sequence_schema = 't_order';

opengauss导入到PostgreSQL

第一步:涉及的用户进行迁移

因导出的SQL包含如下语句

ALTER TABLE public.t_order OWNER TO opengauss;

替换为postgres

ALTER TABLE public.t_order OWNER TO opengauss;

第二步:迁移涉及到序列导入(没有报错就可以了)

postgresql psql -d postgres -U postgres -h 127.0.0.1 -p 54322 -f seq.sql

第三步;迁移表

删除为;

WITH (orientation=row, compression=no);

因导出的SQL包含如下语句

ALTER TABLE public.t_order OWNER TO opengauss;

替换为postgres

ALTER TABLE public.t_order OWNER TO opengauss;

导入(没有报错就可以了)

postgresql psql -d postgres -U postgres -h 127.0.0.1 -p 54322 -f dd.sql

单表迁移总结

  • • -x, --no-privileges|–no-acl 防止转储访问权限(授权/撤销命令)

建立加上-x

gs_dump -U opengauss -p 5432 demo2  -x -t public.t_order -f db_backup.sql -F p 

基本步骤

第一步:涉及的用户进行迁移
第二步:迁移涉及到序列
第三步;迁移表

单库迁移

gs_dump -p 26000 mydb -x -f /home/omm/gs_dump/db_backup.sql -F p

第三步;修改库结构

删除为;

WITH (orientation=row, compression=no);

因导出的SQL包含如下语句

ALTER TABLE public.t_order OWNER TO opengauss;

替换为postgres

ALTER TABLE public.t_order OWNER TO opengauss;

导入(没有报错就可以了)

postgresql psql -d postgres -U postgres -h 127.0.0.1 -p 54322 -f dd.sql >>/tmp/dd.log 2>%1

查看日志有没有报错

vim /tmp/dd.log

经常出错的问题

tinyint字段问题,因为postgrsql没有
pg_systimestamp()字段不兼容,改成timestamp

- pg_systimestamp()
    描述:获取系统时间戳。
    返回值类型:timestamp with time zone

opengauss测试pg_systimestamp()函数

create table timestamp_demo(
id   int  primary key,
ts timestamp,
tstz timestamptz,
created timestamp(6) with time zone DEFAULT pg_systimestamp NOT NUll,
updated timestamp(6) with time zone DEFAULT pg_systimestamp NOT NUll
)

迁移到PG修改为

修改数据类型

alter table timestamp_demo alter created type timestamptz(6);
alter table timestamp_demo alter updated type timestamptz(6);

修改数据类型默认值

ALTER TABLE timestamp_demo ALTER COLUMN created SET DEFAULT CURRENT_TIMESTAMP(0);
ALTER TABLE timestamp_demo ALTER COLUMN updated SET DEFAULT CURRENT_TIMESTAMP(0);

单库迁移总结

基本步骤

第一步:涉及的用户进行迁移删除with表信息
第二步;导入库PG,出错排查
第三步;基本都是兼容性问题

分区表迁移

opengauss与postgresql的SQL是完全不兼容的,需要根据postgresql的格式新建立分区表

总结SQL

create table t_order( id serial primary key,cus_name varchar(30),order_code varchar(30));
insert into t_order(cus_name,order_code) values('小A','OC0001'),('小B','OC0002'),('小C','OC0003'),('小D','OC0004');
gsql -d demo2 -p 5432-r  -AXtq-c "select pg_get_tabledef('t_order')"
gs_dump -U opengauss -p 5432 demo2  -t public.t_order -f db_backup.sql -F p 
序列
gs_dump -U opengauss -p 5432 demo2 -s -t t_order_id_seq  -f t_order_id_seq.sql
SELECT sequence_schema,sequence_name FROM information_schema.sequences WHER sequence_schema ='t_order';

SELECT t.oid::regclass AS table_name,
       a.attname AS column_name,
       s.relname AS sequence_name
FROM pg_class AS t
   JOIN pg_attribute AS a
      ON a.attrelid = t.oid
   JOIN pg_depend AS d
      ON d.refobjid = t.oid
         AND d.refobjsubid = a.attnum
   JOIN pg_class AS s
      ON s.oid = d.objid
WHERE d.classid ='pg_catalog.pg_class'::regclass
  AND d.refclassid ='pg_catalog.pg_class'::regclass
  AND d.deptype IN ('i','a')
  AND t.relkind IN ('r','P')
  AND s.relkind ='S'and t.relname='t_order';


文章转载自SmallDB,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论