How to Migrate From Oracle to PostgreSQL
How to Migrate From Oracle to PostgreSQL
https://t.me/paragonacademy
Caution: Please use the commands with
care, try them on test environments first.
There are several methods for migrating data from an Oracle database into PostgreSQL. For
example, we can use the following methods:
Method 1: Ora2Pg
As the name suggests, Ora2Pg is used to migrate Oracle objects into PostgreSQL. This tool will
connect to an Oracle database and generate SQL scripts that contain table structures and data that
can be executed against PostgreSQL.
• Exports full database schema (such as tables, views including materialized view,
sequences, and indexes).
• Exports specific tables.
• Exports user-defined functions, triggers, procedures, and packages.
• Migrates Oracle user-defined types.
• Supports Oracle BLOB object as BYTEA.
• Supports DBLINK as Oracle FDW.
• Supports SYNONYMS as views.
A sample database migration report can be found here: http://ora2pg.darold.net/report.html.
For more information please visit https://ora2pg.darold.net/start.html.
Here, we will discuss how to migrate data from Oracle to PostgreSQL using the first
method(Ora2Pg).
Prerequisites:
• You will need to have a PostgreSQL database installed and running.
• You will need to have the necessary SQL scripts to migrate your data from Oracle to
PostgreSQL.
• You will need to have a plan for how to handle any data that is not compatible with
PostgreSQL.
createdb my_database
vim /etc/ora2pg.conf
# The username and password of a user with sufficient privileges to access the Oracle database.
ORACLE_USER=oracle
ORACLE_PASSWORD=password
• This is just a sample configuration file, and you may need to modify it to fit your
specific needs. For example, you may need to change the ORACLE_HOST,
ORACLE_PORT, ORACLE_USER, ORACLE_PASSWORD, ORACLE_DATABASE,
POSTGRES_DATABASE, and MIGRATED_DATA_DIR variables to match your own
environment.
• You can also specify additional configuration options in the /etc/ora2pg.conf file.
For a complete list of configuration options, please refer to the ora2pg
documentation: https://ora2pg.darold.net/documentation.html.
This will create a directory called my_database in the current directory. The directory will
contain the schema and data from the Oracle database.
This will create a file called my_database.sql in the current directory. The file will contain
SQL scripts that can be used to load the data into the PostgreSQL database.
7. Load the SQL scripts into the PostgreSQL database.
This will load the data from the SQL scripts into the PostgreSQL database.
This step is optional. If your application depends on stored procedures, packages, functions, or
other code that is specific to Oracle, you will need to port this code to work with PostgreSQL. If
this steps is a must to, use the following walk through:
Stored procedures
Stored procedures are a type of code that is stored in a database and can be executed by a user.
To port a stored procedure from Oracle to PostgreSQL, you can use the following steps:
-- PostgreSQL equivalent
CREATE FUNCTION my_stored_procedure (
p_in_param int
) RETURNS int AS
$$
BEGIN
-- Do something with the parameter.
RETURN p_in_param * 2;
END;
$$ LANGUAGE plpgsql;
Packages
Packages are a collection of stored procedures, functions, and other code that are grouped
together. To port a package from Oracle to PostgreSQL, you can use the following steps:
1. Identify the package in the Oracle database.
2. Write a PostgreSQL equivalent of the package.
3. Create the package in the PostgreSQL database.
-- Oracle package
CREATE PACKAGE my_package AS
PROCEDURE my_stored_procedure (
p_in_param int,
p_out_param int
);
FUNCTION my_function (
p_in_param int
) RETURNS int;
END;
/
-- PostgreSQL equivalent
CREATE PACKAGE my_package AS
PROCEDURE my_stored_procedure (
p_in_param int
);
FUNCTION my_function (
p_in_param int
) RETURNS int;
END;
$$ LANGUAGE plpgsql;
Functions
Functions are a type of code that is executed when it is called. To port a function from Oracle to
PostgreSQL, you can use the following steps:
-- Oracle function
CREATE FUNCTION my_function (
p_in_param int
) RETURNS int AS
BEGIN
-- Do something with the parameter.
RETURN p_in_param * 2;
END;
/
-- PostgreSQL equivalent
CREATE FUNCTION my_function (
p_in_param int
) RETURNS int AS
$$
BEGIN
-- Do something with the parameter.
RETURN p_in_param * 2;
END;
$$ LANGUAGE plpgsql;
Application code
Application code is code that is used to interact with a database. To port application code from
Oracle to PostgreSQL, you can use the following steps:
-- Oracle code
DECLARE
v_result int;
BEGIN
v_result := my_function(10);
dbms_output.put_line(v_result);
END;
/
-- PostgreSQL code
DECLARE
v_result int;
BEGIN
v_result := my_function(10);
print(v_result);
END;
Once you have migrated the database, you should test it thoroughly to make sure that it is
working correctly. You can use the psql command to query the PostgreSQL database and
to run your application.
Critical Steps:
1. Back up your Oracle database. This is important in case anything goes wrong during
the migration process. The following script will create a backup of your Oracle database.
pg_dump.sql
2. Create a PostgreSQL database that is the same schema as your Oracle database.
You can use the pg_dump command to create a backup of your Oracle database, and then
use the psql command to create a PostgreSQL database from the backup.
This script will create a PostgreSQL database from the backup of your Oracle database.
create_postgresql_database.sql
-- This script will create a PostgreSQL database from the backup of your Oracle database.
migrate_data.sql
4. Test the migrated data to make sure that it is correct. You can use the psql command
to query the PostgreSQL database to make sure that the data has been migrated correctly.
# Query the database to make sure that the data has been migrated correctly.
SELECT * FROM my_table;
This script will connect to the PostgreSQL database and query the my_table table. If the data
has been migrated correctly, the query will return the data that was in the my_table table in the
Oracle database.
id | name | email
-- | -- | --
1 | John Doe | [email protected]
2 | Jane Doe | [email protected]
If the output of the script is different from the data that was in the my_table table in the Oracle
database, then the data has not been migrated correctly. In this case, you will need to
troubleshoot the migration process to find and fix the problem.
5. Make any necessary changes to the PostgreSQL database. For example, you may
need to change the data types of some columns to match the data types in the Oracle
database.The table below illustrates Oracle to PostgreSQL safe type mapping:
Oracle PostgreSQL
BINARY_FLOAT REAL
BINARY_INTEGER INTEGER
BINARY_DOUBLE DOUBLE PRECISION
BLOB, RAW(n), LONG
BYTEA (1GB limit)
RAW
CLOB, LONG TEXT (1GB limit)
DATE TIMESTAMP
NUMBER, NUMBER(*) DOUBLE PRECISION or BIGINT if it is a part of Primary Key
NUMBER(n,0), n<5 – SMALLINT5<=n<9 – INT9<=n<19 – BIGINTn>=19 –
NUMBER(n) DECIMAL(n)
NUMBER(p,s) DECIMAL(p,s)
REAL DOUBLE PRECISION
For numeric types it is important to understand the scope of use in database. If it is focused
on accuracy, Oracle numeric types must be mapped in PostgreSQL NUMERIC. If the top
priority is calculation speed, the best mapping would be REAL or DOUBLE PRECISION.
6. Promote the PostgreSQL database to production. Once you are satisfied that the
migration has been successful, you can promote the PostgreSQL database to
production.Here is a script to promote the PostgreSQL database to production:
This script will connect to the PostgreSQL database and promote the database to production.
The ALTER DATABASE command will set the LOGICAL_REPLICATION_MODE to REPLICA.
This means that the PostgreSQL database will no longer be a master database, but will
instead be a replica database.
Once the database has been promoted to production, you can start using it to serve your
application.
Here are some additional things to consider when promoting the PostgreSQL database to
production:
• Make sure that you have a backup of the PostgreSQL database.
• Make sure that you have tested the PostgreSQL database in a staging environment.
• Make sure that you have communicated with your users about the upcoming change.
Note: These are just examples, and you may need to modify them to fit your specific needs. For
example, you may need to change the hostname, port, userid, and database variables to match your
own environment.
Rollback procedure:
If anything goes wrong during the migration process, you can roll back the migration by
restoring the backup of your Oracle database.
1. Identify the backup file. The backup file is typically a file with the .dmp extension. It
should be stored in a safe location, such as a network drive or an external hard drive.
2. Connect to the Oracle database. You can use the sqlplus command to connect to the
Oracle database.
3. Restore the backup file. Use the restore command to restore the backup file. For
example, the following command would restore the backup file called my_backup.dmp to
the database called my_database:
4. Verify the restore. Once the restore is complete, you should verify that the database has
been restored correctly. You can do this by running some basic queries against the
database.
Here are some additional tips for restoring a backup of an Oracle database:
• Make sure that you have the correct backup file. If you have multiple backup files, make
sure that you restore the correct one.
• Make sure that you have enough disk space to restore the backup file. The backup file
can be quite large, so you need to make sure that you have enough disk space to store it.
• Make sure that you are connected to the correct database. If you are not connected to the
correct database, the restore will fail.
Use the following procedure to revert to a previous version of an application, including scripts
and commands:
1. Identify the previous version of the application. The previous version of the
application is typically stored in a backup directory. It should be stored in a safe location,
such as a network drive or an external hard drive.
2. Stop the application. If the application is running, you need to stop it before you can
revert to a previous version.
3. Copy the previous version of the application to the production directory. The
production directory is the directory where the application is currently running. You can
use the cp command to copy the previous version of the application to the production
directory.
4. Start the application. Once the previous version of the application has been copied to
the production directory, you can start the application.
Here are some additional tips for reverting to a previous version of an application:
• Make sure that you have the correct backup of the application. If you have multiple
backups, make sure that you restore the correct one.
• Make sure that you have enough disk space to copy the previous version of the
application. The previous version of the application can be quite large, so you need to
make sure that you have enough disk space to store it.
• Make sure that you stop the application before you copy the previous version. If you do
not stop the application, the copy will fail.
References