Import MySQL dump to PostgreSQL database

后端 未结 17 869
北海茫月
北海茫月 2020-12-04 10:05

How can I import an \"xxxx.sql\" dump from MySQL to a PostgreSQL database?

17条回答
  •  天涯浪人
    2020-12-04 10:14

    With pgloader

    Get a recent version of pgloader; the one provided by Debian Jessie (as of 2019-01-27) is 3.1.0 and won't work since pgloader will error with

    Can not find file mysql://...
    Can not find file postgres://...
    

    Access to MySQL source

    First, make sure you can establish a connection to mysqld on the server running MySQL using

    telnet theserverwithmysql 3306
    

    If that fails with

    Name or service not known

    log in to theserverwithmysql and edit the configuration file of mysqld. If you don't know where the config file is, use find / -name mysqld.cnf.

    In my case I had to change this line of mysqld.cnf

    # By default we only accept connections from localhost
    bind-address    = 127.0.0.1
    

    to

    bind-address    = *
    

    Mind that allowing access to your MySQL database from all addresses can pose a security risk, meaning you probably want to change that value back after the database migration.

    Make the changes to mysqld.cnf effective by restarting mysqld.

    Preparing the Postgres target

    Assuming you are logged in on the system that runs Postgres, create the database with

    createdb databasename
    

    The user for the Postgres database has to have sufficient privileges to create the schema, otherwise you'll run into

    permission denied for database databasename

    when calling pgloader. I got this error although the user had the right to create databases according to psql > \du.

    You can make sure of that in psql:

    GRANT ALL PRIVILEGES ON DATABASE databasename TO otherusername;
    

    Again, this might be privilege overkill and thus a security risk if you leave all those privileges with user otherusername.

    Migrate

    Finally, the command

    pgloader mysql://theusername:thepassword@theserverwithmysql/databasename postgresql://otherusername@localhost/databasename
    

    executed on the machine running Postgres should produce output that ends with a line like this:

    Total import time          ✓     877567   158.1 MB       1m11.230s
    

提交回复
热议问题