Oracle PostgreSQL ora2pg

使用 Ora2pg 將 Oracle 遷移至 PostgreSQL

周志鴻 Vincent Chou 2023/03/08 04:41:24
1239
簡介
 
  • 本文說明如何使用免費的 ora2pg 工具,將 Oracle 資料庫遷移至 PostgreSQL。
 
 
參考網站
 
 
實作環境
 
實作步驟
 
1.首先須安裝PostgreSQL、Strawberry Perl、Git
 
 
 
2.安裝好後需將 <安裝路徑\PostgreSQL\14\bin\>  &  <安裝路徑\Git\bin\> 加入windows path環境變數才能在命令列執行psql、sh指令,如下圖
 

 

 

3.安裝好後開啟Prel command line 輸入下列兩個指令安裝DBD套件
 
 
perl -MCPAN -e install DBD::Oracle
 
perl -MCPAN -e install DBD::Pg
 
 
 
4.在Ora2pg下載頁面下載最新版SourceCode.zip
 

 

 

5.解壓縮到 安裝資料夾 後,在命令列使用 cd 指令至該資料夾後執行下面指令
 
perl Makefile.PL

 

 
 
6.完成後再執行下面指令
 
gmake && gmake install

 

 
 
7.接下來下載DBI,網站左方有下載點
 
 
 
解壓縮後在命令列至該資料夾下執行指令
 
perl Makefile.PL

 

 
 
8.完成後再執行下面指令進行安裝
 
gmake && gmake install

 

 

 

9.先查詢ORACLE_HOME路徑,建立專案&參數設定需要用到,可在Oracle SQL Plus 下指令查詢
 
var OHM varchar2(100);
EXEC dbms_system.get_env('ORACLE_HOME', :OHM) ;
PRINT OHM
 
 
 
 
10.在要建立專案的位置,輸入下列指令建立遷移專案
 
ora2pg --project_base <ORACLE_HOME> --init_project <Project Name>

 

 

建立的專案資料夾架構如下圖

 

 
 
 
11.接下來開始寫遷移腳本,檔案在 專案位置\config\ora2pg.conf,建議原始檔案複製起來,新增一個新的檔案,簡易範例如下
 
ORACLE_HOME               <ORACLE_HOME>
ORACLE_DSN                   dbi:Oracle:host=<IP or localhost>;service_name=<Name>;port=1521    #可用service_name or sid
ORACLE_USER                <Oracle帳號>
ORACLE_PWD                  <Oracle密碼>
EXPORT_SCHEMA            1
SCHEMA                             <Oracle Schema>
USER_GRANTS                 1
TYPE                                  TABLE
PG_VERSION                    14
PG_DSN                             dbi:Pg:dbname=<DB Name>;host=<Pg ip>;port=5432
PG_USER                          <Pg帳號>
PG_PWD                            <Pg密碼>
PG_SCHEMA                     <Pg Schema>
 
 
 
12.寫好後即可測試Oracle是否正常連線,輸入下列指令, 出現Oracle版本即為連線成功
 
ora2pg -t SHOW_VERSION -c .\config\ora2pg.conf
 
 
 
 
13.可用下列指令可以產生遷移報告,來看遷移難易度
 
ora2pg -t SHOW_REPORT -c  .\config\ ora2pg.conf --estimate_cost --dump_as_html > migration_report.html

 

 
下圖為報表內容,會顯示 Oracle 資料庫 Object 數量及遷移難易度
 
 
 
 
14.使用  PowerShell  執行專案資料夾裡 export_schema.ps1,可一次匯出 Data 以外的 Schema 所有內容,匯出的檔案會存在Schema資料夾
 
.\export_schema.ps1

 

 
 
15.接下來匯入所有Schema及Data,使用 PowerShell 輸入以下指令 才會正常顯示互動介面,Data為直接連資料庫匯入
 
sh import_all.sh -d <Pg Database> -o <Onwer> -U <User> -h <Pg ip>
 
 
 
16.如要匯出單一TYPE,可執行下列指令匯出檔案
 
ora2pg -c  .\config\ ora2pg.conf -t <Type> -o <FileName.sql>
 
 
#Type清單
 
- TABLE: Extract all tables with indexes, primary keys, unique keys, foreign keys and check constraints.
- VIEW: Extract only views.
- GRANT: Extract roles converted to Pg groups, users and grants on all objects.
- SEQUENCE: Extract all sequence and their last position.
- TABLESPACE: Extract storage spaces for tables and indexes (Pg >= v8).
- TRIGGER: Extract triggers defined following actions.
- FUNCTION: Extract functions.
- PROCEDURE: Extract procedures.
- PACKAGE: Extract packages and package bodies.
- INSERT: Extract data as INSERT statement.
- COPY: Extract data as COPY statement.
- PARTITION: Extract range and list Oracle partitions with subpartitions.
- TYPE: Extract user defined Oracle type.
- FDW: Export Oracle tables as foreign table for oracle_fdw.
- MVIEW: Export materialized view.
- QUERY: Try to automatically convert Oracle SQL queries.
- KETTLE: Generate XML ktr template files to be used by Kettle.
- DBLINK: Generate oracle foreign data wrapper server to use as dblink.
- SYNONYM: Export Oracle's synonyms as views on other schema's objects.
- DIRECTORY: Export Oracle's directories as external_file extension objects.
- LOAD: Dispatch a list of queries over multiple PostgreSQl connections.
- TEST: perform a diff between Oracle and PostgreSQL database.
- TEST_COUNT: perform a row count diff between Oracle and PostgreSQL table.
- TEST_VIEW: perform a count on both side of number of rows returned by views.
- TEST_DATA: perform data validation check on rows at both sides.

 

若conf裡有設定好參數的話可以直接執行

ora2pg -c  .\config\ ora2pg.conf

 

 

匯出的檔案為.sql檔

 
 
17.若要將單一 SQL檔案 匯入 本機 PostgreSQL ,可用下列指令
 
psql (DB Name) < (File Name).sql (Pg Account)
 
 
如要匯入到其它主機的PostgreSQL,可改為下列 指令
 
psql -U (Account) -d (DbName) -h (ip) -p 5432 < (FileName.sql)

 

 

 

18.以上為簡易 Oracle Migration 至 PostgreSQL 教學,詳細指令及參數設定待下篇講解,或請參考ora2pg網站

周志鴻 Vincent Chou