从 MySQL 切换到 PostgreSQL - 提示、技巧和陷阱?

发布于 2024-07-18 03:38:46 字数 507 浏览 5 评论 0 原文

我正在考虑从 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

小女人ら 2024-07-25 03:38:46

我自己刚刚经历过这个,好吧,我仍然是......

  • 区分大小写的文本
  • 缺少INSERT IGNOREREPLACE
  • 几乎到处都需要显式转换
  • 没有反引号
  • LOAD DATA INFILE< /code> (COPY 很接近,但还不够接近)
  • autoincrement 更改为 SERIAL
  • 虽然在 MySQL 中格式不好,但在 Postgres 中,一个 ON 子句的 >INNER JOIN 不可能发生,使用 CROSS JOIN 或类似的
  • COUNT(*) 可能会很疯狂慢
  • 数据库是用字符集编码的,而不是表
  • 你可以有多个数据库,有多个模式(MySQL 实际上只有一个数据库和多个模式)
  • 分区是不同的
  • MySQL interval 与 Postgres interval< /code> (时间间隔)
  • 隐式列重命名,Postgres 需要 AS
  • 无法在 Postgres 中同时更新多个表
  • Postgres 功能很强大。 所以没有CALL proc();; 将 proc() 重写为函数并 SELECT proc();

Just went through this myself, well I still am...

  • Case sensitive text
  • Lack of INSERT IGNORE and REPLACE
  • Explicit casting needed almost everywhere
  • No backticks
  • LOAD DATA INFILE (COPY is close, but not close enough)
  • Change autoincrement to SERIAL
  • Although bad form in MySQL, in Postgres, an INNER JOIN without an ON clause can't happen, use CROSS JOIN or the like
  • COUNT(*) can be crazy slow
  • Databases are encoded with character sets, not tables
  • You can have multiple databases, with multiple schemas (MySQL really has just one database and multiple schema)
  • Partitioning is different
  • MySQL interval vs. Postgres interval (for time intervals)
  • Implicit column renaming, Postgres requires AS
  • Cannot update multiple tables at the same time in Postgres
  • Postgres functions are powerful. So there is no CALL proc();; rewrite proc() as a function and SELECT proc();.
夜灵血窟げ 2024-07-25 03:38:46

这将是一项艰巨的任务,因为您必须测试整个代码库 - 每个查询,任何地方,

  • 语法
  • 正确的行为(即返回相同的结果)
  • 性能 - 例如是否有任何性能回归/改进,以及你能应付他们吗?
  • 错误处理 - 它们在错误条件下的行为不同,也许您的代码依赖于特定的错误代码

在操作上,您需要查看:

  • 备份/恢复
  • 磁盘空间利用率
  • 内存利用率
  • 一次性数据迁移 - 可能会很大/时间消耗任务
  • 失败时的回滚计划
  • 监控 - 您如何监控 MySQL,以及是否可以调整这些方法
  • (如果相关) - 复制

在考虑这样的举措之前,您肯定必须进行大量的性能测试。

这些成本使得迁移到不同的数据库对于大多数重要的应用程序来说过于昂贵。 仔细考虑一下好处和执行上述所有操作的巨大成本。

如果您在一个不平凡的应用程序中花费不到三个月的时间,我会感到惊讶,在此期间您将无法继续常规开发。

It is going to be a massive task as you'll have to test your entire code-base - every single query, anywhere, for

  • Syntax
  • Correct behaviour (i.e. returns the same results)
  • Performance - e.g. are there any performance regressions / improvements, and can you handle them?
  • Error handling - they do not behave the same under error conditions, maybe your code was relying on specific error codes

Operationally you will need to look at:

  • Backup/restore
  • Disc space utilisation
  • Memory utilisation
  • One-off data migration - could be a big / time consuming task
  • Rollback plan for if it fails
  • Monitoring - how are you monitoring your MySQL, and can those methods be adapted
  • (If relevant) - replication

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.

掩于岁月 2024-07-25 03:38:46

您可以尝试包含最常见问题的 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.

分开我的手 2024-07-25 03:38:46

我发现这个脚本将连接到您的 MySQL 数据库和 PostgreSQL 数据库并仅传输内容。 它对我来说就像一种魅力。

https://github.com/philipsoutham/py-mysql2pgsql

通过

$ pip install py-mysql2pgsql

在任意文件夹中运行

$ py-mysql2pgsql

安装,它将创建一个模板设置文件(mysql2pgsql.yml),您可以编辑并在其中输入数据库的详细信息。

我必须安装 argparse 才能正常工作。

$ pip install argparse

填写完数据库详细信息后,只需

$ py-mysql2pgsql

在与设置文件相同的文件夹中再次运行它,然后,您就完成了。 它没有在屏幕上打印任何内容,但之后我的数据库被完全复制。

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

$ pip install py-mysql2pgsql

Run

$ py-mysql2pgsql

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.

$ pip install argparse

When your database details are filled in, just run it again

$ py-mysql2pgsql

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.

请帮我爱他 2024-07-25 03:38:46

在转换之前,通过启动服务器将 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.

烛影斜 2024-07-25 03:38:46

除了移动数据库结构之外,你无法避免手动调整...

最可靠的传输数据方法(逐表传输,前提是结构相同):

mysql --default-character-set=utf8 -e "SELECT * FROM mytable" > mytable.txt

psql
\copy mytable from '/path/to/mytable.txt' WITH NULL AS 'NULL';

最近一直在尝试其他方法(比如带有大量选项的 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):

mysql --default-character-set=utf8 -e "SELECT * FROM mytable" > mytable.txt

psql
\copy mytable from '/path/to/mytable.txt' WITH NULL AS 'NULL';

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文