# 使用 ora2pg 将oracle 数据导入 postgresql ## 安装edb 准备 edb 安装过程看 [Centos搭建edb9.5.md](./Centos搭建edb9.5.md) ```sh PGSETUP_INITDB_OPTIONS="-E UTF-8 -D /home/edbdata" /usr/edb/as14/bin/edb-as-14-setup initdb #EDB12初始初始化 PGSETUP_INITDB_OPTIONS="-E UTF-8 -D /home/edbdata" /usr/edb/as12/bin/edb-as-12-setup initdb ``` ##### 启动 edb ```sh systemctl start edb-as-14 ``` ##### 连接edb ```sh sudo su - enterprisedb psql edb ``` ### 常见问题 - 远程连接数据库失败 修改文件 data/pg_hba.conf 添加 ``` host all all 0.0.0.0/0 md5 ``` - 修改数据库密码(在edb 库执行) ``` ALTER USER enterprisedb WITH PASSWORD 'yiing&654'; ``` ## 安装 ora2pg ### 安装 perl 环境 ```sh yum install -y perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker yum -y install perl-CPAN ``` **安装 perl 依赖** ```sh perl -MCPAN -e 'install DBD::Oracle' perl -MCPAN -e 'install DBI' perl -MCPAN -e 'install DBD::Pg' ``` **常见问题** ``` Can't locate Test/NoWarnings.pm in @INC (@INC contains: t/lib /root/.cpan/build/DBD-Oracle-1.83-LDK8k0/blib/lib /root/.cpan/build/DBD-Oracle-1.83-LDK8k0/blib/arch /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at t/26exe_array.t line 15. ``` 安装 perl-Test-NoWarnings-1.04-2.el7.noarch 解决 提示测试不通过 手动下载源码 安装 下载地址:https://metacpan.org/release/DBI ```sh tar -zxvf DBI-1.642.tar.gz cd DBI-1.642 perl Makefile.PL make make install ``` ## 安装Oracle客户端 官网下载 Oracle Instant Client 等包 上传服务器安装 ```sh rpm -ivh oracle-instantclient19.15-basic-19.15.0.0.0-1.x86_64.rpm rpm -ivh oracle-instantclient19.15-devel-19.15.0.0.0-1.x86_64.rpm rpm -ivh oracle-instantclient19.15-jdbc-19.15.0.0.0-1.x86_64.rpm rpm -ivh oracle-instantclient19.15-sqlplus-19.15.0.0.0-1.x86_64.rpm ``` **设置环境变量**ORACLE_HOME ```sh export ORACLE_HOME=/usr/lib/oracle/19.15/client64/ ``` **本地已安装有**Oracle数据库 ORACLE_HOME如下设置 ```sh export ORACLE_HOME=/opt/oracle/product/19c/dbhome_1 export LD_LIBRARY_PATH=$ORACLE_HOME/lib ``` 按照自己 的路径设置 环境变量 例如: ```sh export POSTGRES_HOME=/usr/edb/as14 export POSTGRES_HOME=/var/lib/edb/as14 ``` ## 安装ora2pg 下载软件包 https://github.com/darold/ora2pg/releases 默认安装在`/usr/local/bin/` 目录下 ``` [root@Test01 ~]# tar -xjf ora2pg-20.0.tar.bz2 [root@Test01 ~]# cd ora2pg-xx/ [root@Test01 ~]# perl Makefile.PL PREFIX= [root@Test01 ora2pg-18.2]# make && make install ``` **简单检查** ora2pg --help ## 迁移数据 **初始化项目** ```sh //初始化项目 ora2pg --init_project ora2pg ``` **配置配置文件** 默认配置文件即可 需要修改 ``` # Set Oracle database connection (datasource, user, password) ORACLE_DSN dbi:Oracle:host=dev-db.yldev.net;sid=hcc;port=1521 ORACLE_USER icc ORACLE_PWD ICCICC # Oracle schema/owner to use SCHEMA ICC # Set which object to export from. By default Ora2Pg export all objects. # Value must be a list of object name or regex separated by space. Note # that regex will not works with 8i database, use % placeholder instead # Ora2Pg will use the LIKE operator. There is also some extended use of # this directive, see chapter "Limiting object to export" in documentation. # 可以使用正则 如 t_.* 可以排除 不想导出的对象 如!t_t2 #配置中文释义 https://blog.csdn.net/Loiterer_Y/article/details/110927688 #ALLOW TABLE_TEST # The value can be a comma delimited list of schema but not when using TABLE # export type because in this case it will generate the CREATE SCHEMA statement # and it doesn't support multiple schema name. For example, if you set PG_SCHEMA # to something like "user_schema, public", the search path will be set like this # SET search_path = user_schema, public; # forcing the use of an other schema (here user_schema) than the one from Oracle # schema set in the SCHEMA directive. You can also set the default search_path # for the PostgreSQL user you are using to connect to the destination database # by using: # ALTER ROLE username SET search_path TO user_schema, public; #in this case you don't have to set PG_SCHEMA. #PG_SCHEMA LLOW PG_SCHEMA icc ``` **测试连接** 执行以下命令 返回oracle 版本则连接成功 ``` ora2pg -t SHOW_VERSION -c config/ora2pg.conf ``` **常用命令** ``` ora2pg -t SHOW_VERSION -c config/ora2pg.conf ora2pg -t SHOW_SCHEMA -c config/ora2pg.conf ora2pg -t SHOW_TABLE -c config/ora2pg.conf ora2pg -t SHOW_COLUMN -c config/ora2pg.conf ora2pg -t SHOW_ENCODING -c config/ora2pg.conf ``` **安装 screen** 先安装 screen 防止意外断开 导出失败 ```sh yum install -y screen ``` screen 常用命令 ``` //开启一个screen screen -S name //进入一个 screen screen -r name // screen 列表 screen -ls 可以用快捷键 Ctrl+a d(即按住 Ctrl,依次再按 a,d),而会话中的程序不会关闭 ``` #### **导出数据** 该命令根据数据库大小 表大小等待时间不一 建议使用screen 运行 ``` ora2pg -t COPY -o data.sql -b ./data -c ./config/ora2pg.conf ``` **修改校验表结构** 导出完成后 在 `./schema/tables` 下有转换好的表结构(主要修改关键字段) ``` oid tableoid xmin cmin xmax cmax ctid ``` #### 导入数据库 切换到`enterprisedb` 用户 执行 `sh import_all.sh` 按照提示导入数据 ## 附录 **修改用户组 权限** ``` chown [-R] ownname:groupname filename chmod -R 777 filename ``` **追踪服务日志** ```sh journalctl -f -u icc-analysis.service ``` **查看硬盘大小** ```sh df -hl df -h path ls -lht du -ah --max-depth=1 /path #查看删除的文件 lsof -n | grep deleted du -bsh / ```