将 MySQL 转储导入到 PostgreSQL 数据库
如何将“xxxx.sql”转储从 MySQL 导入到 PostgreSQL 数据库?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何将“xxxx.sql”转储从 MySQL 导入到 PostgreSQL 数据库?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(18)
这个问题有点老了,但几天前我正在处理这种情况并发现 pgloader.io。
这是迄今为止最简单的方法,您需要安装它,然后运行一个简单的 lisp 脚本(script.lisp),其中包含以下 3 行:
之后您的 postgresql 数据库将具有您在 MySQL SB 中拥有的所有信息。
顺便说一句,请确保编译 pgloader,因为在撰写本文时,安装程序有一个错误。 (3.2.0版本)
This question is a little old but a few days ago I was dealing with this situation and found pgloader.io.
This is by far the easiest way of doing it, you need to install it, and then run a simple lisp script (script.lisp) with the following 3 lines:
And after that your postgresql DB will have all of the information that you had in your MySQL SB.
On a side note, make sure you compile pgloader since at the time of this post, the installer has a bug. (version 3.2.0)
Mac OS X
Mac OS X
不要指望不经过编辑就可以工作。也许需要大量的编辑工作。
mysqldump 有一个兼容性参数,
--兼容=名称
,其中“名称”可以是“oracle”或“postgresql”,但这并不能保证兼容性。我认为像 ANSI_QUOTES 这样的服务器设置也有一定的效果。如果您包含用于创建转储的完整命令以及收到的任何错误消息,而不是仅仅说“没有什么对我有用”,您将在这里获得更有用的帮助。
Don't expect that to work without editing. Maybe a lot of editing.
mysqldump has a compatibility argument,
--compatible=name
, where "name" can be "oracle" or "postgresql", but that doesn't guarantee compatibility. I think server settings like ANSI_QUOTES have some effect, too.You'll get more useful help here if you include the complete command you used to create the dump, along with any error messages you got instead of saying just "Nothing worked for me."
我发现最快(也是最完整)的方法是使用 Kettle。这还将生成所需的表、转换索引和其他所有内容。
mysqldump
兼容性参数不起作用。步骤:
从 http://kettle.pentaho.org/ 下载 Pentaho ETL(社区版本)
解压并运行Pentaho(spoon.sh/spoon.bat,取决于unix/windows)
创建一个新作业
为 MySQL 源创建数据库连接
(工具->向导->创建数据库连接)
为 PostgreSQL 源创建数据库连接(如上)
运行 < code>复制表向导(工具 -> 向导 -> 复制表)
The fastest (and most complete) way I found was to use Kettle. This will also generate the needed tables, convert the indexes and everything else. The
mysqldump
compatibility argument does not work.The steps:
Download Pentaho ETL from http://kettle.pentaho.org/ (community version)
Unzip and run Pentaho (spoon.sh/spoon.bat depending on unix/windows)
Create a new job
Create a database connection for the MySQL source
(Tools -> Wizard -> Create database connection)
Create a database connection for the PostgreSQL source (as above)
Run the
Copy Tables
wizard (Tools -> Wizard -> Copy Tables)Run the job
您可以从 MySQL 导出到 CSV,然后将 CSV 导入 PostgreSQL。
You could potentially export to CSV from MySQL and then import CSV into PostgreSQL.
献给 2015 年以后的 Google 员工。
我在这上面浪费了一整天的时间,想总结一下。
我已经尝试了 这篇文章由Alexandru Cotioras撰写(充满了绝望)。在提到的所有解决方案中,只有一个对我有用。
— lanyrd/mysql-postgresql-converter @ github.com (Python)
但仅此一点是不行的。当您导入新的转换后的转储文件时:
PostgreSQL
会告诉您来自MySQL
的混乱类型:因此您必须按照 此表。
简而言之:
您可以使用 regex 和任何很酷的编辑器来完成它。
MacVim
+替换
:For those Googlers who are in 2015+.
I've wasted all day on this and would like to sum things up.
I've tried all the solutions described at this article by Alexandru Cotioras (which is full of despair). Of all the solutions mentioned there only one worked for me.
— lanyrd/mysql-postgresql-converter @ github.com (Python)
But this alone won't do. When you'll be importing your new converted dump file:
PostgreSQL
will tell you about messed types fromMySQL
:So you'll have to fix those types manually as per this table.
In short it is:
You can use
regex
and any cool editor to get it done.MacVim
+Substitute
:您可以使用 pgloader。
使用:
You can use pgloader.
Using:
Mac/Win
下载 Navicat 试用版 14 天(我不明白 1300 美元) - 完整的企业包:
连接数据库 mysql 和 postgres
菜单 - 工具 - 数据传输
在第一个屏幕上连接两个数据库。仍在该屏幕上时,有一个常规/选项 - 在右侧的选项检查下检查 - 出现错误时继续
* 请注意,您可能想取消选中左侧的索引和键。您可以在 postgres 中轻松地重新分配它们。
至少将数据从 MySQL 导入 Postgres!
希望这有帮助!
Mac/Win
Download Navicat trial for 14 days (I don't understand $1300) - full enterprise package:
connect both databases mysql and postgres
menu - tools - data transfer
connect both dbs on this first screen. While still on this screen there is a general / options - under the options check on the right side check - continue on error
* note you probably want to un-check index's and keys on the left.. you can reassign them easily in postgres.
at least get your data from MySQL into Postgres!
hope this helps!
我有这个 bash 脚本来迁移数据,它不会创建表,因为它们是在迁移脚本中创建的,所以我只需要转换数据。我使用表列表来不从
migrations
和sessions
表导入数据。就是这样,刚刚经过测试:您将收到很多警告,您可以安全地忽略,如下所示:
I have this bash script to migrate the data, it doesn't create the tables because they are created in migration scripts, so I need only to convert the data. I use a list of the tables to not import data from the
migrations
andsessions
tables. Here it is, just tested:You will get a lot of warnings you can safely ignore like this:
使用
pgloader
获取最新版本的
pgloader
; Debian Jessie(截至 2019-01-27)提供的版本是 3.1.0,不会 时出错工作,因为
pgloader
将在访问MySQL源首先,确保您可以使用以下命令在运行MySQL的服务器上建立与
mysqld
的连接如果失败,
登录
theserverwithmysql
并编辑mysqld
的配置文件。如果您不知道配置文件在哪里,请使用find / -name mysqld.cnf
。就我而言,我必须更改 mysqld.cnf 的这一行
,以
记住允许从所有地址访问您的 MySQL 数据库可能会带来安全风险,这意味着您可能希望在数据库之后更改回该值迁移。
通过重新启动 mysqld 使对 mysqld.cnf 的更改生效。
准备 Postgres 目标
假设您登录到运行 Postgres 的系统,使用以下命令创建数据库
Postgres 数据库的用户必须具有足够的权限才能创建架构,否则您将遇到
调用 pgloader 时 。尽管用户有权根据 psql > 创建数据库,但我收到此错误\du。
您可以在
psql
中确保这一点:同样,如果您将所有这些权限留给用户
otherusername
,这可能会导致权限过度,从而带来安全风险。迁移
最后,在运行 Postgres 的机器上执行的命令
应该产生以如下行结尾的输出:
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 sincepgloader
will error withAccess to MySQL source
First, make sure you can establish a connection to
mysqld
on the server running MySQL usingIf that fails with
log in to
theserverwithmysql
and edit the configuration file ofmysqld
. If you don't know where the config file is, usefind / -name mysqld.cnf
.In my case I had to change this line of
mysqld.cnf
to
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 restartingmysqld
.Preparing the Postgres target
Assuming you are logged in on the system that runs Postgres, create the database with
The user for the Postgres database has to have sufficient privileges to create the schema, otherwise you'll run into
when calling
pgloader
. I got this error although the user had the right to create databases according topsql > \du
.You can make sure of that in
psql
:Again, this might be privilege overkill and thus a security risk if you leave all those privileges with user
otherusername
.Migrate
Finally, the command
executed on the machine running Postgres should produce output that ends with a line like this:
在新文件夹或根目录中进行克隆:
git 克隆 https://github.com/AnatolyUss/nmig.git
cd nmig
git checkout v5.5.0
nano config/config.json
签出后打开此文件。<前><代码>“来源”:{
“主机”:“本地主机”,
“端口”:3306,
“数据库”:“test_db”,
“字符集”:“utf8mb4”,
“支持BigNumbers”:正确,
“用户”:“根”,
“密码”:“0123456789”
}
“目标”: {
“主机”:“本地主机”,
“端口”:5432,
“数据库”:“test_db”,
“字符集”:“UTF8”,
“用户”:“postgres”,
“密码”:“0123456789”
}
npm 安装
npm run build
npm 启动
take a clone in new folder or root directory:
git clone https://github.com/AnatolyUss/nmig.git
cd nmig
git checkout v5.5.0
nano config/config.json
open this file after checkout.npm install
npm run build
npm start
无法将 Oracle(二进制)转储导入到 PostgreSQL。
如果 MySQL 转储采用纯 SQL 格式,则需要编辑该文件以使 PostgreSQL 的语法正确(例如,删除非标准反引号、删除 CREATE TABLE 语句的引擎定义、调整数据类型等)其他事物)
It is not possible to import an Oracle (binary) dump to PostgreSQL.
If the MySQL dump is in plain SQL format, you will need to edit the file to make the syntax correct for PostgreSQL (e.g. remove the non-standard backtick quoting, remove the engine definition for the CREATE TABLE statements adjust the data types and a lot of other things)
这是一个简单的程序,用于创建 mysql 数据库(honey)中的所有表并将其加载到 postgresql。 mysql 的类型转换是粗粒度的,但很容易细化。您必须手动重新创建索引:
Here is a simple program to create and load all tables in a mysql database (honey) to postgresql. Type conversion from mysql is coarse-grained but easily refined. You will have to recreate the indexes manually:
与大多数数据库迁移一样,并没有真正成熟的解决方案。
进行迁移时要记住以下一些想法:
记住这些。最好的方法可能是编写一个转换实用程序。祝您转换愉快!
As with most database migrations, there isn't really a cut and dried solution.
These are some ideas to keep in mind when doing a migration:
Keep these in mind. The best way is probably to write a conversion utility. Have a happy conversion!
最近,我不得不对许多大小约为 7 GB 的大型 .sql 文件执行此操作。即使是 VIM 编辑这些内容也很麻烦。最好的选择是将 .sql 导入 MySql,然后将其导出为 csv,然后将其导入到 Postgres。
但是,MySQL 导出为 csv 的速度非常慢,因为它运行 select * from yourtable 查询。如果您有一个大型数据库/表,我建议使用其他方法。一种方法是编写一个脚本,逐行读取 sql 插入,并使用字符串操作将其重新格式化为“Postgres 兼容”插入语句,然后在 Postgres 中执行这些语句
I had to do this recently to a lot of large .sql files approximately 7 GB in size. Even VIM had troubling editing those. Your best bet is to import the .sql into MySql and then export it as a csv which can be then imported to Postgres.
But, the MySQL export as a csv is horrendously slow as it runs the select * from yourtable query. If you have a large database/table I would suggest using some other method. One way is to write a script that reads the sql inserts line by line and uses string manipulation to reformat it to "Postgres-compliant" insert statements and then execute these statements in Postgres
我可以使用 SQuirreL SQL 客户端的 DBCopy 插件 将表从 MySQL 复制到 Postgres。
这不是来自转储,而是来自实时数据库之间。
I could copy tables from MySQL to Postgres using DBCopy Plugin for SQuirreL SQL Client.
This was not from a dump, but between live databases.
使用 xxx.sql 文件设置 MySQL 数据库并使用 FromMysqlToPostrgreSQL。非常容易使用,配置简短,而且工作起来很有魅力。它使用表上设置的主键、外键和索引导入数据库。如果您在配置文件中设置适当的标志,您甚至可以单独导入数据。
另请参阅此处:PG Wiki 页面
Use your xxx.sql file to set up a MySQL database and make use of FromMysqlToPostrgreSQL. Very easy to use, short configuration and works like a charm. It imports your database with the set primary keys, foreign keys and indices on the tables. You can even import data alone if you set appropriate flag in the config file.
See here too: PG Wiki Page
如果您使用 phpmyadmin,您可以将数据导出为 CSV,然后在 postgres 中导入会更容易。
If you are using phpmyadmin you can export your data as CSV and then it will be easier to import in postgres.