Home > Software design >  How to import a MySQL dump into a PostgreSQL database?
How to import a MySQL dump into a PostgreSQL database?

Time:11-24

I have a MySQL dump generated by PHPMyAdmin, and I need to import it into a Postgresql database, but I dont know if it's even possible. I've seen people recommending pgloader but seens a little confusing on how to do it. Also I'm on windows if its relevant at all.

I only need the tables, so I'm not concerned about the data in the old or in the new database.

It's not that big too, only 84 tables. But big enough for me to write it.

Thank you!

CodePudding user response:

You can use pgloader.

You need to install it, and then run a simple lisp script (script.lisp) with the following 3 lines:

/* content of the script.lisp */
LOAD DATABASE
FROM mysql://dbuser@localhost/dbname
INTO postgresql://dbuser@localhost/dbname;


/*run this in the terminal*/
pgloader script.lisp

And after that, your PostgreSQL DB will have all of the information that you had in your MySQL DB.

CodePudding user response:

There are a lot of resources to do this. None of them are simple.

  • pgloader which you mentioned is among the many tools listed on this page: https://wiki.postgresql.org/wiki/Converting_from_other_Databases_to_PostgreSQL#MySQL
  • MySQLdump is supposed to have an option --compatible=postgres but don't rely on that. It makes some changes to its output, but not enough to be fully compatible with PostgreSQL syntax.
  • Another option is to dump tables to CSV files with mysqldump --tables instead of dumping to SQL format. Then you can bulk-load the CSV files one by one with the COPY statement in PostgreSQL.

If your MySQL database contains views or stored routines (procedures, functions, triggers, or events), then in general those can't be converted by any tool. The PostgreSQL language for stored routines is too different from MySQL. You must just start over and code routines in PostgreSQL that do equivalent logic, but coded in a way more idiomatic for PostgreSQL.

  • Related