Trying to migrate a database from MySQL to PostgreSQL. All the documentation I have read covers, in great detail, how to migrate the structure. I have found very little documentation on migrating the data. The schema has 13 tables (which have been migrated successfully) and 9 GB of data.
MySQL version: 5.1.x
PostgreSQL version: 8.4.x
I want to use the R programming language to analyze the data using SQL select statements; PostgreSQL has PL/R, but MySQL has nothing (as far as I can tell).
Create the database location (/var has insufficient space; also dislike having the PostgreSQL version number everywhere -- upgrading would break scripts!):
sudo mkdir -p /home/postgres/mainsudo cp -Rp /var/lib/postgresql/8.4/main /home/postgressudo chown -R postgres.postgres /home/postgressudo chmod -R 700 /home/postgressudo usermod -d /home/postgres/ postgresAll good to here. Next, restart the server and configure the database using these installation instructions:
sudo apt-get install postgresql pgadmin3sudo /etc/init.d/postgresql-8.4 stopsudo vi /etc/postgresql/8.4/main/postgresql.confdata_directory to /home/postgres/main
sudo /etc/init.d/postgresql-8.4 startsudo -u postgres psql postgres\password postgressudo -u postgres createdb climatepgadmin3Use pgadmin3 to configure the database and create a schema.
The episode continues in a remote shell known as bash, with both databases running, and the installation of a set of tools with a rather unusual logo: SQL Fairy.
perl Makefile.PLsudo make installsudo apt-get install perl-doc (strangely, it is not called perldoc)perldoc SQL::Translator::ManualExtract a PostgreSQL-friendly DDL and all the MySQL data:
sqlt -f DBI --dsn dbi:mysql:climate --db-user user --db-password password -t PostgreSQL > climate-pg-ddl.sqlclimate-pg-ddl.sql and convert the identifiers to lowercase, and insert the schema reference (using VIM):
:%s/"\([A-Z_]*\)"/\L\1/g:%s/ TABLE / TABLE climate./g:%s/ on / on climate./gmysqldump --skip-add-locks --complete-insert --no-create-db --no-create-info --quick --result-file="climate-my.sql" --databases climate --skip-comments -u root -pIt might be worthwhile to simply rename the tables and columns in MySQL to lowercase:
select concat( 'RENAME TABLE climate.', TABLE_NAME, ' to climate.', lower(TABLE_NAME), ';' ) from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA='climate';Recreate the structure in PostgreSQL as follows:
pgadmin3 (switch to it)climate-pg-ddl.sql
TABLE " replace with TABLE climate." (insert the schema name climate)on " replace with on climate." (insert the schema name climate)F5 to executeThis results in:
Query returned successfully with no result in 122 ms.
At this point I am stumped.
climate-my.sql to climate-pg.sql so that they can be executed against PostgreSQL?A fair bit of information was needed to get this far:
Thank you!
Postgres offers a wider variety of data types than MySQL. If your application deals with any of the unique data types it has available, or unstructured data, PostgreSQL may be a better pick. If you're using only basic character and numeric data types, both databases will suit you.
What I usually do for such migrations is two-fold:
Then do the conversion, and write a program in whatever language you are most comfortable with that accomplishes the following:
Redesign the tables for PostgreSQL to take advantage of its features.
If you just do something like use a sed script to convert the SQL dump from one format to the next, all you are doing is putting a MySQL database in a PostgreSQL server. You can do that, and there will still be some benefit from doing so, but if you're going to migrate, migrate fully.
It will involve a little bit more up-front time spent, but I have yet to come across a situation where it isn't worth it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With