Mysql导入PostgreSQL
异构数据库导入可以用FDW,但测试后发现FDW部分显示乱码,所以决定采用COPY 文本文件的方式,具体步骤如下:
1、转化DDL
modeltrans.pl
2、MySQL端生成文本文件,并做相应转化,把换行符替换掉
mysql > SELECT CONCAT('select * from ',
table_name,
' into outfile '/tmp/pv_split/',
table_name,
'.dat' fields terminated by '|' ;')
FROM information_schema.tables
WHERE table_schema='who_ebi';
shell> sed -i 's/r//g' /tmp/pv_split/*
如果不替换这一步会报错误
ERROR: literal carriage return found in data
HINT: Use "r" to represent carriage return.
CONTEXT: COPY acl_resources, line 6
3、导入PostgreSQL数据库
select 'copy '||table_name||' from
''/tmp/pv_split/'||table_name||'.dat'' with
DELIMITER ''|'';' from information_schema.tables
where table_schema='public' and
table_catalog='who_ebi' ;
4、导入后还要检查是否有乱码,数据丢失等现象
5、更改列属性,比如自增长、主键等,并建立相关索引
##MYSQL执行,生成创建序列SQL
SELECT CONCAT('create sequence
',table_name,'_',column_name,' cache
200;') FROM information_schema.columns
WHERE table_schema='who_tracking'
AND extra='auto_increment';
##将序列绑定具体字段
SELECT CONCAT('alter table ',table_name,' alter
column ',column_name,' set default
nextval('',table_name,'_',column_name,'');')
FROM information_schema.columns
WHERE table_schema='who_tracking'
AND extra='auto_increment';
##创建主键
SELECT CONCAT('alter table ',table_name,' add
primary key (',GROUP_CONCAT(column_name),');') FROM
information_schema.STATISTICS
WHERE table_schema='who_tracking'
AND index_name='PRIMARY'
GROUP BY table_name;
##创建相关索引
SELECT CONCAT('create ',CASE WHEN non_unique=1 THEN
'' WHEN non_unique=0 THEN ' unique ' END
,'index idx_',table_name,'_',column_name,' on
',table_name,'(',GROUP_CONCAT(column_name),');'
) FROM information_schema.STATISTICS
WHERE table_schema='who_tracking'
AND index_name<>'PRIMARY'
GROUP BY table_name;
[@more@]
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/23721637/viewspace-1059802/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/23721637/viewspace-1059802/