项目 |
Oracle源库 |
PG目标库 |
db 类型 |
单实例 |
单实例 |
db version |
12.2.0 |
15.2 |
OS版本 |
CentOS7.9 64位 |
CentOS7.9 64位 |
OS hostname |
tencent |
tencent |
IP地址 |
10.0.4.16 |
10.0.4.16 |
dbname |
orcl |
orcl |
监听端口 |
1521 |
4519 |
SOFT_HOME |
/oracle/app/oracle/product/12.2.0/db_1 |
/postgresql/pg15 |
安装用户 |
oracle |
postgres |
同步用户 |
hr |
hr |
软件安装:
主机 |
需要的软件 |
源端(10.0.4.16) |
Oracle 12c database |
目标端(10.0.4.16) |
PG 15.2、ora2pg软件 |
使用oracle 模版下的hr schema数据。
查看hr 下对象及对象类型
SQL> SELECT a.table_name,a.num_rows FROM dba_tables a where a.OWNER='HR';
TABLE_NAME NUM_ROWS
-------------------------------------------------- ----------
REGIONS 4
LOCATIONS 23
DEPARTMENTS 27
JOBS 19
EMPLOYEES 107
JOB_HISTORY 10
ORDER_TABLE 500000
DEMO 0
COUNTRIES 25
9 rows selected.
SQL> select object_type,count(*) from dba_objects where owner='HR' group by object_type;
OBJECT_TYPE COUNT(*)
----------------------- ----------
SEQUENCE 4
PROCEDURE 5
TRIGGER 2
INDEX 19
TABLE 9
VIEW 1
6 rows selected.
SQL> select sum(bytes)/1024/1024 from dba_segments where owner='HR';
SUM(BYTES)/1024/1024
--------------------
54.625
创建存储过程
--用户授权
conn / as sysdba
GRANT dba to hr;
GRANT CREATE ANY TABLE, CREATE ANY VIEW ,CREATE ANY PROCEDURE TO hr;
GRANT ALTER ANY TABLE, ALTER ANY PROCEDURE TO hr;
GRANT DROP ANY TABLE, DROP ANY VIEW, SELECT ANY TABLE, INSERT ANY TABLE, UPDATE ANY TABLE, DELETE ANY TABLE TO hr;
创建存储过程
CREATE OR REPLACE PROCEDURE sp_generate_report_and_diff_and_highest_paid_emp
AS
-- 定义游标变量
CURSOR c_emp IS
SELECT e.first_name,
e.last_name,
e.job_id,
d.department_name,
e.salary
FROM employees e
INNER JOIN departments d ON e.department_id = d.department_id;
BEGIN
-- 打印表头信息
DBMS_OUTPUT.PUT_LINE('EMPLOYEE REPORT');
DBMS_OUTPUT.PUT_LINE('----------------');
-- 创建员工报告表
EXECUTE IMMEDIATE 'CREATE TABLE emp_report AS
SELECT emp.first_name,
emp.last_name,
emp.job_id,
dept.department_name,
emp.salary
FROM employees emp
INNER JOIN departments dept ON emp.department_id = dept.department_id';
-- 创建员工薪资差异表
EXECUTE IMMEDIATE 'CREATE TABLE emp_salary_diff AS
SELECT emp.employee_id,
emp.first_name,
emp.last_name,
emp.job_id,
emp.salary,
(emp.salary - AVG(emp.salary) OVER(PARTITION BY emp.job_id)) AS salary_diff
FROM employees emp';
-- 创建薪水最高员工表
EXECUTE IMMEDIATE 'CREATE TABLE highest_paid_emp AS
SELECT emp.employee_id,
emp.first_name,
emp.last_name,
emp.job_id,
dept.department_name,
emp.salary
FROM employees emp
INNER JOIN departments dept ON emp.department_id = dept.department_id
WHERE emp.salary = (SELECT MAX(salary) FROM employees)';
-- 打印表尾信息
DBMS_OUTPUT.PUT_LINE('----------------');
DBMS_OUTPUT.PUT_LINE('END OF REPORT');
END;
/
执行存储过程
SQL> exec sp_generate_report_and_diff_and_highest_paid_emp
2.1.1、Ora2Pg 简介
Ora2Pg 是一个免费的工具,用于将 Oracle 或MySQL数据库迁移到 PostgreSQL 数据库里。它连接到 Oracle或MySQL 数据库,自动扫描并抽取其结构和数据,然后生成用于实现迁移的SQL 脚本,利用该脚本可以将数据库结构和数据加载到 PostgreSQL 数据库中。
2.1.2、ora2pg 时允许指定的所有命令行参数
Usage: ora2pg [-dhpqv --estimate_cost --dump_as_html] [--option value]
-a | --allow str : 指定允许导出的对象列表,使用逗号分隔。也可以与 SHOW_COLUMN 选项一起使用。
-b | --basedir dir: 设置默认的导出目录,用于存储导出结果。
-c | --conf file : 设置非默认的配置文件,默认配置文件为 /etc/ora2pg/ora2pg.conf。
-d | --debug : 使用调试模式,输出更多详细信息。
-D | --data_type STR : 通过命令行设置数据类型转换。
-e | --exclude str: 指定导出时排除的对象列表,使用逗号分隔。也可以与 SHOW_COLUMN 选项一起使用。
-h | --help : 显示帮助信息。
-g | --grant_object type : 导出指定类型的对象上的授权信息,取值参见 GRANT_OBJECT 配置项。
-i | --input file : 指定要导入的 Oracle PL/SQL 代码文件,导入文件时不需要连接到 Oracle 数据库。
-j | --jobs num : 设置用于发送数据到 PostgreSQL 的并发进程数量。
-J | --copies num : 设置用于从 Oracle 导出数据的并发连接数量。
-l | --log file : 设置日志文件,默认为 stdout。
-L | --limit num : 导出数据时,每次写入磁盘之前在内存中缓冲的记录数量,默认值为 10000。
-m | --mysql : 导出 MySQL 数据库。
-n | --namespace schema : 设置需要导出的 Oracle 模式。
-N | --pg_schema schema : 设置 PostgreSQL 中的搜索路径 search_path。
-o | --out file : 设置导出的 SQL 文件的存储路径。默认值为当前目录下的 output.sql 文件。
-p | --plsql : 启用 PLSQL 代码到 PLPGSQL 代码的转换。
-P | --parallel num: 同时导出多个表,设置并发数量。
-q | --quiet : 不显示进度条。
-s | --source DSN : 设置 Oracle DBI 数据源。
-t | --type export: 设置导出类型。该参数将会覆盖配置文件中的导出类型(TYPE)。
-T | --temp_dir DIR: 为多个同时运行的 ora2pg 脚本指定不同的临时存储目录。
-u | --user name : 设置连接 Oracle 数据库连接的用户名。也可以使用 ORA2PG_USER 环境变量。
-v | --version : 显示 Ora2Pg 版本信息并退出。
-w | --password pwd : 设置连接 Oracle 数据库的用户密码。也可以使用 ORA2PG_PASSWD 环境变量。
--forceowner : 导入数据时,强制 ora2pg 将导入 PostgreSQL 的表和序列的拥有者设置为连接 Oracle 数据库时的用户。如果设置为指定的用户名,所有导入的对象属于该用户。默认情况下,对象的拥有者为连接 Pg 数据库的用户。
--nls_lang code: 设置 Oracle 客户端的 NLS_LANG 编码。
--client_encoding code: 设置 PostgreSQL 客户端编码。
--view_as_table str: 将视图导出为表,多个视图使用逗号分隔。
--estimate_cost : 在 SHOW_REPORT 结果中输出迁移成本评估信息。
--cost_unit_value minutes: 成本评估单位,使用分钟数表示。默认值为 5 分钟,表示一个 PostgreSQL 专家迁移所需的时间。如果是第一次迁移,可以设置为 10 分钟。
--dump_as_html : 生成 HTML 格式的迁移报告,只能与 SHOW_REPORT 选项一起使用。默认的报告是一个简单的文本文件。
--dump_as_csv : 与上个参数相同,但是生成 CSV 格式的报告。
--dump_as_sheet : 生成迁移评估时,为每个数据库生成一行 CSV 记录。
--init_project NAME: 创建一个ora2pg 项目目录结构。项目的顶级目录位于根目录之下。
--project_base DIR : 定义ora2pg 项目的根目录,默认为当前目录。
--print_header : 与 --dump_as_sheet 一起使用,输出 CSV 标题信息。
--human_days_limit num : 设置迁移评估级别从 B 升到 C 所需的人工日数量。默认值为 5 人工日。
--audit_user LIST : 设置查询 DBA_AUDIT_TRAIL 表时需要过滤的用户名,多个用户使用逗号分隔。该参数只能用于 SHOW_REPORT 和 QUERY 导出类型。
--pg_dsn DSN : 设置在线导入时的 PostgreSQL 数据源。
--pg_user name : 设置连接 PostgreSQL 的用户名。
--pg_pwd password : 设置连接 PostgreSQL 的用户密码。
--count_rows : 在 TEST 方式下执行真实的数据行数统计。
--no_header : 在导出文件中不添加 Ora2Pg 头部信息。
--oracle_speed : 用于测试 Oracle 发送数据的速度。不会真的处理或者写入数据。
--ora2pg_speed : 用于测试 Ora2Pg 发送转换后的数据的速度。不会写入任何数据。
如果执行成功,ora2pg 返回 0;如果出现错误,返回 1 。如果某个子进程被中断,并且用户收到了警告信息:“WARNING: an error occurs during data export. Please check what’s happen.”,ora2pg 将会返回 2 。大多数情况下是内存溢出(OOM)的问题,可以先尝试减小 DATA_LIMIT 参数的值。
对于开发者而言,可以在 ora2pg Perl 脚本中添加自定义的选项;因为 ora2pg.conf 文件中的所有配置选项都会以小写形式传递给新建的 Ora2Pg 对象实例。参考 ora2pg 代码以添加自定义的选项。
官网:
https://ora2pg.darold.net/start.html
下载地址:
https://github.com/darold/ora2pg/releases
文档:
https://ora2pg.darold.net/documentation.html
为使安装能够顺利通过,在安装 Ora2Pg 之前必须先确保系统已经安装了 Perl 模块以及 DBI、DBD::Oracle 模块。若需要直接导入到 PostgreSQL 则还需要安装 DBD::Pg 模块。
## 在编译安装了PG的机器上安装ora2pg(本测试oracle和pg在同一台机器Tencent上)
--安装依赖包,perl版本5.10以上
yum install -y gcc perl-DBD-Pg perl perl-devel perl-DBI perl-CPAN bzip2 \
perl-ExtUtils-eBuilder perl-ExtUtils-MakeMaker perl-Time-HiRes perl-tests perf cpan
--安装“DBI”
下载地址:https://metacpan.org/release/DBI
tar -zxvf DBI-1.643.tar.gz
cd DBI-1.643
perl Makefile.PL
make && make install
--安装“DBD::Oracle”
下载地址:https://sourceforge.net/projects/ora2pg/
tar -zxvf DBD-Oracle-1.83.tar.gz
cd DBD-Oracle-1.83
perl Makefile.PL
make && make install
--配置root 环境变量,添加oracle的信息
cat >> /root/.bash_profile << "EOF"
export ORACLE_HOME=/oracle/app/oracle/product/12.2.0/db_1
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
EOF
source /root/.bash_profile
--安装“ora2pg”
下载地址: https://github.com/darold/ora2pg/releases/tag/v23.2
tar -zxvf ora2pg-23.2.tar.gz
cd ora2pg-23.2
perl Makefile.PL
make && make install
--安装“DBD::Pg”(可选)
如果想直接将ora2pg导出的数据直接导入pg数据库,不生成本地文件,则可以安装该插件并配置使用
下载地址:http://www.cpan.org/authors/id/T/TU/TURNSTEP/
tar -zxvf DBD-Pg-3.16.3.tar.gz
cd DBD-Pg-3.16.3
perl Makefile.PL # 输入/postgresql/pg15/bin/pg_config
perl Makefile.PL
make && make install
--查看ora2pg安装情况
[root@tencent tmp]# which ora2pg
/usr/local/bin/ora2pg
[root@tencent tmp]# ora2pg –version
Ora2Pg v23.2
--检查所有软件是否已成功安装
创建检查脚本
cat > /root/check.pl <<"EOF"
#!/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);
}
exit;
EOF
执行检查
[root@tencent tmp]# perl /root/check.pl
DBD::Oracle -- 1.83
DBD::Pg -- 3.16.3
DBI -- 1.643
Ora2Pg -- 23.2
Perl -- 5.16.3
配置参考:https://ora2pg.darold.net/documentation.html#CONFIGURATION
首先从示例文件复制一份,然后根据实际情况更改配置文件相关参数:
cp /etc/ora2pg/ora2pg.conf.dist /etc/ora2pg/ora2pg.conf
[root@tencent ora2pg]# cat /etc/ora2pg/ora2pg.conf.dist | grep -v ^# | grep -v ^$ | wc -l
125
大约100多个参数
2.3.1、导出表结构的配置文件
-- 表结构、约束(主外键等)、索引等
cat > /etc/ora2pg/ora2pg_table_ddl.conf <<"EOF"
ORACLE_HOME /oracle/app/oracle/product/12.2.0/db_1
ORACLE_DSN dbi:Oracle:host=10.0.4.16;sid=ORCL;port=1521
#ORACLE_DSN dbi:Oracle:tns_ora12c
ORACLE_USER system
ORACLE_PWD oracle
SCHEMA HR
EXPORT_SCHEMA 1
CREATE_SCHEMA 1
TYPE TABLE
PG_NUMERIC_TYPE 0
PG_INTEGER_TYPE 1
DEFAULT_NUMERIC float
SKIP fkeys checks
#SKIP keys pkeys ukeys indexes checks
NLS_LANG AMERICAN_AMERICA.UTF8
OUTPUT_DIR
OUTPUT table_ddl_output.sql
PG_VERSION 15
EOF
2.3.2、导出其它对象的配置文件
cat > /etc/ora2pg/ora2pg_other_ddl.conf <<"EOF"
ORACLE_HOME /oracle/app/oracle/product/12.2.0/db_1
ORACLE_DSN dbi:Oracle:host=10.0.4.16;sid=ORCL;port=1521
#ORACLE_DSN dbi:Oracle:tns_ora12c
ORACLE_USER system
ORACLE_PWD oracle
SCHEMA HR
EXPORT_SCHEMA 1
CREATE_SCHEMA 1
TYPE PACKAGE,PROCEDURE,TRIGGER,FUNCTION,VIEW,GRANT,SEQUENCE,MVIEW,TYPE,SYNONYM
PG_NUMERIC_TYPE 0
PG_INTEGER_TYPE 1
DEFAULT_NUMERIC float
SKIP fkeys
#SKIP keys pkeys ukeys indexes checks
NLS_LANG AMERICAN_AMERICA.UTF8
OUTPUT_DIR
OUTPUT other_ddl_output.sql
PG_VERSION 15
EOF
2.3.3、导出表数据的配置文件
cat > /etc/ora2pg/ora2pg_data.conf <<"EOF"
ORACLE_HOME /oracle/app/oracle/product/12.2.0/db_1
ORACLE_DSN dbi:Oracle:host=10.0.4.16;sid=ORCL;port=1521
#ORACLE_DSN dbi:Oracle:tns_ora12c
ORACLE_USER system
ORACLE_PWD oracle
SCHEMA HR
TYPE INSERT
PG_NUMERIC_TYPE 0
PG_INTEGER_TYPE 1
DEFAULT_NUMERIC float
SKIP fkeys checks
#SKIP fkeys pkeys ukeys indexes checks
NLS_LANG AMERICAN_AMERICA.UTF8
OUTPUT_DIR
OUTPUT data_output.sql
PG_VERSION 15
#PG_DSN dbi:Pg:dbname=test_db;host=localhost;port=5432
#PG_USER test
#PG_PWD test
EOF
注意:若去掉PG_DSN、PG_USER和PG_PWD参数的注释,则会将数据直接导入到PG中,而不会导出到中间文件。
导出数据type 可以设置为COPY或者INSERT,本测试设置为INSERT
2.4.1、测试连接
[root@tencent ora2pg]# ora2pg -t SHOW_VERSION -c ora2pg_table_ddl.conf
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0
2.4.2、迁移成本评估
[root@tencent ora2pg]# ora2pg -t SHOW_REPORT --estimate_cost -c ora2pg_table_ddl.conf
[========================>] 9/9 tables (100.0%) end of scanning.
[========================>] 10/10 objects types (100.0%) end of objects auditing.
Ora2Pg v23.2 - Database Migration Report
Version Oracle Database 12c Enterprise Edition Release 12.2.0.1.0
Schema HR
Size 54.62 MB
Object Number Invalid Estimated cost Comments Details
DATABASE LINK 0 0 0.00 Database links will be exported as SQL/MED PostgreSQL's Foreign Data Wrapper (FDW) extensions using oracle_fdw.
GLOBAL TEMPORARY TABLE 0 0 0.00 Global temporary table are not supported by PostgreSQL and will not be exported. You will have to rewrite some application code to match the PostgreSQL temporary table behavior.
INDEX 19 0 3.00 11 index(es) are concerned by the export, others are automatically generated and will do so on PostgreSQL. Bitmap will be exported as btree_gin index(es). Domain index are exported as b-tree but commented to be edited to mainly use FTS. Cluster, bitmap join and IOT indexes will not be exported at all. Reverse indexes are not exported too, you may use a trigram-based index (see pg_trgm) or a reverse() function based index and search. Use 'varchar_pattern_ops', 'text_pattern_ops' or 'bpchar_pattern_ops' operators in your indexes to improve search with the LIKE operator respectively into varchar, text or char columns. 11 b-tree index(es).
JOB 0 0 0.00 Job are not exported. You may set external cron job with them.
PROCEDURE 5 1 31.50 Total size of procedure code: 2415 bytes. secure_dml: 3. sp_500: 3. sp_create_data: 18.5. add_job_history: 3.
SEQUENCE 4 0 1.00 Sequences are fully supported, but all call to sequence_name.NEXTVAL or sequence_name.CURRVAL will be transformed into NEXTVAL('sequence_name') or CURRVAL('sequence_name').
SYNONYM 0 0 0.00 SYNONYMs will be exported as views. SYNONYMs do not exists with PostgreSQL but a common workaround is to use views or set the PostgreSQL search_path in your session to access object outside the current schema.
TABLE 9 0 1.20 2 check constraint(s). Total number of rows: 500215. Top 10 of tables sorted by number of rows:. order_table has 500000 rows. employees has 107 rows. departments has 27 rows. countries has 25 rows. locations has 23 rows. jobs has 19 rows. job_history has 10 rows. regions has 4 rows. demo has 0 rows. Top 10 of largest tables:.
TRIGGER 2 0 5.00 Total size of trigger code: 123 bytes. update_job_history: 3.
VIEW 1 0 1.00 Views are fully supported but can use specific functions.
Total 40 1 42.70 42.70 cost migration units means approximatively 1 person-day(s). The migration unit was set to 5 minute(s)
Migration level : B-5
Migration levels:
A - Migration that might be run automatically
B - Migration with code rewrite and a human-days cost up to 5 days
C - Migration with code rewrite and a human-days cost above 5 days
Technical levels:
1 = trivial: no stored functions and no triggers
2 = easy: no stored functions but with triggers, no manual rewriting
3 = simple: stored functions and/or triggers, no manual rewriting
4 = manual: no stored functions but with triggers or views with code rewriting
5 = difficult: stored functions and/or triggers with code rewriting
Details of cost assessment per function
Function sp_create_data total estimated cost: 18.5
DBMS_ => 5 (cost: 3)
TRUNC => 4 (cost: 0.1)
TEST => 2
SIZE => 1
ADD_MONTHS => 1 (cost: 0.1)
Function sp_500 total estimated cost: 3
TEST => 2
SIZE => 1
Function add_job_history total estimated cost: 3
TEST => 2
SIZE => 1
Function secure_dml total estimated cost: 3
TEST => 2
SIZE => 1
Details of cost assessment per trigger
Trigger update_job_history total estimated cost: 3
TEST => 2
SIZE => 1
2.4.3、迁移表结构
[root@tencent ora2pg]# ora2pg -c ora2pg_table_ddl.conf
[========================>] 12/12 tables (100.0%) end of scanning.
[========================>] 12/12 tables (100.0%) end of table export.
查看生成的表结构SQL
-rw-r--r-- 1 root root 12216 4月 21 14:12 table_ddl_output.sql
2.4.4、迁移其他对象结构
[root@tencent ora2pg]# ora2pg -c ora2pg_other_ddl.conf
[========================>] 0/0 packages (100.0%) end of output.
[========================>] 3/3 procedures (100.0%) end of procedures export.
[========================>] 1/1 triggers (100.0%) end of output.
[========================>] 0/0 functions (100.0%) end of functions export.
[========================>] 1/1 views (100.0%) end of output.
[========================>] 3/3 sequences (100.0%) end of output.
[========================>] 0/0 materialized views (100.0%) end of output.
[========================>] 0/0 types (100.0%) end of output.
[========================>] 0/0 synonyms (100.0%) end of output.
查看生成的其他对象结构SQL
-rw-r--r-- 1 root root 312 4月 21 14:18 PACKAGE_other_ddl_output.sql
-rw-r--r-- 1 root root 3264 4月 21 14:18 PROCEDURE_other_ddl_output.sql
-rw-r--r-- 1 root root 760 4月 21 14:18 TRIGGER_other_ddl_output.sql
-rw-r--r-- 1 root root 313 4月 21 14:18 FUNCTION_other_ddl_output.sql
-rw-r--r-- 1 root root 1047 4月 21 14:18 VIEW_other_ddl_output.sql
-rw-r--r-- 1 root root 341 4月 21 14:18 TYPE_other_ddl_output.sql
-rw-r--r-- 1 root root 312 4月 21 14:18 SYNONYM_other_ddl_output.sql
-rw-r--r-- 1 root root 549 4月 21 14:18 SEQUENCE_other_ddl_output.sql
-rw-r--r-- 1 root root 31 4月 21 14:18 MVIEW_other_ddl_output.sql
-rw-r--r-- 1 root root 312 4月 21 14:18 GRANT_other_ddl_output.sql
2.4.5、导出表数据
[root@tencent ora2pg]# ora2pg -c ora2pg_data.conf
[========================>] 12/12 tables (100.0%) end of scanning.
[========================>] 25/25 rows (100.0%) Table COUNTRIES (25 recs/sec)
[=> ] 25/467 total rows (5.4%) - (1 sec., avg: 25 recs/sec).
[========================>] 27/27 rows (100.0%) Table DEPARTMENTS (27 recs/sec)
[==> ] 52/467 total rows (11.1%) - (1 sec., avg: 52 recs/sec).
[========================>] 107/107 rows (100.0%) Table EMPLOYEES (107 recs/sec)
[========> ] 159/467 total rows (34.0%) - (1 sec., avg: 159 recs/sec).
[========================>] 106/106 rows (100.0%) Table EMP_REPORT (106 recs/sec)
[=============> ] 265/467 total rows (56.7%) - (1 sec., avg: 265 recs/sec).
[========================>] 107/107 rows (100.0%) Table EMP_SALARY_DIFF (107 recs/sec)
[===================> ] 372/467 total rows (79.7%) - (2 sec., avg: 186 recs/sec).
[========================>] 1/1 rows (100.0%) Table HIGHEST_PAID_EMP (1 recs/sec)
[===================> ] 373/467 total rows (79.9%) - (2 sec., avg: 186 recs/sec).
[========================>] 19/19 rows (100.0%) Table JOBS (19 recs/sec)
[====================> ] 392/467 total rows (83.9%) - (2 sec., avg: 196 recs/sec).
[========================>] 10/10 rows (100.0%) Table JOB_HISTORY (10 recs/sec)
[====================> ] 402/467 total rows (86.1%) - (2 sec., avg: 201 recs/sec).
[========================>] 23/23 rows (100.0%) Table LOCATIONS (23 recs/sec)
[=====================> ] 425/467 total rows (91.0%) - (3 sec., avg: 141 recs/sec).
[========================>] 4/4 rows (100.0%) Table REGIONS (4 recs/sec)
[======================> ] 429/467 total rows (91.9%) - (3 sec., avg: 143 recs/sec).
[========================>] 19/19 rows (100.0%) Table T1 (19 recs/sec)
[=======================> ] 448/467 total rows (95.9%) - (3 sec., avg: 149 recs/sec).
[========================>] 19/19 rows (100.0%) Table T2 (19 recs/sec)
[========================>] 467/467 total rows (100.0%) - (4 sec., avg: 116 recs/sec).
[========================>] 467/467 rows (100.0%) on total estimated data (4 sec., avg: 116 recs/sec)
查看导出的表数据SQL
-rw-r--r-- 1 root root 75422 4月 21 14:36 data_output.sql
2.4.6、PG数据库创建orcl数据库及HR用户
[postgres@tencent ~]$ psql
psql (15.2)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | ICU Locale | Locale Provider | Access privileges
-----------+----------+----------+-------------+-------------+------------+-----------------+-----------------------
orcl | hr | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | | libc |
postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | | libc |
template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | | libc | =c/postgres +
| | | | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | | libc | postgres=CTc/postgres+
| | | | | | | =c/postgres
(4 rows)
postgres=# create user u1 with password 'u1';
CREATE ROLE
postgres=# alter role u1 createrole createdb;
ALTER ROLE
postgres=# \c - u1
You are now connected to database "postgres" as user "u1".
postgres=> create database u1;
CREATE DATABASE
postgres=> \c u1
You are now connected to database "xx" as user "u1".
u1=> create schema u1;
CREATE SCHEMA
u1=> \dn
List of schemas
Name | Owner
--------+----------
public | postgres
u1 | u1
(2 rows)
u1=> \l
List of databases
Name | Owner | Encoding | Collate | Ctype | ICU Locale | Locale Provider | Access privileges
-----------+----------+----------+-------------+-------------+------------+-----------------+-----------------------
orcl | hr | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | | libc |
postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | | libc |
template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | | libc | =c/postgres +
| | | | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | | libc | postgres=CTc/postgres+
| | | | | | | =c/postgres
u1 | u1 | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | | libc |
(5 rows)
2.4.7、导入表结构
psql -d orcl -Uhr -f table_ddl_output.sql
注意:由于有物化视图,在TABLE_sh.sql 里包含了物化视图的索引,会创建失败。需先创建表,在创建物化视图,最后创建索引。取消物化视图索引,后面单独创建
2.4.8、导入other对象结构
psql -d orcl -Uhr -f PACKAGE_other_ddl_output.sql
psql -d orcl -Uhr -f PROCEDURE_other_ddl_output.sql
psql -d orcl -Uhr -f TRIGGER_other_ddl_output.sql
psql -d orcl -Uhr -f FUNCTION_other_ddl_output.sql
psql -d orcl -Uhr -f VIEW_other_ddl_output.sql
psql -d orcl -Uhr -f SEQUENCE_other_ddl_output.sql
psql -d orcl -Uhr -f GRANT_other_ddl_output.sql
psql -d orcl -Uhr -f TYPE_other_ddl_output.sql
psql -d orcl -Uhr -f SYNONYM_other_ddl_output.sql
psql -d orcl -Uhr -f MVIEW_other_ddl_output.sql
2.4.9、导入表数据
psql -d orcl -Uhr -f data_output.sql
注意:导入数据默认是最后提交,如果前面的事务INSERT失败,后面的都会失败,所以可以在每个insert事务后面添加commit,这样保证每个事务的提交是独立的,即使报错也不影响其他事务数据的提交。
2.4.10、导入完成后,PG查看数据
--表、序列
orcl=> \d+
List of relations
Schema | Name | Type | Owner | Persistence | Access method | Size | Description
--------+------------------+----------+-------+-------------+---------------+------------+---------------------------------------------------------------------------------------
hr | countries | table | hr | permanent | heap | 8192 bytes | country table. Contains 25 rows. References with locations table.
hr | departments | table | hr | permanent | heap | 8192 bytes | Departments table that shows details of departments where employees +
| | | | | | | work. Contains 27 rows; references with locations, employees, and job_history tables.
hr | departments_seq | sequence | hr | permanent | | 8192 bytes |
hr | emp_details_view | view | hr | permanent | | 0 bytes |
hr | emp_report | table | hr | permanent | heap | 8192 bytes |
hr | emp_salary_diff | table | hr | permanent | heap | 8192 bytes |
hr | employees | table | hr | permanent | heap | 16 kB | employees table. Contains 107 rows. References with departments, +
| | | | | | | jobs, job_history tables. Contains a self reference.
hr | employees_seq | sequence | hr | permanent | | 8192 bytes |
hr | highest_paid_emp | table | hr | permanent | heap | 8192 bytes |
hr | job_history | table | hr | permanent | heap | 8192 bytes | Table that stores job history of the employees. If an employee +
| | | | | | | changes departments within the job or changes jobs within the department, +
| | | | | | | new rows get inserted into this table with old job information of the +
| | | | | | | employee. Contains a complex primary key: employee_id+start_date. +
| | | | | | | Contains 25 rows. References with jobs, employees, and departments tables.
hr | jobs | table | hr | permanent | heap | 8192 bytes | jobs table with job titles and salary ranges. Contains 19 rows. +
| | | | | | | References with employees and job_history table.
hr | locations | table | hr | permanent | heap | 8192 bytes | Locations table that contains specific address of a specific office, +
| | | | | | | warehouse, and/or production site of a company. Does not store addresses / +
| | | | | | | locations of customers. Contains 23 rows; references with the +
| | | | | | | departments and countries tables.
hr | locations_seq | sequence | hr | permanent | | 8192 bytes |
hr | regions | table | hr | permanent | heap | 8192 bytes |
hr | t1 | table | hr | permanent | heap | 8192 bytes |
hr | t2 | table | hr | permanent | heap | 8192 bytes |
(16 rows)
--表大小
orcl=> select relname, pg_size_pretty(pg_relation_size(relid)) from pg_stat_user_tables where schemaname='hr' order by pg_relation_size(relid) desc;
relname | pg_size_pretty
------------------+----------------
locations | 8192 bytes
t1 | 8192 bytes
regions | 8192 bytes
job_history | 8192 bytes
jobs | 8192 bytes
departments | 8192 bytes
emp_report | 8192 bytes
highest_paid_emp | 8192 bytes
t2 | 8192 bytes
emp_salary_diff | 8192 bytes
countries | 8192 bytes
employees | 0 bytes
(12 rows)
--索引
orcl=> \di
List of relations
Schema | Name | Type | Owner | Table
--------+-----------------------+-------+-------+-------------
hr | countries_pkey | index | hr | countries
hr | departments_pkey | index | hr | departments
hr | dept_location_ix | index | hr | departments
hr | emp_department_ix | index | hr | employees
hr | emp_job_ix | index | hr | employees
hr | emp_manager_ix | index | hr | employees
hr | emp_name_ix | index | hr | employees
hr | employees_email_key | index | hr | employees
hr | employees_pkey | index | hr | employees
hr | jhist_department_ix | index | hr | job_history
hr | jhist_employee_ix | index | hr | job_history
hr | jhist_job_ix | index | hr | job_history
hr | job_history_pkey | index | hr | job_history
hr | jobs_pkey | index | hr | jobs
hr | loc_city_ix | index | hr | locations
hr | loc_country_ix | index | hr | locations
hr | loc_state_province_ix | index | hr | locations
hr | locations_pkey | index | hr | locations
hr | regions_pkey | index | hr | regions
(19 rows)
--序列
orcl=> \ds
List of relations
Schema | Name | Type | Owner
--------+-----------------+----------+-------
hr | departments_seq | sequence | hr
hr | employees_seq | sequence | hr
hr | locations_seq | sequence | hr
(3 rows)
(4 rows)
--函数
orcl=> \df
List of functions
Schema | Name | Result data type | Argument data types
| Type
--------+--------------------------------------------------+------------------+-------------------------------------------------------------------------------------------------------------------------------
------------------------------------------+------
hr | add_job_history | | IN p_emp_id integer, IN p_start_date timestamp without time zone, IN p_end_date timestamp without time zone, IN p_job_id chara
cter varying, IN p_department_id smallint | proc
hr | secure_dml | |
| proc
hr | sp_generate_report_and_diff_and_highest_paid_emp | |
| proc
hr | trigger_fct_update_job_history | trigger |
| func
(4 rows)
--存储过程
orcl=> select oid,proname,proowner from pg_proc where proowner=16476;
oid | proname | proowner
-------+--------------------------------------------------+----------
16804 | trigger_fct_update_job_history | 16476
16801 | add_job_history | 16476
16802 | secure_dml | 16476
16803 | sp_generate_report_and_diff_and_highest_paid_emp | 16476
(4 rows)
--数据库大小
orcl=> select pg_database_size('orcl');
pg_database_size
------------------
8401711
(1 row)