我正在考虑从 MySQL 切换到 PostgreSQL。
使用 PostgreSQL 有哪些提示、技巧和陷阱?
MySQLer 应该注意什么?
另请参阅:PostgreSQL 与 MySQL 有什么不同?
另请参阅:从 MySQL 迁移到 PostgreSQL
注意 - 我不认为这是重复的。 特别是答案的类型非常不同,这里的答案有更多的实现细节,这正是我正在寻找的
I am contemplating a switch from MySQL to PostgreSQL.
What are your tips, tricks and gotchas for working with PostgreSQL?
What should a MySQLer look out for?
See also: How different is PostgreSQL to MySQL?
See also: Migrate from MySQL to PostgreSQL
Note - I don't think this is a duplicate. In particular the type of answers are quite diffferent and the responses here have much more implementation detail, which is what I was looking for
发布评论
评论(6)
我自己刚刚经历过这个,好吧,我仍然是......
INSERT IGNORE
和REPLACE
LOAD DATA INFILE< /code> (
COPY
很接近,但还不够接近)autoincrement
更改为SERIAL
ON 子句的 >INNER JOIN 不可能发生,使用
CROSS JOIN
或类似的COUNT(*)
可能会很疯狂慢interval
与 Postgresinterval< /code> (时间间隔)
AS
CALL proc();
; 将proc()
重写为函数并SELECT proc();
。Just went through this myself, well I still am...
INSERT IGNORE
andREPLACE
LOAD DATA INFILE
(COPY
is close, but not close enough)autoincrement
toSERIAL
INNER JOIN
without anON
clause can't happen, useCROSS JOIN
or the likeCOUNT(*)
can be crazy slowinterval
vs. Postgresinterval
(for time intervals)AS
CALL proc();
; rewriteproc()
as a function andSELECT proc();
.这将是一项艰巨的任务,因为您必须测试整个代码库 - 每个查询,任何地方,
在操作上,您需要查看:
在考虑这样的举措之前,您肯定必须进行大量的性能测试。
这些成本使得迁移到不同的数据库对于大多数重要的应用程序来说过于昂贵。 仔细考虑一下好处和执行上述所有操作的巨大成本。
如果您在一个不平凡的应用程序中花费不到三个月的时间,我会感到惊讶,在此期间您将无法继续常规开发。
It is going to be a massive task as you'll have to test your entire code-base - every single query, anywhere, for
Operationally you will need to look at:
You will definitely have to do major amounts of performance testing before considering such a move.
These costs make moving to a different database too expensive for most nontrivial apps. Consider the benefits VERY carefully against the vast, vast costs of doing all of the above.
I would be surprised if it takes you less than three months, in a nontrivial application, during which time you won't be able to continue regular development.
您可以尝试包含最常见问题的 PostgreSQL 陷阱 。 一般来说,PostgreSQL 文档也非常好,所以也请把它放在枕头下。
另外,pgsql wiki 上的从 MySQL 转换为 PostgreSQL。
You could try PostgreSQL gotchas that contains the most common issues. Generally, the PostgreSQL documentation is pretty good too, so keep that under your pillow as well.
Also, Converting from MySQL to PostgreSQL on the pgsql wiki.
我发现这个脚本将连接到您的 MySQL 数据库和 PostgreSQL 数据库并仅传输内容。 它对我来说就像一种魅力。
https://github.com/philipsoutham/py-mysql2pgsql
通过
在任意文件夹中运行
安装,它将创建一个模板设置文件(mysql2pgsql.yml),您可以编辑并在其中输入数据库的详细信息。
我必须安装 argparse 才能正常工作。
填写完数据库详细信息后,只需
在与设置文件相同的文件夹中再次运行它,然后,您就完成了。 它没有在屏幕上打印任何内容,但之后我的数据库被完全复制。
I found this script that will connect to your MySQL database and your PostgreSQL database and just transfer the contents. It worked like a charm for me.
https://github.com/philipsoutham/py-mysql2pgsql
Installed by
Run
in any folder, and it will create a template settings file for you (mysql2pgsql.yml) that you can edit and enter your databases' details in.
I had to install argparse for it to work.
When your database details are filled in, just run it again
in the same folder as the settings file, and wham, you are done. It didn't print anything to the screen, but my database was fully copied afterwards.
在转换之前,通过启动服务器将 MySQL 设置为 ANSI 严格性: --transaction-isolation=SERIALIZABLE --sql-mode=ANSI
确保您没有使用 MyIsam 表。
MySQL 允许大量不应该进行的转换; pg 需要演员阵容。
您的存储过程、函数和触发器将必须重新编写。 pg 为您提供了这些语言的选择,但您必须安装这些语言; 它不像 MySQL 那样用户友好。
pg 将只允许在选择列表中包含 group by 或聚合的列; 如果您这样做,MySQL 将通过选择组中的第一个值来作弊。
MySQL 添加了一堆扩展:不等于运算符可以是
!=
就像 C 中一样,它允许 '&&' 作为“和”、“||”的同义词 表示“或”等。特别是,pg 使用“||” 表示字符串连接。基本上,pg 是非常严格的 ANSI,而 MySQL 不是。 我强烈建议在转换为 pg 之前让您的 MySQL 尽可能严格地符合 ANSI,然后在运行应用程序时检查是否有任何警告。
Before converting, set your MySQL to ANSI-strictness by starting the server with: --transaction-isolation=SERIALIZABLE --sql-mode=ANSI
Make sure you're not using MyIsam tables.
MySQL allows a lot of conversions it shouldn't; pg will require a cast.
Your stored procs, functions, and triggers will have to be re-written. pg gives you a choice of languages for these, but you have to install the languages; it's not as user friendly as MySQL.
pg will only allow in a select list columns that are in a group by or are aggregates; MySQL will cheat by selecting the first value in the group if you do this.
MySQL adds a bunch of extensions: the not-equal operator can be
!=
as in C, it allows '&&' as a synonym for 'and', '||' for 'or' etc. In particular, pg uses '||' to mean string catenation.Basically, pg is pretty strictly ANSI, MySQL isn't. I'd strongly suggest getting your MySQL to as strict an ANSI compliance as possible before converting to pg, then checking for any warnings when you run your applications.
除了移动数据库结构之外,你无法避免手动调整...
最可靠的传输数据方法(逐表传输,前提是结构相同):
最近一直在尝试其他方法(比如带有大量选项的 mysqldump + sed 等),但没有什么比这更好的了。
当结构沿途发生变化时,这种方法还提供了一定的灵活性 - 只需编写适当的 SELECT 即可。
Aside of moving database structure, where you cannot avoid manual adjustments...
The most reliable method of transferring data (table by table, provided that the structures are the same):
Have been trying every other approach recently (like mysqldump with tons of options + sed etc.), but nothing worked as nice as this.
This approach also allows for some flexibility when structure is changed along the way - just write an appropriate SELECT.