Conversión de datos a PostgreSQL

From PostgreSQL wiki
Jump to navigationJump to search

DBase II, III, IV+

MS Access

Introducción

Estas página es intento de crear una guía rápida de usurario para convertir datos de MS Access a PostgreSQL. Esta basada en la experiencia del autor Microsoft Access to PostgreSQL Conversion by Jon Hutchings (2001-07-20), no se pretende con este documento sea utilizado como guía de conversión para otras versiones.

Paso 1 - tener corriendo en su Workstation MS Access

The first stage in the conversion process is to download the PostgreSQL upsizing tool. This can be obtained from the authors website. I strongly recommend reading the authors documentation and also note the limitations listed below of this tool.

This tool takes the form of an MS Access 97 database.

Once opened, the main screen (frmmain) has a number of options relating to the SQL conversion.

Select the database that you want to convert., analyse it using the Analyse function and amend any parameters you need to change.

It is important to fill out the SQL database name field with the name you wish to give your final postgres database otherwise you will have more work to do later.

Check the Export data option to move the data across - amend the file locations in the Miscellaneous options section if necessary.

Use Create SQL to make the SQL files and then Export SQL. This will place all the files needed in stage 2 in the directory specified on the Miscellaneous tab of the amend Parameters screen within pgupt.

FTP these files to a suitable empty directory on your postgres server.

Paso 2 - tener corriendo en su Servidor Linux PostgreSQL

Log into your server and change to the directory where you have uploaded the files

If you did not give the database a name earlier then the following must be performed before proceeding

   * edit the loadd.sh file (for example joe loadd.sh will open the file in the joe editor). 
   * Change all references to "test" in the file to the name which you want to give your database. 
   * HINT - Don't include non alphanumeric charaters in your database name (i.e. don't use -_ '?/!"\| etc.) 
   * Save and close the file. 
   * Open the create_schema.sql file and examine the table structure. Ensure that the fieldnames and data types are correct. 

Issue the following command to make the loadd.sh executable

chmod u+x loadd.sh

Execute the the loadd.sh script using the following command at your shell prompt.

./loadd.sh 2> output1

This will execute the script and put the errors and output in a file called output1. If everything is working correctly the screen should display a CREATE for each table in the database, if you only see two create statements and you have three tables , then one of the tables has not been converted. In this case examine the log file (output1 in this case), for details. A common cause for this is the naming of tables or fields using SQL reserved words. For a list of these see the Postgresql documentation for your version of postgres.

WARNING: Once you have succesfully converted your database and it is in use you should NOT rerun this script as it will reinitialize your database back to the state in which it was when you converted it. I suggest that when your database is working you remove the execute permissions on the loadd.sh script to prevent accidential execution.

Paso 3 - Testando su base de datos utilizando la consola del servidor.

Once you are happy that the conversion has worked, you can test your database by connecting to it with the Postgresql monitor in the normal way. For example, if you called your database "customerdata" you would use

psql customerdata

at a shell prompt on your linux postgres server to connect (you will probably need other command line options for usernames and passwords etc.). Once connected you should use the \dt command to display a list of tables and compare it with the source database. If these look correct, try some simple queries such as

select * FROM <tablename> ;

This will display (at some speed !) all the records, but more usefully at the end you will get a record count for that table which you can compare to ensure all the data has been converted correctly.

Paso 4 -Configuración de ODBC

If you wish to continue to use Microsoft Access97 to interact with the converted database you will need to set up a suitable ODBC driver and datasource on the client PC(s).

The first stage is to download the PostgreSQL ODBC driver. This can be obtained via the PostgreSQL ODBC website or you can download alternative PostgreSQL ODBC driver from Devart website.

Once installed, use the ODBC control panel on your Windows 9x/NT 4.0/Windows 2000 PC and add a new User DSN or user data source. Select the PostgreSQL driver and fill in the details with the server name, port, database name, username and password, for your postgresql server.

Paso 5 - Configuración en MS Access.

To create an Microsoft Access based front end to the newly created PostgreSQL database, do the following:

Make a copy of your original Access database.

Open it in Microsoft Access.

Select the Tables tab.

Select New.

Select Link Table.

In the Files of Type list select ODBC Databases.

In the data sources dialogue find the data source you created earlier (on NT 4.0 this will typically be on the Machine Data Sources tab).

Select the tables which you wish to link.

Once linked you are free to remove the old local tables and compact the database.

NOTE - If any field names have changed during conversion (usually due to the use of non-alphabetic characters in the field and table names) then your queries, forms and reports etc. will need updating to reflect this.

Limitaciones

En primer lugar, esta script solamente fue testado con Postgres 6.5.3 y Access 97, puede trabajar con otras combinaciones, pero no ha sido testado por el autor.

En segundo lugar, ther are some limitations to this tool, which I have discovered, these are

  1. The tool expects at least one relationship, so simple single table database conversions will fail.
  2. The tool assumes you will be using the standard postgesql server port.
  3. Using this tool it is possible to generate trigger and function names longer than 32 characters which can cause postgresql some problems

Oracle para PostgreSQL

Instalar ora2pg

Para la instalación del ora2pg seguir el siguiente paso:

aptitude install ora2pg

Importando los datos hacia una Nueva Instalación de Oracle

Importar un (dump) de datos hacia Oracle es un proceso de dos fases :

   * Crear usuarios requeridos
   * Importar los datos 

