Use ora2pg to migrate oracle12.2 to postgresql15.2

1. Environment configuration

1.1. Environmental Planning

project

Oracle source

PG target library

db type

single instance

single instance

db version

12.2.0

15.2

OS version

CentOS7.9 64 bit

CentOS7.9 64 bit

OS hostname

tencent

tencent

IP address

10.0.4.16

10.0.4.16

dbname

orcl

orcl

listening port

1521

4519

SOFT_HOME

/oracle/app/oracle/product/12.2.0/db_1

/postgresql/pg15

install user

oracle

postgres

sync user

hr

hr

Software Installation:

the host

required software

Source ( 10.0.4.16 )

Oracle 12c database

Target side ( 10.0.4.16 )

PG 15.2 , ora2pg software

1.2, oracle test data

Use the hr schema data under the oracle template.

View objects and object types under 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

create stored procedure

--User authorization

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 stored procedure

CREATE OR REPLACE PROCEDURE sp_generate_report_and_diff_and_highest_paid_emp

AS

-- Define the cursor variable

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

-- Print header information

DBMS_OUTPUT.PUT_LINE('EMPLOYEE REPORT');

DBMS_OUTPUT.PUT_LINE('----------------');

-- Create employee report form

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';

-- Create employee salary difference table

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';

-- Create table of top paid employees

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)';

-- Print the end of the table information

DBMS_OUTPUT.PUT_LINE('----------------');

DBMS_OUTPUT.PUT_LINE('END OF REPORT');

END;

/

execute stored procedure

SQL> exec sp_generate_report_and_diff_and_highest_paid_emp

2. Use ora2pg to migrate Oracle 12.2 data to PG15.2

2.1 Introduction to Ora2Pg

2.1.1 Introduction to Ora2Pg

Ora2Pg is a free tool for migrating Oracle or MySQL databases to PostgreSQL databases. It connects to an Oracle or MySQL database , automatically scans and extracts its structure and data, and then generates SQL scripts for migration, which can be used to load the database structure and data into a PostgreSQL database.

​​​​​​​​2.1.2 , ora2pg allows all specified command line parameters

Usage: ora2pg [-dhpqv --estimate_cost --dump_as_html] [--option value]

-a | --allow str : Specify a list of objects that are allowed to be exported, separated by commas. Can also be used with the SHOW_COLUMN option.

-b | --basedir dir: Set the default export directory for storing the export results.

-c | --conf file : Set a non-default configuration file, the default configuration file is /etc/ora2pg/ora2pg.conf.

-d | --debug : use debug mode, output more detailed information.

-D | --data_type STR : Set data type conversion via command line.

-e | --exclude str: Specify a list of objects to exclude when exporting, separated by commas. Can also be used with the SHOW_COLUMN option.

-h | --help : Display help information.

-g | --grant_object type : Export the authorization information on the object of the specified type, see the GRANT_OBJECT configuration item for the value.

-i | --input file : Specifies the Oracle PL/SQL code file to be imported, and does not need to connect to the Oracle database when importing the file.

-j | --jobs num : Set the number of concurrent processes used to send data to PostgreSQL.

-J | --copies num : Set the number of concurrent connections used to export data from Oracle.

-l | --log file : Set the log file, the default is stdout.

-L | --limit num : When exporting data, the number of records buffered in memory before each write to disk, the default value is 10000.

-m | --mysql : Export MySQL database.

-n | --namespace schema : Set the Oracle schema that needs to be exported.

-N | --pg_schema schema : Set the search path search_path in PostgreSQL.

-o | --out file : Set the storage path of the exported SQL file. The default value is the output.sql file in the current directory.

-p | --plsql : Enable conversion of PLSQL code to PLPGSQL code.

-P | --parallel num: Export multiple tables at the same time, and set the number of concurrency.

-q | --quiet : Do not show the progress bar.

-s | --source DSN : Set the Oracle DBI data source.

-t | --type export: Set the export type. This parameter will override the export type (TYPE) in the configuration file.

-T | --temp_dir DIR: Specify different temporary storage directories for multiple concurrently running ora2pg scripts.

-u | --user name : Set the username to connect to the Oracle database. You can also use the ORA2PG_USER environment variable.

-v | --version : Display Ora2Pg version information and exit.

-w | --password pwd : Set the user password for connecting to the Oracle database. You can also use the ORA2PG_PASSWD environment variable.

--forceowner : When importing data, force ora2pg to set the owner of the table and sequence imported into PostgreSQL as the user when connecting to the Oracle database. If set to the specified username, all imported objects belong to that user. By default, the object is owned by the user connected to the Pg database.

--nls_lang code: Set the NLS_LANG code for the Oracle client.

--client_encoding code: Set PostgreSQL client encoding.

--view_as_table str: Export the view as a table, multiple views are separated by commas.

--estimate_cost : Output migration cost estimate information in SHOW_REPORT results.

