【问题标题】:Export MySQL database and import it to PostgreSQL database导出 MySQL 数据库并将其导入 PostgreSQL 数据库
【发布时间】:2016-10-16 21:08:15
【问题描述】:

我正在尝试导出 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

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

【问题讨论】:

  • 重新考虑使用该 GitHub 脚本,因为它提到:此脚本是为我们特定的数据库和列要求而设计的。因此,您的数据库中可能有一些语法/类型未在上述脚本中处理,导致 Postgre 错误。
  • 考虑导出/导入 csv 文件或使用 php/python 连接两个服务器并运行 SQL DML 和 DDL 语句的漫长方式。
  • 好的,谢谢您的建议。我会研究那些导出/导入 csv 文件。

标签: php python mysql database postgresql


【解决方案1】:

你可以使用 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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-26
    • 2011-07-22
    • 2014-05-05
    • 2015-05-21
    • 2012-10-20
    • 2018-02-05
    • 1970-01-01
    相关资源
    最近更新 更多