Crear usuario

Desde una computadora con un browser y ssh instalados, correr el comando

ssh -L 9999:localhost:8080 root@192.168.1.101

Una vez que nos hemos autenticado apuntar el browser a la siguiente dirección

http://localhost:9999/apex 

y debe ver la consola de administración de Oracle. Crea desde ahí los usuarios requerido para Oracle.

Importar los Datos

Para importar los datos hacia Oracle, primero copie el dump EXPDAT.DMP en alguna carpeta de la PC cliente: Ejemplo.-

scp EXPDAT.DMP root@192.168.1.101:.

Ahora loguearse como root en la maquina cliente, mueva el dump de oracle para el directorio home del usuario oracle, cambiarse al usuario de oracle y modificar el environment:

ssh root@192.168.1.101
mv EXPDAT.DMP ~oracle/
su - oracle
export ORACLE_HOME=/usr/lib/oracle/product/10.2.0/server
export ORACLE_SID=XE
export PATH=$PATH:$ORACLE_HOME/bin

Finalmente, corra el comando imp para importar el dump hacia la base de datos:

imp system/manager FILE=EXPDAT.DMP FULL=Y

Si alcanza a ver al final el mensaje ``Import terminated successfully without warnings. . Felicitaciones, ya usted tiene la base de datos de Oracle corriendo! Exportando los Datos para PostgreSQL

Ya estamos listos para exportar la base de datos de Oracle. Tenemos que realizar algunas configuraciones y luego podemos importar la base de datos hacia script de Postgres e importalos hacia PostgreSQL.

Vamos a utilizar la herramienta ora2pg, el cual ya habíamos instalado. Necesitamos preparar el archivo de configuración ora2pg antes de utilizarlo, primero copiamos archivo configuración de ejemplo al directorio actual como el usuario oracle:

su - oracle
export ORACLE_HOME=/usr/lib/oracle/product/10.2.0/server
export ORACLE_SID=XE
export PATH=$PATH:$ORACLE_HOME/bin

Usted quizás desee poner las 3 sentencias export debajo dentro del .bash_profile del usuario oracle para que las variables queden puestas automáticamente cada vez que se haga 'su'.

cp /usr/share/doc/ora2pg/examples/ora2pg.conf.gz .
gunzip ora2pg.conf.gz

Editar el ora2pg.conf realizar los cambios según la tabla:

Linea Original: ORACLE_HOME /usr/local/oracle/oracle816 
Cambiar para: ORACLE_HOME /usr/lib/oracle/product/10.2.0/server 
Linea Original: ORACLE_DSN dbi:Oracle:host=thedb.mydom.fr;sid=TEST 
Cambiar para: ORACLE_DSN dbi:Oracle:host=127.0.0.1;sid=XE 
Linea Original: # EXPORT_SCHEMA 1 
Cambiar para: EXPORT_SCHEMA 1 
Linea Original: # SCHEMA APPS 
Cambiar para: SCHEMA <name of database schema owner that you created earlier in APEX> 
Linea Original: TYPE TABLE 
Cambiar para: (see under) 

Desafortunadamente ora2pg no permite dumping de toda la base de datos de un tiro, por ello debemos de exportar diferentes componentes de la base de datos en múltiples comandos. Los componentes que pueden ser exportados son:

TABLE 
PACKAGE 
COPY (or DATA, but COPY is faster) 
VIEW 
GRANT 
SEQUENCE 
TRIGGER 
FUNCTION 
PROCEDURE 
TABLESPACE 

Salve el ora2pg.conf y corra la siguientes script para exportar cada componente nombrado a un fichero llamado <component>.out dentro del directorio actual:

for i in TABLE PACKAGE COPY VIEW GRANT SEQUENCE TRIGGER FUNCTION PROCEDURE TABLESPACE ;
do 
 perl -p -i -e "s/^TYPE\s+.*/TYPE $i/; s/^OUTPUT\s+.*/OUTPUT $i.sql/" ora2pg.conf; 
 ora2pg ora2pg.conf; 
done

Como resultado se obtienen los siguientes archivos dentro del directorio actual, los cuales pueden ser corridos como scripts de PostgreSQL:

TABLE.sql 
PACKAGE.sql 
COPY.sql 
VIEW.sql 
GRANT.sql 
SEQUENCE.sql 
TRIGGER.sql 
FUNCTION.sql 
PROCEDURE.sql 
TABLESPACE.sql


MySQL

Utilizar la script "mysql2pgsql.perl"

A considerar:

* No se migra estructura de un atributo con comentarios.
* Los atributos no deben tener caracteres raros Ej.ñ.

Descargar la scipt y salvar con el mysql2pgsql.perl:

  • mysql2pgsql - A Perl script used to convert a MySQL database dump to PostgreSQL-compatible format, by Maxim Rudensky and Valentine Danilchuk

Pasos para utilizarla:

  • Sacar el backup de la estructura de base de datos en Mysql.
 mysqldump --database --table --no-data > mysql_.sql
  • Convertir para que PostgreSql lo pueda cargar.
 perl mysql2pgsql.perl mysql_.sql postgres_.sql --nodrop
    • La opción --nodrop elimina las eliminaciones de las tablas, si se quiere ver mas opciones --help.
su postgres
createdb NombreDB
psql -f postgres_.sql -u dad NombreDB
    • Para los datos. Exportar de MySql solo datos con la opción de --complete-insert y -c.