导出MySQL数据库并将其导入PostgreSQL数据库

时间:2016-06-15 16:17:11

标签: php python mysql database postgresql

我正在尝试导出 MySQL 数据库并将其导入 PostgreSQL 数据库。

我试过

使用此mysql-postgresql-converter python脚本

我有一个PHP脚本

if (!ini_get('date.timezone')) {
    ini_set('date.timezone', 'America/New_York');
}


//Configuration
$db          = 'local';
$username    = 'root';
$hostname    = '127.0.0.1';
$password    = '';
$date        = date('Y-m-d');
$export_type = 'mysql'; // option : mysql | psql
$file_name   = $date.'-portal';
$file_path   = $file_name;

if($export_type == 'mysql'){
    // MySQL Export
    $cmd = 'mysqldump --compatible=postgresql --default-character-set=utf8 -r '.$db.'.mysql -u root '.$db;
    echo $cmd. PHP_EOL;
    exec($cmd);
}

// Convert
$import = 'python db_converter.py '.$db.'.mysql '.$db.'.psql';
echo $import. PHP_EOL;
exec($import);

//Import to PostSQL
$import = 'psql  -U bheng  -d portal -f '.$db.'.psql';
echo $import. PHP_EOL;
exec($import);

结果

我创建了2个文件

1- local.mysql 2- local.psql

我一跑完

psql  -U bheng  -d portal -f local.psql

我得到了

psql:local.psql:12: ERROR:  relation "caches" already exists
psql:local.psql:14: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:52: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:54: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:63: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:65: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:78: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:80: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:92: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:94: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:110: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:112: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:116: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:118: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:131: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:133: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:144: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:146: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:158: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:160: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:179: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:181: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:195: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:197: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:247: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:249: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:256: ERROR:  cannot cast type smallint to boolean
LINE 1: ...LT, ALTER COLUMN "sign_enable" TYPE boolean USING CAST("sign...
                                                             ^
psql:local.psql:257: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:258: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:263: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:264: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:265: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:266: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:267: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:268: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:269: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:270: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:271: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:272: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:273: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:274: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:275: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:276: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:277: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:278: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:279: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:280: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:281: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:282: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:283: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:284: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:285: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:286: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:287: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:288: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:289: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:290: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:291: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:292: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:293: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:294: ERROR:  current transaction is aborted, commands ignored until end of transaction block
psql:local.psql:295: ERROR:  current transaction is aborted, commands ignored until end of transaction block

对此的任何提示/建议将不胜感激!

1 个答案:

答案 0 :(得分:0)

您可以使用pgloader。

sudo apt-get install pgloader

使用:

pgloader mysql:// user:pass @ host / database postgresql:// user:pass @ host / database

示例:

pgloader mysql://root:root@127.0.0.1:3306/mydbinmysql postgresql://postgres:postgres@localhost:5432/mydbexportedonpgsql

请事先在postgres中创建要导出到的新数据库。在上面的例子中mydbexportedonpgsql。