oracle迁移到pg全流程 1
postgres限制 3
DBA的概念映射 3
Oracle pg的数据类型映射 4
oracle迁移postgres转换 5
oracle与postgres对象 8
Schema 8
标识符 8
表 8
列 8
Constraint: 8
迁移方案 9
加载加速 9
业务逻辑存储过程迁移 9
12,条件触发器 10
13,储存过程 10
14,函数 10
15,Oracle postrges 储存过程迁移注意事项 10
16,package 11
17,synonyms 11
18,database links 11
19,connect by 11
20,物化视图 12
21,分区 12
22,sequence序列 12
024,集合操作 12
25,使用参数名进行函数调用 12
26,Dual 13
27,Rownum 13
28,rowid 13
迁移工具 13
1,Ora2pg特性介绍 14
2,oracle_fdw特性介绍 14
迁移工具使用 15
Ora2pg案例 15
安装oracle 略 15
安装pg 略 15
环境 15
依赖安装 16
1、安装perl依赖 16
2、 安装oracle-instantclient安装包 16
3、安装 DBI 17
4、安装 DBD-Oracle 17
5,DBD:PG安装 17
6、Ora2pg install 18
7、检查 18
8、配置ora2pg 19
9、查看oracle的表数据 19
10、导出表结构 20
11、查看表结构 21
12、更改ora2pg配置导出数据 22
13、导出表内容 22
14、导入表结构和数据 23
15、数据校验 24
16、连接Postgres 25
17、更多用法 创建迁移模板 25
Oracle_fdw案例(略) 26
特性Features: 26
oracle_fdw安装使用 27
下载 28
安装 28
登陆查看可用插件 28
查看oracle_fdw相关函数 28
使用案例 29
数据校验 30
oracle转postgres函数兼容 32
1 Connect by 32
2 ratio 35
3,eval 36
4,decode 36
5,Insert all 36
6,instr 38
7,rownum 40
8,synonym 匿名 43
9,order by INT position 44
10,timestamp + numeric 45
11,系统列(关键字、保留字)的处理 47
12,rowid 48
13,round interval 50
14,UUID 51
15,pipelined 52
oracle plsql迁移plpgsql 54
函数的教程 54
plpgsql存储过程的好处 55
PLpgSQL 转换 PL/SQL 55
oracle函数转pg案例 56
oracle 包函数 转 plpgsql的案例 60
plpgsql 储存过程检验 60
触发器 60
事件触发器 61
规则 61
参考 61
plpgsql官方教程 61
Oracle转pg的成本 61
后期运维差异 61
开发成本 61
运营成本以及收益 62
学习成本 62
Limit |
Value |
Maximum Database Size |
Unlimited |
Maximum Table Size |
32TB |
Maximum Row Size |
1.6TB |
Maximum Field Size |
1GB |
Maximum Rows/Table |
Unlimited |
Maximum Columns/Table |
250~1600 |
Maximum Indexes/Table |
Unlimited |
Postgres的架构和oracle对比
1,MVCC的原理实现有差异:
oracle采用rollback segment的方式实现
Postgres采用事务id xmin xmax的方式实现
2,SGA-> shared_buffers
3,PGA-> work_mem
4,PMON-> postmaster
5,TNS Listener -> postmaster
6,grant/revoke -> 几乎一样的语法
Oracle Type |
postgresL Type |
Comment |
Varchar、varchar2、nvarchar、nvarchar2 |
char, varchar, text |
|
char nchar |
char, varchar, text |
|
cblog, long |
Varchar,text,jsonb |
|
Number |
Bigint,int,small,real,double presion:性能很好,精度不好控制 Numeric:精度很高,性能略差 |
|
Binary_integer,binary_float, BINARY_DOUBLE |
Integer,float,numeric |
|
Blob,raw,log_raw |
Bytea 如果大对象是json可以换做jsonb |
|
Date |
Date or timestamp Timestamp with timezone |
|
Date 加减 |
Date + inteval ‘ N day/minute’ |
|
Nls_date_format |
To_char to_date |
|
TIMESTAMP |
date,timestamp, timestamptz, char,varchar, text |
|
TIMESTAMP WITH TIME ZONE |
date, timestamp, timestamptz, char, varchar, text |
|
TIMESTAMP WITH LOCAL TIME ZONE |
date, timestamp, timestamptz, char, varchar, text |
|
INTERVAL YEAR TO MONTH |
interval, char, varchar, text |
|
INTERVAL DAY TO SECOND |
interval, char, varchar, text |
|
MDSYS.SDO_GEOMETRY |
geometry (see "PostGIS support") |
项目 |
Oracle |
Postgres |
当前时间 |
SYSDATE |
now(),clock_timestamp(),current_time,current_date,current_time,current_timestamp,localtime,localtimestamp |
序列 |
SEQNAME.NEXTVAL |
NEXTVAL('SEQNAME') |
固定值列 |
SELECT '1' AS COL1 |
SELECT CAST('1' AS TEXT) as col |
NVL |
NVL函数 |
用COALESCE函数替换 |
INSTR函数 |
instr('str1','str2') |
strpos('str1','str2') |
外连接 |
Oracle可简写为(+) |
用LEFT JOIN等语句替换 |
层次查询 |
START WITH语句 CONNECT BY语句 |
用WITH RECURSIVE语句 |
数据库对象大小写 |
统一大写,””包起来的除外 |
统一小写,””包起来的除外 |
GOTO语句 |
GOTO语句 |
pgsql不支持 |
同义词 |
Oracle支持同义词 |
用视图代替 |
trunc |
trunc(时间) |
date_trunc() |
DUAL |
SELECT 1+1 FROM DUAL |
SELECT 1+1 或者 CREATE VIEW dual AS SELECT current_timestamp |
ROWNUM |
ROWNUM关键字 |
两种情况: 1.限制结果集数量,用于翻页等: SELECT * FROM T LIMIT 5 OFFSET 0 2.生成行号: ROW_NUMBER() OVER() |
DECODE等判断函数 |
DECODE() |
用标准的CASE WHEN THEN ELSE END语句替换 |
TO_CHAR |
TO_CHAR(COL,FMT),格式化字符串可以为空 |
TO_CHAR(COL1,'FM999999'),9的个数为字段长度,详细定义见: |
TO_NUMBER |
TO_NUMBER(COL,FMT),格式化字符串可以为空 |
TO_NUMBER(COL1,'999999'),9的个数为字段长度,详细定义见: http://www.postgresql.org/docs/10/static/functions-formatting.html |
NULL和'' |
ORACLE认为''等同于NULL,'a'||null 结果是'a' |
NULL和''不同,'a'||null 结果是null,用concat()函数替代 |
NULL和'' |
LENGTH('')为NULL |
LENGTH('')为0 |
NULL和'' |
TO_DATE('','YYYYMMDD')为空 |
TO_DATE('','YYYYMMDD')为0001-01-01 BC |
NULL和'' |
TO_NUMBER('',1)为NULL |
TO_NUMBER('',1),报错 |
ADD_MONTHS |
ADD_MONTHS(DATE,INT) |
CREATE FUNCTION add_months(date, int) RETURNS date AS 'SELECT ($1 +($2::text||'' month'')::interval)::date' LANGUAGE 'sql' 或SQL: SELECT ($1 +($2::text||' month')::interval) |
LAST_DAY |
LAST_DAY(DATE) |
创建函数来解决 CREATE OR REPLACE FUNCTION last_day(date) RETURNS date AS $$ SELECT (date_trunc('MONTH', $1) + INTERVAL '1 MONTH - 1 day')::date; $$ LANGUAGE 'sql'; 或SQL: SELECT (date_trunc('MONTH', $1) + interval '1 month - 1 day')::date; |
MONTHS_BETWEEN |
MONTHS_BETWEEN(DATE,DATE) |
创建函数来解决 CREATE FUNCTION MONTH_BETWEEN (d1 timestamp,d2 timestamp) RETURNS NUMERIC AS 'SELECT (extract(year from age(d1,d2))*12 + extract(month from age(d1,d2)))::integer' LANGUAGE 'sql'; |
BITAND |
BITAND(A,B) |
A & B |
MINUS |
MINUS语句 |
以EXCEPT语句来替代 |
BIN_ |
SELECT BIN_TO_NUM(1,0,1,0) AS VALUE1 FROM DUAL |
SELECT CAST(B'1010' AS INTEGER) AS VALUE1 |
UPDATE语句列列表 |
UPDATE accounts SET (contact_last_name, contact_first_name) = (SELECT last_name,first_name FROM salesmen WHERE salesmen.id =accounts.sales_id); |
UPDATE accounts a SET contact_last_name=blast_name, contact_first_name=b.first_name From salesmen b b.id =a.sales_id); |
SUBSTR函数 |
如果从第一个开始取子串,可以从0开始,也可以从1开始,如果不是第一个开始,则从1开始计数,可以为负值,从字符串结尾计数,用于取最后几位。 |
从1开始计数。如果要取最后几位,可以用RIGHT函数解决 |
子查询别名 |
子查询别名 |
必须有别名 |
列(别)名为关键字 |
Oracle中比如name,type这样的关键字可以直接作为列的别名,比如:select xx name from t |
需要加as,比如select xx as name from t |
当前登录用户 |
SELECT USER FROM DUAL |
select current_user |
ALL_COL_COMMENTS |
通过SELECT * FROM ALL_COL_COMMENTS可以获得列注释信息 |
select s.column_name as COLUMN_NAME, coalesce(col_description(c.oid,ordinal_position) ,s.column_name) as COMMENTS from information_schema.columns s,pg_class c where s.table_name = 'ac01_si' and s.table_name = c.relname and s.table_schema = current_schema() PG需要通过col_description获得列注释信息 |
修改表字段类型 |
1.如果字段无数据,可直接修改 |
1.如果新类型和原类型兼容,可直接修改 |
储存过程函数包 |
Function,procedure package |
pgsql不支持procedure和package,都需要改写成function,当package有全局变量的情况修改起来比较麻烦,我们是用临时表传递的。 |
cursor的属性 |
%FOUND %NOTFOUND %ISOPEN %ROWCOUNT |
%FOUND → found %NOTFOUND → not found %ISOPEN → pgsql不支持 %ROWCOUNT → pgsql不支持 另外关于cursor还发现了其他差异 |
oracle是按照每个用户为独立的schema,postgres是可以独立创建schema,和用户无关
Schema、表、列、函数、视图...
oracle的是大写,除非是双引号括起来
Postgres统一转换为小写,除非是双引号括起来
关键还是要保持一致
创建表一般都兼容,除了
Global temporay table
使用local temp 表
分区表
使用inherent trigger rule 和 check constraint pg_pathman
Initrans,maxextents 存储参数
删除他们
Pctfree: 使用fillfactor 填充因子
虚拟列:使用视图
数据类型:根据类型映射
主键、外键、唯一键、条件约束、非空约束 都支持
索引:
Btree/descending/ascending :pg都支持
Reverse key/bitmap/join:pg没实现
Partition:
Hash、List、range:都兼容 pg_pathman 或触发器实现 pg10自带分区功能
Tablespace:
原理不一样,但工作的效果是一样的
数据
如果类型转换顺利
数据类型字节长度大小正常
使用ETL方式
1,可以采用自定义导出到plain-text,csv固定分隔符的文件
2,采用copy from的方式加载
3,或者采用pg_bulkload的方式进行加载
加载加速
不要开启wal归档
数据导入完毕后在创建索引
唯一键和主键也可以考虑在导入完成后在创建
业务逻辑存储过程迁移
1,return 改为 returns
2,Execute immediate 改为 execute
3,select没有into该为 perform
4,选择一种储存过程语言
create or replace function fn(a inout) returns int as $$ declare ... begin ... end;$$language;
5,%type,%rowtype:能正常功能
6,cursor_name%rowtype:不工作,使用为类型 record
7,refcursors:没有替代方案,使用returning特性
8,匿名块:Postrges不支持
9,在事务中commit/rollback, pg11支持事务自治
10,reverse loop:可以采用调换start/end的条件解决
For i in reverse 1..10 loop
For i in reverse 10..1 loop
11,触发器
PostgreSQL: Documentation: 11: 43.10. Trigger Functions
改写为出发函数和触发器的方式解决
Create or replace function trg_fn() returns trigger as $$ ... $$ language xx;
Create trigger tbl_trg before update on table execute procedure trg_fn();
:NEW,:OLD
代表触发器使用时捕获的新值和旧值
Updating,insert -> 通过TG_OP;TG_*等变量获取
在before trigger记得返回return NEW;
12,条件触发器
达到某个条件才执行触发器
pg可以采用事件触发器
13,储存过程
postgres只有函数,采用returns void的返回值
14,函数
1,Return 改为returns
2,对于函数的空参数,需要提供双括号()
Create function fn() returns ...
3,默认值 default ,postgres支持
4,可以返回为类型record,但是调用者需要知道列的名字
5,可以返回set of record: returns setof type
oracle有table functions
PostgreSQL: Documentation: 9.6: Porting from Oracle PL/SQL
1,如果一个 SQL 命令中使用的名字可能是一个表的列名或者是对一个函数中变量的引用,那么PL/SQL会将它当作一个列名
2,在PostgreSQL中,函数体必须写成字符串文本。因此你需要使用美元符引用或者转义函数体中的单引号
3,数据类型名称常常需要翻译
4,应该用模式把函数组织成不同的分组,而不是用包
5,因为没有包,所以也没有包级别的变量。可以在临时表里保存会话级别的状态
6,带有REVERSE的整数FOR循环的工作方式不同:PL/SQL中是从第二个数向第一个数倒数,而PL/pgSQL是从第一个数向第二个数倒数,因此在移植时需要交换循环边界
7,查询上的FOR循环(不是游标)的工作方式同样不同:目标变量必须已经被声明,而PL/SQL总是会隐式地声明它们。但是这样做的优点是在退出循环后,变量值仍然可以访问
8,在使用游标变量方面,存在一些记法差异
16,package
1,一组变量,函数和储存过程
2,采用schema对函数分组
3,使用(临时)表替换包内的变量
4,对于private函数和变量,没有替代方案
5,包的初始代码,可以在每次调用函数调用一个初始函数
6,local function 函数里面递归调用函数
postgres不支持,采用正常的函数替换
17,synonyms
1,postgres不支持这个特性
采用视图解决或包装成函数
18,database links
1,不支持这个特性
2,采用dblink插件 和视图解决
19,connect by
采用with recursive by改写
20,物化视图
Postgres支持
21,分区
可以采用inherent 触发器 规则 条件约束 和constraint_exlusion pg_pathman来解决
22,sequence序列
1,和oracle一样的机制
2,nocache改为cache 1(或者remove这个参数)
3,maxvalue 9999999999999999999999999
减少限制 最大 9223372036854775807
4,.next,.currval
nextval(‘sequence’)
5,order/noorder
oracle需要这个做cluster/rac的设置
Postgres没有
6,no {cache|minvalue|maxvalue|cycle}
通no{*} 代替
nominvalue 改为 minvalue
23,关联语法
Postgres 提供{left|right|full|out} join oracle也提供
024,集合操作
UNION 并集
INTERSECT交集
EXCEPT 差集
25,使用参数名进行函数调用
=>改为 :=
var = fn(c=>10,a=>’xyz’,b=>2.5)
改为
var = fn(c := 10,a :=’xyz’,b:=2.5)
26,Dual
Orafce 兼容oracle相关函数
Oracle functionality (en) – PostgreSQL
Napište si debugger PL/pgSQL aneb pokročilé techniky programování v PostgreSQL – PostgreSQL
1,Orafce
很多兼容的功能
Dbms_alert
Dbms_pipe
Utl_file
Dbms_output
Dbms_random
Date operations
Dual
To_char() 支持多不同的数据类型
(需要安装插件包,虽然可以兼容,但更建议直接改写,减少依赖)
27,Rownum
Row_number()窗口函数
PostgreSQL: Documentation: 11: 9.21. Window Functions
28,rowid
使用ctid系统列
不能用作分区键,空间回收ctid会变化
使用oid列
1,Ora2pg
2,oracle_fdw
Features included:
- Export full database schema (tables, views, sequences, indexes), with
unique, primary, foreign key and check constraints.
- Export grants/privileges for users and groups.
- Export range/list partitions andi sub partitions.
- Export a table selection (by specifying the table names).
- Export Oracle schema to a PostgreSQL 8.4+ schema.
- Export predefined functions, triggers, procedures, packages and
package bodies.
- Export full data or following a WHERE clause.
- Full support of Oracle BLOB object as PG BYTEA.
- Export Oracle views as PG tables.
- Export Oracle user defined types.
- Provide some basic automatic conversion of PLSQL code to PLPGSQL.
- Works on any plateform.
- Export Oracle tables as foreign data wrapper tables.
- Export materialized view.
- Show a detailled report of an Oracle database content.
- Migration cost assessment of an Oracle database.
- Migration difficulty level assessment of an Oracle database.
- Migration cost assessment of PL/SQL code from a file.
- Migration cost assessment of Oracle SQL queries stored in a file.
- Generate XML ktr files to be used with Penthalo Data Integrator (Kettle)
- Export Oracle locator and spatial geometries into PostGis.
- Export DBLINK as Oracle FDW.
- Export SYNONYMS as views.
- Export DIRECTORY as external table or directory for external_file extension.
- Full MySQL export just like Oracle database.
- Dispatch a list of SQL orders over multiple PostgreSQL connections
- Perform a diff between Oracle and PostgreSQL database for test purpose.
特性Features:
1,Uses the standard compliant SQL/MED environment of PostgreSQL 9.1 and above
2,Supports translation of Oracle data types to similar PostgreSQL data types
3,WHERE conditions and ORDER BY expressions are propagated to Oracle where possible
4,Only the required Oracle table columns are fetched
5,EXPLAIN shows the remote query, EXPLAIN VERBOSE the Oracle execution plan
6,Should compile and run on all platforms supported by Oracle Client and PostgreSQL
7,Works with the regular Oracle client and Oracle Instant Client
8,Installable with a single CREATE EXTENSION command
9,Allows foreign tables based on arbitrary Oracle queries
10,Oracle connections are cached for the duration of the PostgreSQL session
11,Supports Oracle external authentication to avoid storing passwords in the database
12,Uses Oracle prefetching for high performance
13,Supports gathering statistics with ANALYZE from PostgreSQL 9.2 on
14,Supports INSERT, UPDATE and DELETE from PostgreSQL 9.3 on
15,Efficient mapping between MDSYS.SDO_GEOMETRY and PostGIS geometry
16,Supports IMPORT FOREIGN SCHEMA from PostgreSQL 9.5 on
17,Propagates 2-way inner joins between foreign tables to Oracle from PostgreSQL 9.6 on
https://github.com/darold/ora2pg
Ora2Pg : Migrates Oracle to PostgreSQL
[postgres@DD_DB2 ~]$ cat /etc/redhat-release
CentOS release 6.5 (Final)
[postgres@DD_DB2 ~]$ uname -a
Linux DD_DB2 2.6.32-573.22.1.el6.x86_64 #1 SMP Wed Mar 23 03:35:39 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
1、安装perl依赖
yum install -y perf cpan perl-Time-HiRes
oracle官网下载basic、devel、sqlplus三个rpm包。
http://www.oracle.com/technetwork/database/database-technologies/instant-client/downloads/index.html
直接下载
http://download.oracle.com/otn/linux/instantclient/122010/oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpm
http://download.oracle.com/otn/linux/instantclient/121020/oracle-instantclient12.1-devel-12.1.0.2.0-1.x86_64.rpm
http://download.oracle.com/otn/linux/instantclient/122010/oracle-instantclient12.2-sqlplus-12.2.0.1.0-1.x86_64.rpm
安装
rpm -ivh oracle-instantclient*.rpm
rpm -ivh *.rpm
Preparing... ########################################### [100%]
package oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64 is already installed
package oracle-instantclient12.2-devel-12.2.0.1.0-1.x86_64 is already installed
package oracle-instantclient12.2-sqlplus-12.2.0.1.0-1.x86_64 is already installed
[root@10-0-98-60 ~]# echo "/usr/lib/oracle/12.2/client64/lib" > /etc/ld.so.conf.d/oracle_client.conf
[root@10-0-98-60 ~]# ldconfig
[root@10-0-98-60 ~]#
[root@10-0-98-60 ~]# ldconfig -p|grep oracle
libsqlplusic.so (libc6,x86-64) => /usr/lib/oracle/12.2/client64/lib/libsqlplusic.so
libsqlplus.so (libc6,x86-64) => /usr/lib/oracle/12.2/client64/lib/libsqlplus.so
liboramysql12.so (libc6,x86-64) => /usr/lib/oracle/12.2/client64/lib/liboramysql12.so
libons.so (libc6,x86-64) => /usr/lib/oracle/12.2/client64/lib/libons.so
libocijdbc12.so (libc6,x86-64) => /usr/lib/oracle/12.2/client64/lib/libocijdbc12.so
libociei.so (libc6,x86-64) => /usr/lib/oracle/12.2/client64/lib/libociei.so
libocci.so.12.1 (libc6,x86-64) => /usr/lib/oracle/12.2/client64/lib/libocci.so.12.1
libocci.so (libc6,x86-64) => /usr/lib/oracle/12.2/client64/lib/libocci.so
libnnz12.so (libc6,x86-64) => /usr/lib/oracle/12.2/client64/lib/libnnz12.so
libmql1.so (libc6,x86-64) => /usr/lib/oracle/12.2/client64/lib/libmql1.so
libipc1.so (libc6,x86-64) => /usr/lib/oracle/12.2/client64/lib/libipc1.so
libclntshcore.so.12.1 (libc6,x86-64) => /usr/lib/oracle/12.2/client64/lib/libclntshcore.so.12.1
libclntshcore.so (libc6,x86-64) => /usr/lib/oracle/12.2/client64/lib/libclntshcore.so
libclntsh.so.12.1 (libc6,x86-64) => /usr/lib/oracle/12.2/client64/lib/libclntsh.so.12.1
libclntsh.so (libc6,x86-64) => /usr/lib/oracle/12.2/client64/lib/libclntsh.so
测试 sqlplus username/password@ip:port/sid
sqlplus64 scott/[email protected]:1521/orcl
3、安装 DBI
cpan install DBI
4、安装 DBD-Oracle
export ORACLE_HOME=/usr/lib/oracle/12.2/client64
export PATH=$ORACLE_HOME/bin:$PATH
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
wget http://search.cpan.org/CPAN/authors/id/P/PY/PYTHIAN/DBD-Oracle-1.74.tar.gz
tar -zxvf DBD-Oracle-1.74.tar.gz
cd DBD-Oracle-1.74
perl Makefile.PL -l
make && make test
make install
5,DBD:PG安装
下载
http://search.cpan.org/~turnstep/DBD-Pg/
wget http://www.cpan.org/authors/id/T/TU/TURNSTEP/DBD-Pg-3.7.4.tar.gz
tar -zxf DBD-Pg-3.7.4.tar.gz
cd DBD-Pg-3.7.4
perl Makefile.PL
make
make install
history
6、Ora2pg install
wget https://github.com/darold/ora2pg/archive/v18.2.tar.gz
tar -zxvf v18.2.tar.gz
cd ora2pg-18.2/
perl Makefile.PL
make && make install
perl -MCPAN -e shell
cpan> get DBD::mysql
cpan> quit
cd ~/.cpan/build/DBD-mysql*
perl Makefile.PL
make
make install
7、检查
cat check.pl
#!/usr/bin/perl
use strict;
use ExtUtils::Installed;
my $inst=ExtUtils::Installed->new();
my @modules = $inst->modules();
foreach(@modules){
my $ver = $inst->version($_) || "???";
printf("%-12s -- %s\n",$_,$ver);
}
perl check.pl
DBD::Oracle -- 1.74
DBD::Pg -- 3.7.4
DBD::mysql -- 4.046
DBI -- 1.641
Ora2Pg -- 18.2
Perl -- 5.10.1
Test::Simple -- 1.302136
8、配置ora2pg
复制
cp /etc/ora2pg/ora2pg.conf.dist /etc/ora2pg/ora2pg.conf
编辑导出配置文件
cd /etc/ora2pg/
cat ora2pg.conf
ORACLE_HOME /usr/local/oracle/product/11.2.0/db_1
#Set Oracle database connection (data source, user, password)
ORACLE_DSN dbi:Oracle:host=10.0.98.60;sid=orcl
ORACLE_USER scott
ORACLE_PWD tiger
SCHEMA scott
DEBUG 1
ORA_INITIAL_COMMAND
EXPORT_SCHEMA 0
CREATE_SCHEMA 1
COMPILE_SCHEMA 0
TYPE TABLE #导出标结果
OUTPUT output.sql #导出的文件名
9、查看oracle的表数据
SQL> show user;
USER is "SCOTT"
SQL> select SYS_CONTEXT('USERENV','CURRENT_SCHEMA') CURRENT_SCHEMA from dual;
CURRENT_SCHEMA
--------------------------------------------------------------------------------
SCOTT
SQL> select table_name,tablespace_name from user_tables;
TABLE_NAME TABLESPACE_NAME
------------------------------ ------------------------------
DEPT USERS
EMP USERS
BONUS USERS
SALGRADE USERS
TMP USERS
TEST USERS
T1 USERS
10、导出表结构
[oracle@10-0-98-60 ora2pg]$ ora2pg -c ora2pg.conf
Ora2Pg version: 18.2
Trying to connect to database: dbi:Oracle:host=10.0.98.60;sid=orcl
Isolation level: SET TRANSACTION ISOLATION LEVEL READ COMMITTED
Looking forward functions declaration in schema SCOTT.
Retrieving table information...
[1] Scanning table BONUS (0 rows)...
[2] Scanning table DEPT (4 rows)...
[3] Scanning table EMP (14 rows)...
[4] Scanning table SALGRADE (5 rows)...
[5] Scanning table T1 (1 rows)...
[6] Scanning table TEST (1 rows)...
[7] Scanning table TMP (1 rows)...
Dumping table T1...
Dumping table TEST...
Dumping table DEPT...
Dumping table BONUS...
Dumping table EMP...
Dumping table SALGRADE...
Dumping table TMP...
Dumping RI EMP...
问题
[oracle@10-0-98-60 ora2pg]$ ora2pg -c ora2pg.conf
Ora2Pg version: 18.2
Trying to connect to database: dbi:Oracle:host=10.0.98.60;sid=orcl
Isolation level: SET TRANSACTION ISOLATION LEVEL READ COMMITTED
Looking forward functions declaration in schema SCOTT.
DBD::Oracle::db prepare failed: ORA-00942: table or view does not exist (DBD ERROR: error possibly near
解决方案
oracle授予用户DBA权限
SQL> grant dba to scott;
Grant succeeded.
11、查看表结构
自动做了表结构数据类型转换
[oracle@10-0-98-60 ora2pg]$ more output.sql
-- Generated by Ora2Pg, the Oracle database Schema converter, version 18.2
-- Copyright 2000-2017 Gilles DAROLD. All rights reserved.
-- DATASOURCE: dbi:Oracle:host=10.0.98.60;sid=orcl
SET client_encoding TO 'UTF8';
\set ON_ERROR_STOP ON
SET check_function_bodies = false;
CREATE TABLE dept (
deptno smallint NOT NULL,
dname varchar(14),
loc varchar(13)
) ;
ALTER TABLE dept ADD PRIMARY KEY (deptno);
CREATE TABLE bonus (
ename varchar(10),
job varchar(9),
sal bigint,
comm bigint
) ;
CREATE TABLE emp (
empno smallint NOT NULL,
ename varchar(10),
job varchar(9),
mgr smallint,
hiredate timestamp,
sal decimal(7,2),
comm decimal(7,2),
deptno smallint
) ;
ALTER TABLE emp ADD PRIMARY KEY (empno);
12、更改ora2pg配置导出数据
cat ora2pg.conf
ORACLE_HOME /usr/local/oracle/product/11.2.0/db_1
#Set Oracle database connection (data source, user, password)
ORACLE_DSN dbi:Oracle:host=10.0.98.60;sid=orcl
ORACLE_USER scott
ORACLE_PWD tiger
SCHEMA scott
DEBUG 1
ORA_INITIAL_COMMAND
EXPORT_SCHEMA 0
CREATE_SCHEMA 1
COMPILE_SCHEMA 0
TYPE DATA #导出数据内容
OUTPUT data.sql #导出的数据文件名
13、导出表内容
[oracle@10-0-98-60 ora2pg]$ ora2pg -c ora2pg.conf
Ora2Pg version: 18.2
Trying to connect to database: dbi:Oracle:host=10.0.98.60;sid=orcl
Isolation level: SET TRANSACTION ISOLATION LEVEL READ COMMITTED
Looking forward functions declaration in schema SCOTT.
Retrieving table information...
[1] Scanning table BONUS (0 rows)...
[2] Scanning table DEPT (4 rows)...
[3] Scanning table EMP (14 rows)...
[4] Scanning table SALGRADE (5 rows)...
[5] Scanning table T1 (1 rows)...
[6] Scanning table TEST (1 rows)...
[7] Scanning table TMP (1 rows)...
Trying to connect to database: dbi:Oracle:host=10.0.98.60;sid=orcl
Isolation level: SET TRANSACTION ISOLATION LEVEL READ COMMITTED
Retrieving partitions information...
Looking how to retrieve data from BONUS...
Fetching all data from BONUS tuples...
DEBUG: Formatting bulk of 10000 data for PostgreSQL.
DEBUG: Creating output for 10000 tuples
Dumping data from BONUS to file: data.sql
Extracted records from table BONUS: total_records = 0 (avg: 0 recs/sec)
[> ] 0/26 total rows (0.0%) - (0 sec., avg: 0 recs/sec).
Looking how to retrieve data from DEPT...
Fetching all data from DEPT tuples...
DEBUG: Formatting bulk of 10000 data for PostgreSQL.
DEBUG: Creating output for 10000 tuples
Dumping data from DEPT to file: data.sql
Extracted records from table DEPT: total_records = 4 (avg: 4 recs/sec)
....
[========================>] 51/26 total rows (196.2%) - (1 sec., avg: 51 recs/sec).
Looking how to retrieve data from TMP...
Fetching all data from TMP tuples...
DEBUG: Formatting bulk of 10000 data for PostgreSQL.
DEBUG: Creating output for 10000 tuples
Dumping data from TMP to file: data.sql
Extracted records from table TMP: total_records = 14 (avg: 14 recs/sec)
[========================>] 65/26 total rows (250.0%) - (1 sec., avg: 65 recs/sec).
Restarting sequences
14、导入表结构和数据
导入表结构
[oracle@10-0-98-60 ora2pg]$ psql -h 127.0.0.1 -U postgres < output.sql
SET
SET
CREATE TABLE
CREATE TABLE
CREATE TABLE
ALTER TABLE
CREATE TABLE
CREATE TABLE
ALTER TABLE
CREATE TABLE
CREATE TABLE
ALTER TABLE
导入数据
[oracle@10-0-98-60 ora2pg]$ psql -h 127.0.0.1 -U postgres < data.sql
SET
SET
BEGIN
INSERT 0 1
INSERT 0 1
...
INSERT 0 1
INSERT 0 1
COMMIT
15、数据校验
oracle
SQL> select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
postgres
postgres=# select * from emp;
empno | ename | job | mgr | hiredate | sal | comm | deptno
-------+--------+-----------+------+---------------------+---------+---------+--------
7369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | | 20
7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30
7521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30
7566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | | 20
7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30
7698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | | 30
7782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | | 10
7788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000.00 | | 20
7839 | KING | PRESIDENT | | 1981-11-17 00:00:00 | 5000.00 | | 10
7844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30
7876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | | 20
7900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950.00 | | 30
7902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | | 20
7934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300.00 | | 10
(14 rows)
16、连接Postgres
PG_DSN dbi:Pg:dbname=postgres;host=10.0.98.60;port=5432
PG_USER postgres
PG_PWD 123456
OUTPUT output.sql
17、更多用法 创建迁移模板
创建迁移模板需要 --project_base and --init_project 这两个参数
ora2pg --project_base /app/migration/ --init_project test_project
Creating project test_project.
/app/migration/test_project/
schema/
dblinks/
directories/
functions/
grants/
mviews/
packages/
partitions/
procedures/
sequences/
synonyms/
tables/
tablespaces/
triggers/
types/
views/
sources/
functions/
mviews/
packages/
partitions/
procedures/
triggers/
types/
views/
data/
config/
reports/
Generating generic configuration file
Creating script export_schema.sh to automate all exports.
Creating script import_all.sh to automate all imports.
全流程文档
各种数据库迁移到PostgreSQL及PG维保、紧急救援及商业服务,请扫码联系。