ora2pg(3)
NAME
Ora2Pg - Oracle to PostgreSQL database schema converter
SYNOPSIS
Ora2pg as a companion script called ora2pg.pl when use in
conjonction with a custom version of ora2pg.conf do what
I'm trying to explain bellow :-) See content of the ora2pg.conf
file for more explanation on configuration directives.
BEGIN {
$ENV{ORACLE_HOME} = '/usr/local/oracle/oracle816';
}
use strict;
use Ora2Pg;
# Init the database connection
my $dbsrc = 'dbi:Oracle:host=testdb.mydom.fr;sid=TEST;port=1521';
my $dbuser = 'system';
my $dbpwd = 'manager';
# Create an instance of the Ora2Pg perl module
my $schema = new Ora2Pg (
datasource => $dbsrc, # Database DBD datasource
user => $dbuser, # Database user
password => $dbpwd, # Database password
);
# Create the PostgreSQL representation of all tables
$schema->export_schema("output.sql");
exit(0);
or if you only want to extract some tables:
# Create an instance of the Ora2Pg perl module
my @tables = ('t1', 't2', 't3');
my $schema = new Ora2Pg (
datasource => $dbsrc, # Database DBD datasource
user => $dbuser, # Database user
password => $dbpwd, # Database password
tables => \@tables,
or
tables => [('tab1','tab2')], # Tables to extract
debug => 1 # Verbose running.
);
or if you only want to extract the 10 first tables:
# Create an instance of the Ora2Pg perl module
my $schema = new Ora2Pg (
datasource => $dbsrc, # Database DBD datasource
user => $dbuser, # Database user
password => $dbpwd, # Database password
max => 10 # 10 first tables to extract
);
or if you only want to extract tables 10 to 20:
# Create an instance of the Ora2Pg perl module
my $schema = new Ora2Pg (
datasource => $dbsrc, # Database DBD datasource
user => $dbuser, # Database user
password => $dbpwd, # Database password
min => 10, # Begin extraction at indice 10
max => 20 # End extraction at indice 20
);
Setting showtableid to 1 will display a table and their indices without
any extraction. This will help you to set values of min/max options.
To choose a particular Oracle schema to export just set the following
option to your schema name:
schema => 'APPS'
This schema definition can also be needed when you want to export data.
If export failed and complain that the table doesn't exists use this to
prefix the table name by the schema name.
If you want to use PostgreSQL 7.4+ schema support activate the init
option 'export_schema' set to 1. Default is no schema export.
To know at which indices tables can be found during extraction use the
option:
showtableid => 1
You can now process multiple types of extraction at the samse time by
setting the value to a space separated liste of the following keywords.
To extract all views set the type option as follow:
type => 'VIEW'
To extract all grants set the type option as follow:
type => 'GRANT'
To extract all sequences set the type option as follow:
type => 'SEQUENCE'
To extract all triggers set the type option as follow:
type => 'TRIGGER'
To extract all functions set the type option as follow:
type => 'FUNCTION'
To extract all procedures set the type option as follow:
type => 'PROCEDURE'
To extract all packages and body set the type option as follow:
type => 'PACKAGE'
Default is table extraction
type => 'TABLE'
To extract tablespace (PostgreSQL >= v8):
type => 'TABLESPACE'
To extract all data from table extraction as INSERT statement use:
type => 'DATA'
To extract all data from table extraction as COPY statement use:
type => 'COPY'
and data_limit => n to specify the max tuples to return. If you set
this options to 0 or nothing, no limitation are used. Additional option
'table', 'min' and 'max' can also be used. This is usefull only when
data are send to Pg backend directly, not when when dumping to file.
When use of COPY or DATA you MUST export data by calling method:
$schema->export_data("output.sql");
Data are dumped to the given filename or to stdout with no argument.
You can also send these datas directly to a PostgreSQL backend using
the following method:
$schema->send_to_pgdb($destdatasrc,$destuser,$destpasswd);
In this case you must call export_data() without argument after the
call to method send_to_pgdb().
If you set type to COPY and you want to dump data directly to a PG
database, you must call method send_to_pgdb but data will not be sent
via DBD::Pg but they will be load to the database using the psql
command. Calling this method is still required to be able to extract
database name, hostname and port information. Edit the $PSQL variable
to match the path of your psql command (nothing to edit if psql is in
your path).
When copying tables, Ora2Pg normally exports constraints as they are;
if they are non-deferrable they are exported as non-deferrable.
However, non-deferrable constraints will probably cause problems when
attempting to import data to PostgreSQL. The option
fkey_deferrable => 1
will cause all foreign key constraints to be exported as deferrable,
even if they are non-deferrable. In addition,
defer_fkey => 1
when exporting data will add a command to actually defer all
constraints before importing data.
To non perl gurus, you can use the configuration file and run
ora2pg.pl. You will find all information into the ora2pg.conf to be
able to set it correctly.
DESCRIPTION
Ora2Pg is a perl OO module used to export an Oracle database schema to
a PostgreSQL compatible schema.
It simply connect to your Oracle database, extract its structure and
generate a SQL script that you can load into your PostgreSQL database.
I'm not a Oracle DBA so I don't really know something about its
internal structure so you may find some incorrect things. Please tell
me what is wrong and what can be better.
It currently dump the database schema (tables, views, sequences,
indexes, grants), with primary, unique and foreign keys into PostgreSQL
syntax without editing the SQL code generated.
It now can dump Oracle data into PostgreSQL DB as online process. You
can choose what columns can be exported for each table.
Functions, procedures and triggers PL/SQL code generated must be
reviewed to match the PostgreSQL syntax. Some usefull recommandation on
porting Oracle to PostgreSQL can be found at
"http://techdocs.postgresql.org/" under the "Converting from other
Databases to PostgreSQL" Oracle part. I just notice one thing more is
that the trunc() function in Oracle is the same for number or date so
be carefull when porting to PostgreSQL to use trunc() for number and
date_trunc() for date.
ABSTRACT
The goal of the Ora2Pg perl module is to cover all part needed to
export an Oracle database to a PostgreSQL database without other thing
that provide the connection parameters to the Oracle database.
- Features include:
- - Database schema export (tables, views, sequences, indexes),
with unique, primary and foreign key, check constraint.
- - Grants/privileges export by user and group.
- Table selection (by name and max table) export.
- Export Oracle schema to PostgreSQL 7.3+ schema.
- Predefined functions/triggers/procedures/packages export.
- Data export.
- Export oracle views as Pg table/data. - See ora2pg.conf for more information on use.
- My knowledge regarding database is really poor especially for Oracle so contribution is welcome.
REQUIREMENT
You just need the DBI, DBD::Pg and DBD::Oracle perl module to be
installed
PUBLIC METHODS
- new HASH_OPTIONS
- Creates a new Ora2Pg object.
- Supported options are (See ora2pg.conf for more details):
- datasource : DBD datasource (required)
- user : DBD user (optional with public access)
- password : DBD password (optional with public access)
- schema : Oracle internal schema to extract
- type : Type of data to extract, can be TABLE,VIEW,GRANT,SEQUENCE,TRIGGER,FUNCTION,PROCEDURE,DATA,COPY,PACKAGE,TABLESPACE
or a combinaison of these keywords.- debug : Print the current state of the parsing
- export_schema : Export Oracle schema to PostgreSQL 7.3 schema - tables : Extract only the given tables (arrayref) and set theextracting order- exclude : Exclude the given tables from extract (arrayref)
- showtableid : Display only the table indice during extraction
- min : Indice to begin extraction. Default to 0
- max : Indice to end extraction. Default to 0 mean no limits
- data_limit : Number max of tuples to return during data extraction(default 0 no limit)- case_sensitive: Allow to preserve Oracle object name as they arewritten. Default is not.- skip_fkeys : Skip foreign key constraints extraction. Default to 0(extraction)- skip_pkeys : Skip primary keys extraction. Default to 0 (extraction) - skip_ukeys : Skip unique column constraints extraction. Default to 0(extraction)- skip_indices : Skip all other index types extraction. Default to 0(extraction)- skip_checks : Skip checks constraints extraction. Default to 0(extraction)- keep_pkey_names : By default, primary key names in the source databaseare ignored, and default key names are created in the target database. If this is set to true, primary key names are kept.- bzip2 : Path to the Bzip2 program to compress data export. Default/usr/bin/bzip2- gen_user_pwd : When set to 1 this will replace default password'change_my_secret' with a random string. Need String::Random- fkey_deferrable: Force foreign key constraints to be exported asdeferrable. Default 0: asis.- defer_fkey : Force all foreign key constraints to be deferred duringdata import. Default 0: asis.- pg_numeric_type: Convert Oracle NUMBER data type to internal PG datatype instead of use of the slow numeric(p,s) data type.- default_numeric: NUMBER(x) without precision are by default convertedto float. You can overwrite this value by any PG type.- keep_pkey_names: preserve oracle primary keys name. Default ignored. - pg_supports_inout: Allow PG support of in/ou/inout function parameterMust be used with PostgreSQL > 8.1. Default no support (backward compatibility).- pg_supports_role: Allow PG support of roles instead of user/group.Default no support (backward compatibility).- disable_triggers: Disables triggers on all tables in COPY orDATA mode.- disable_sequence: Disables alter sequence on all tables in COPY orDATA mode.- noescape: disable character escaping during data export.
- datatype: allow to redefine datatype Oracle to PostgreSQl conversion. - binmode: force perl to use a given binary mode for output. Default is':raw';- sysusers: allow to add other system users to the default exclusionlist (SYS,SYSTEM,DBSNMP,OUTLN,PERFSTAT).- ora_sensitive: force to use Oracle case sensitive table/view name - Attempt that this list should grow a little more because all
initialization is done by this way. - Specials configuration options to handle character encoding:
----------------------------------------------------------- - NLS_LANG
- If you experience any issue where mutibyte characters are being
substituted with replacement characters during export try to set the
NLS_LANG configutation directive to the Oracle encoding. This may help a lot especially with UTF8 encoding. - BINMODE
- If you experience the perl message: "Wide character in print", this
warning happens when you output a Unicode string to a non-unicode
filehandle. You can force perl to use binary mode for output by
setting the BINMODE configuration option to a given encoding. If you
set it to 'utf8', it will force printing like this: binmode OUTFH,
":utf8"; By default Ora2Pg open the output file in 'raw' binary mode. - Exporting Oracle VIEW as PostgreSQL table:
----------------------------------------- - Since version 4.10 you can export Oracle VIEW as PostgreSQL table
simply by setting TYPE configuration option to TABLE and COPY or DATA
and specifying your views in the TABLES configuration option. Then if
Ora2Pg don't find your tables name it automatically deduce that it must search in views and if it found the view it will extract his schema (if TYPE=TABLE) into a PG create table form, or it will extract the datas
(if TYPE=COPY or DATA) following the view schema. - Case sensitive table name in Oracle:
----------------------------------- - Since version 4.11 you can extract/export Oracle database with case
sensitive table/view name defined. This requier the use of quoted
table/view names during Oracle querying. Set the configuration option
ORA_SENSITIVE to 1 to enable this feature. By default it is off. - export_data FILENAME
- Print SQL data output to a filename or to STDOUT if no file is given.
- Must be used only if type option is set to DATA or COPY
- export_sql FILENAME
- Print SQL conversion output to a filename or simply return these data
if no file is given. - send_to_pgdb DEST_DATASRC DEST_USER DEST_PASSWD
- Open a DB handle to a PostgreSQL database
- modify_struct TABLE_NAME ARRAYOF_FIELDNAME
- Modify a table structure during export. Only given fieldname will be
exported. - replace_tables HASH
- Modify tables table names during export.
- replace_cols HASH
- Modify column names during export.
- set_where_clause HASH
- Add WHERE clause during data export on specifics table and/or all table
PRIVATE METHODS
- _init HASH_OPTIONS
- Initialize a Ora2Pg object instance with a connexion to the Oracle
database. - _grants
- This function is used to retrieve all privilege information.
- It extract all Oracle's ROLES to convert them as Postgres groups and
search all users associated to these roles. - Set the main hash $self->{groups}. Set the main hash $self->{grants}.
- _sequences
- This function is used to retrieve all sequences information.
- Set the main hash $self->{sequences}.
- _triggers
- This function is used to retrieve all triggers information.
- Set the main hash $self->{triggers}.
- _functions
- This function is used to retrieve all functions information.
- Set the main hash $self->{functions}.
- _packages
- This function is used to retrieve all packages information.
- Set the main hash $self->{packages}.
- _tables
- This function is used to retrieve all table information.
- Set the main hash of the database structure $self->{tables}. Keys are
the names of all tables retrieved from the current database. Each table
information compose an array associated to the table_info key as array
reference. In other way:
$self->{tables}{$class_name}{table_info} = [(OWNER,TYPE)]; - DBI TYPE can be TABLE, VIEW, SYSTEM TABLE, GLOBAL TEMPORARY, LOCAL
TEMPORARY, ALIAS, SYNONYM or a data source specific type identifier.
This only extract TABLE type. - It also get the following informations in the DBI object to affect the
main hash of the database structure :
$self->{tables}{$class_name}{field_name} = $sth->{NAME};
$self->{tables}{$class_name}{field_type} = $sth->{TYPE}; - It also call these other private subroutine to affect the main hash of
the database structure :
@{$self->{tables}{$class_name}{column_info}} = $self->_column_info($class_name, $owner);
%{$self->{tables}{$class_name}{unique_key}} = $self->_unique_key($class_name, $owner);
@{$self->{tables}{$class_name}{foreign_key}} = $self->_foreign_key($class_name, $owner);
%{$self->{tables}{$class_name}{check_constraint}} = $self->_check_constraint($class_name, $owner); - _views
- This function is used to retrieve all views information.
- Set the main hash of the views definition $self->{views}. Keys are the
names of all views retrieved from the current database values are the
text definition of the views. - It then set the main hash as follow:
# Definition of the view
$self->{views}{$table}{text} = $view_infos{$table}; - _tablespaces
- This function is used to retrieve all Oracle Tablespaces information.
- Set the main hash $self->{tablespaces}.
- _get_sql_data
- Returns a string containing the entire SQL Schema definition compatible with PostgreSQL
- _extract_sequence_info
- This function adjusts the last used number returned from the sequence
used by the source database. Result is a SQL script assigning the new
start value to the sequences found. - _get_data TABLE
- This function implements a Oracle-native data extraction.
- Return a list of array reference containing the data
- _sql_type INTERNAL_TYPE LENGTH PRECISION SCALE
- This function return the PostgreSQL datatype corresponding to the
Oracle internal type. - _column_info TABLE OWNER
- This function implements a Oracle-native column information.
- Return a list of array reference containing the following informations for each column the given a table
- [(
column name,
column type,
column length,
nullable column,
default value )] - _unique_key TABLE OWNER
- This function implements a Oracle-native unique (including primary) key column information.
- Returns a hash of hashes in the following form:
( constraintname => (type => 'PRIMARY',columns => ('a', 'b', 'c')),constraintname => (type => 'UNIQUE',columns => ('b', 'c', 'd')),etc.
- )
- _check_constraint TABLE OWNER
- This function implements a Oracle-native check constraint information.
- Return a hash of list of all column name defined as check constraint
for the given table and constraint name. - _foreign_key TABLE OWNER
- This function implements a Oracle-native foreign key reference
information. - Return a list of hash of hash of array reference. Ouuf! Nothing very
difficult. The first hash is composed of all foreign key name. The
second hash just have two key known as 'local' and remote'
corresponding to the local table where the foreign key is defined and
the remote table where the key refer. - The foreign key name is composed as follow:
'local_table_name->remote_table_name' - Foreign key data consist in two array representing at the same indice
the local field and the remote field where the first one refer to the
second. Just like this:
@{$link{$fkey_name}{local}} = @local_columns;
@{$link{$fkey_name}{remote}} = @remote_columns; - _get_users
- This function implements a Oracle-native users information.
- Return a hash of all users as an array.
- _get_roles
- This function implements a Oracle-native roles information.
- Return a hash of all groups (roles) as an array of associated users.
- _get_all_roles
- This function retrieve all Oracle roles information.
- Return a hash of all roles as an array of associated users.
- _get_all_grants
- This function implements a Oracle-native user privilege information.
- Return a hash of all tables grants as an array of associated users.
- _get_indexes TABLE OWNER
- This function implements a Oracle-native indexes information.
- Return hash of array containing all unique index and a hash of array of all indexes name which are not primary keys for the given table.
- _get_sequences
- This function implements a Oracle-native sequences information.
- Return a hash of array of sequence name with MIN_VALUE, MAX_VALUE,
INCREMENT and LAST_NUMBER for the given table. - _get_views
- This function implements a Oracle-native views information.
- Return a hash of view name with the SQL query it is based on.
- _alias_info
- This function implements a Oracle-native column information.
- Return a list of array reference containing the following informations for each alias of the given view
- [(
column name,
column id )] - _get_triggers
- This function implements a Oracle-native triggers information.
- Return an array of refarray of all triggers informations
- _get_functions
- This function implements a Oracle-native functions information.
- Return a hash of all function name with their PLSQL code
- _get_packages
- This function implements a Oracle-native packages information.
- Return a hash of all function name with their PLSQL code
- _table_info
- This function retrieve all Oracle-native tables information.
- Return a handle to a DB query statement
- _get_tablespaces
- This function implements a Oracle-native tablespaces information.
- Return a hash of array of tablespace name with system filepath for the given table.
- _get_schema
- This function return 1 if the requested schema is found in the database else return 0
AUTHOR
Gilles Darold <gilles@darold.net>
COPYRIGHT
Copyright (c) 2001-2008 Gilles Darold - All rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
BUGS
This perl module is in the same state as my knowledge regarding
database, it can move and not be compatible with older version so I
will do my best to give you official support for Ora2Pg. Your volontee
to help construct it and your contribution are welcome.
SEE ALSO
DBI, DBD::Oracle, DBD::Pg
ACKNOWLEDGEMENTS
- The following person help a lot with reports and help:
- Jason Servetar
Jean-Francois Ripouteau
Octavi Fors
Adriano Bonat
Thomas Reiss
Bozkurt Erkut from SONY - The following person help a lot with code review and bug fix:
Stephane Schildknecht
Jean-Paul Argudo
Jan Kester
Paolo Mattioli
Mike Wilhelm-hiltz
Jefferson Medeiros
Ian Boston
Thomas Wegner
Andreas Haumer
Marco Lombardo
Adam Sah and Zedo Inc
Antonios Christofide and National Technical University of Athens Josian Larcheveque
Stephane Silly
David Cotter - Alatto Technologies Ltd
Wojciech Szenajch
Richard Chen
Sergio Freire
Matt Miller
Rene Bentzen
Schnabl Andrea
Ugo Brunel - Bull
Bernd Helmle - credativ GmbH
Peter Eisentraut
Marc Cousin- My best regards to all of them.