--cost_unit_value minutes: Cost evaluation unit expressed in minutes. The default value is 5 minutes, which represents the time required for a PostgreSQL expert to migrate. If it is the first migration, it can be set to 10 minutes.

   --dump_as_html : Generate a migration report in HTML format, can only be used with the SHOW_REPORT option. The default report is a simple text file.

   --dump_as_csv : Same as the previous argument, but generate a report in CSV format.

   --dump_as_sheet : When generating migration assessments, generate one row of CSV records for each database.

   --init_project NAME: Create an ora2pg project directory structure. The project's top-level directory is located below the root directory.

   --project_base DIR : Define the root directory of the ora2pg project, the default is the current directory.

   --print_header : Use with --dump_as_sheet to output CSV header information.

   --human_days_limit num : Set the number of human days required to raise the migration evaluation level from B to C. The default is 5 man days.

   --audit_user LIST : Set the user names that need to be filtered when querying the DBA_AUDIT_TRAIL table, and multiple users are separated by commas. This parameter can only be used for SHOW_REPORT and QUERY export types.

   --pg_dsn DSN : Set the PostgreSQL data source for online import.

   --pg_user name : Set the user name to connect to PostgreSQL.

   --pg_pwd password : Set the user password to connect to PostgreSQL.

   --count_rows : Perform real data row count in TEST mode.

   --no_header : Do not add Ora2Pg header information in the export file.

   --oracle_speed : Used to test the speed at which Oracle sends data. Does not actually process or write data.

   --ora2pg_speed : Used to test the speed at which Ora2Pg sends converted data. No data will be written.

ora2pg returns 0 if successful, and 1 if an error occurs. If a child process is interrupted and the user receives a warning message: "WARNING: an error occurs during data export. Please check what's happened.", ora2pg will return 2. In most cases, it is a memory overflow (OOM) problem, you can first try to reduce the value of the DATA_LIMIT parameter.

For developers, you can add custom options in the ora2pg Perl script; because all configuration options in the ora2pg.conf file will be passed to the newly created Ora2Pg object instance in lowercase. Refer to the ora2pg code for adding custom options.

2.2. Install Ora2Pg

Official website:
https://ora2pg.darold.net/start.html
Download address:
https://github.com/darold/ora2pg/releases
Documentation:
https://ora2pg.darold.net/documentation.html

In order to make the installation go smoothly, before installing Ora2Pg, you must first ensure that the system has installed the Perl module and the DBI, DBD::Oracle module. If you need to import directly to PostgreSQL, you also need to install the DBD::Pg module.

## Install ora2pg on the machine where PG is compiled and installed (this test oracle and pg are on the same machine Tencent)

--Install dependent packages, perl version 5.10 or above

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

--install "dbi"

Download address: https://metacpan.org/release/DBI

tar -zxvf DBI-1.643.tar.gz

cd DBI-1.643

perl Makefile.PL

make &&  make install

--install "DBD::Oracle"

Download address: https://sourceforge.net/projects/ora2pg/

tar -zxvf DBD-Oracle-1.83.tar.gz

cd DBD-Oracle-1.83

perl Makefile.PL

make &&  make install

-- Configure the root environment variable and add oracle information

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

--install "ora2pg"

Download address: 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

-- Install 'dbd::pg' (optional)

If you want to directly import the data exported by ora2pg into the pg database without generating local files, you can install this plug-in and configure it to use

Download address: 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 # input /postgresql/pg15/bin/pg_config

perl Makefile.PL

make && make install

--Check ora2pg installation status

[root@tencent tmp]# which ora2pg

/usr/local/bin/hour2pg

[root@tencent tmp]# ora2pg –version

Ora2Pg v23.2

-- Check if all software has been successfully installed

Create check script

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

perform check

[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

2.3. Create a configuration file

Configuration reference: https://ora2pg.darold.net/documentation.html#CONFIGURATION

First copy a copy from the sample file, and then change the relevant parameters of the configuration file according to the actual situation:

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

About more than 100 parameters

2.3.1. Export the configuration file of the table structure

-- Table structure, constraints (primary and foreign keys, etc.), indexes, etc.

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. Export configuration files of other objects

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. Configuration file for exporting table data

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

 Note: If the comments of the PG_DSN, PG_USER, and PG_PWD parameters are removed, the data will be directly imported into PG instead of being exported to an intermediate file.

Export data type can be set to COPY or INSERT, this test is set to INSERT

2.4. Use ora2pg to migrate data

2.4.1. Test connection

[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. Migration cost assessment

[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. Migration table structure

[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.

View the generated table structure SQL

-rw-r--r-- 1 root root    12216 4月  21 14:12 table_ddl_output.sql

2.4.4. Migrating other object structures

[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.

View other generated object structure 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. Export table data

[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)

View the exported table data SQL

-rw-r--r-- 1 root root    75422 4月  21 14:36 data_output.sql

2.4.6, PG database to create orcl database and HR user

[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 | in1

(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. Import table structure

psql -d orcl -Uhr -f table_ddl_output.sql

Note: Due to the materialized view, the index of the materialized view is included in TABLE_sh.sql, and the creation will fail. You need to create the table first, then create the materialized view, and finally create the index.Cancel the materialized view index and create it separately later

2.4.8. Import other object structure

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. Import table data

psql -d orcl -Uhr -f data_output.sql

Note: Importing data is submitted last by default. If the previous transaction INSERT fails, the subsequent ones will fail, so you can add a commit after each insert transaction, so as to ensure that the submission of each transaction is independent, and even if an error is reported, it will not affect other transactions. Submission of data.

2.4.10 After the import is complete, PG checks the data

-- table, sequence

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)

-- table size

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)

--index

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)

--sequence

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)

--function

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)

--stored procedure

orcl=> select oid,proname,proowner from pg_proc where proowner=16476;

  oid | proname | pro owner

-------+--------------------------------------------------+----------

 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)

--database size

orcl=> select pg_database_size('orcl');

 pg_database_size

------------------

  8401711

(1 row)

Guess you like

Origin blog.csdn.net/zhrzhl/article/details/130292370