← 返回 Skills 市场
lawrencehe

MySQL to PostgreSQL Migration

作者 LawrenceHe · GitHub ↗ · v1.0.1 · MIT-0
cross-platform ✓ 安全检测通过
183
总下载
0
收藏
0
当前安装
2
版本数
在 OpenClaw 中安装
/install mysql2postgres
功能描述
Use this skill when migrating a project's database layer from MySQL to PostgreSQL. Covers SQL dialect conversion (MyBatis XML / raw SQL), ORM configuration (...
使用说明 (SKILL.md)

MySQL → PostgreSQL 迁移 Skill

概览

本 Skill 总结了将 Java(Spring Boot + MyBatis/MyBatis-Plus)项目从 MySQL 迁移到 PostgreSQL 的完整经验,涵盖五类核心改动,以及各类运行时报错的根因与修复方法。


一、JDBC 连接配置

application.yaml 改动

# MySQL(原)
spring.datasource.url: jdbc:mysql://host/dbname
validation-query: SELECT 1 FROM DUAL

# PostgreSQL(改后)
spring.datasource.url: jdbc:postgresql://host/dbname?currentSchema=your_schema
validation-query: SELECT 1

关键点:

  • currentSchema=your_schema 指定默认 schema,避免每条 SQL 都加 schema 前缀
  • SELECT 1 FROM DUAL 是 MySQL/Oracle 方言,PG 直接用 SELECT 1

二、SQL 方言转换速查表(MyBatis XML)

类别 MySQL 写法 PostgreSQL 写法 备注
空值处理 IFNULL(a, b) COALESCE(a, b) 标准 SQL,推荐
日期格式化 DATE_FORMAT(d, fmt) TO_CHAR(d, fmt) 格式符略有差异
日期加减 DATE_ADD(d, INTERVAL 1 DAY) d + INTERVAL '1 day' PG 用运算符
当前日期 CURDATE() CURRENT_DATE
当前时间 NOW() CURRENT_TIMESTAMP PG 也支持 NOW(),可保留
类型转换-时间 CAST(x AS DATETIME) CAST(x AS TIMESTAMP) PG 无 DATETIME 类型
类型转换-日期 DATE(x) CAST(x AS DATE)
Upsert-忽略 INSERT IGNORE INTO INSERT INTO ... ON CONFLICT DO NOTHING
Upsert-更新 INSERT ... ON DUPLICATE KEY UPDATE INSERT INTO ... ON CONFLICT (col) DO UPDATE SET ...
多表更新 UPDATE t1 INNER JOIN t2 ON ... SET t1.col=... UPDATE t1 SET col=... FROM t2 WHERE ... 见下方详解

UPDATE JOIN → UPDATE FROM 详解

-- MySQL
UPDATE orders o
INNER JOIN users u ON o.user_id = u.id
SET o.name = u.name
WHERE o.status = 1;

-- PostgreSQL
UPDATE orders o
SET name = u.name
FROM users u
WHERE o.user_id = u.id
  AND o.status = 1;

规律: PG 把 JOIN 的表放到 FROM 子句,WHERE 里写关联条件。


三、数据类型陷阱

BIT(1) 类型与逻辑删除

MySQL 的 bit(1) 迁移到 PG 后仍是 BIT / BIT(1) 类型,但 PG 严格禁止 BIT 与整数直接比较。

报错:

operator does not exist: bit = integer

两处修复:

① MyBatis-Plus 逻辑删除配置(application.yaml):

# 原(MySQL 兼容)
mybatis-plus.global-config.db-config.logic-delete-value: 1
mybatis-plus.global-config.db-config.logic-not-delete-value: 0

# 改后(PG BIT literal)
mybatis-plus.global-config.db-config.logic-delete-value: "B'1'"
mybatis-plus.global-config.db-config.logic-not-delete-value: "B'0'"

② XML 手写 SQL 中的 deleted 字段:

-- MySQL / 错误写法
WHERE deleted = 0
WHERE deleted = 1

-- PostgreSQL 正确写法
WHERE deleted = B'0'
WHERE deleted = B'1'

⚠️ 注意: MyBatis-Plus 自动生成的 deleted=0 和 XML 里手写的都要改,两处独立,不要遗漏。建议用全局搜索:deleted = 0 / deleted = 1,统计 XML 文件中所有出现位置后批量替换。


四、自增主键 / Sequence

MySQL 的 AUTO_INCREMENT 在 PG 中对应 SEQUENCE

MyBatis-Plus @KeySequence

// Java DO 类上标注
@KeySequence("demo_entity_seq")
public class DemoEntityDO { ... }

运行时 MyBatis-Plus 会执行:

SELECT nextval('demo_entity_seq')

常见报错:

ERROR: relation "demo_entity_seq" does not exist

修复:在对应 schema 下创建 Sequence:

-- 对齐现有数据的最大 ID,避免主键冲突
CREATE SEQUENCE your_schema.demo_entity_seq
  START WITH 10001      -- 从当前最大 id + 1 开始
  INCREMENT BY 1
  NO MINVALUE
  NO MAXVALUE
  CACHE 1;

最佳实践: 迁移数据后,用 SELECT MAX(id) FROM table 确认最大 ID,再设置 Sequence 的起始值。


五、GROUP BY 严格模式

PG 严格遵循 SQL 标准:SELECT 中所有非聚合列必须出现在 GROUP BY 中。MySQL 可通过 sql_mode 去掉 ONLY_FULL_GROUP_BY 来绕过,但 PG 没有对应的宽容模式

报错:

ERROR: column "d.flag_col" must appear in the GROUP BY clause
or be used in an aggregate function

修复策略:

情况 解决方案
列的值在组内唯一(由业务保证) 加入 GROUP BY
列是枚举/状态值,取任意值均可 MAX(col)MIN(col)
列是标志位,有任意一行满足即为真 BOOL_OR(col)MAX(col)
-- 原 MySQL 写法(SELECT 了非聚合列)
SELECT o.id, d.flag_col, d.status_col,
       MIN(d.amount_col) as amount_col
FROM demo_order o
LEFT JOIN demo_order_detail d ON o.id = d.order_id
GROUP BY o.id;

-- PG 修复写法
SELECT o.id,
       MAX(d.flag_col) as flag_col,      -- 取最大值
       MAX(d.status_col) as status_col,  -- 有任意在售即为在售
       MIN(d.amount_col) as amount_col
FROM demo_order o
LEFT JOIN demo_order_detail d ON o.id = d.order_id
GROUP BY o.id;

六、包名 / Namespace 重构(MyBatis)

迁移时建议将 MyBatis 的包路径和 namespace 从 dal.mysql 改为 dal.pg,保持语义清晰。

涉及的修改点:

  1. src/main/java 下的目录:dal/mysql/dal/pg/
  2. XML 文件中的 namespacedal.mysql.XxxMapperdal.pg.XxxMapper
  3. Java 文件中的 import 语句
  4. application.yaml 中的日志监控包名配置

七、迁移检查清单

[ ] JDBC URL 改为 PG 格式,指定 currentSchema
[ ] validation-query 去掉 FROM DUAL
[ ] IFNULL → COALESCE(全局搜索替换)
[ ] DATE_FORMAT → TO_CHAR
[ ] DATE_ADD(...INTERVAL) → date + INTERVAL '...'
[ ] CURDATE() → CURRENT_DATE
[ ] CAST(x AS DATETIME) → CAST(x AS TIMESTAMP)
[ ] DATE(x) → CAST(x AS DATE)
[ ] INSERT IGNORE → INSERT ... ON CONFLICT DO NOTHING
[ ] UPDATE ... JOIN → UPDATE ... FROM ... WHERE
[ ] deleted = 0/1 → deleted = B'0'/B'1'(XML 手写 SQL)
[ ] MyBatis-Plus logic-delete-value 改为 B'1' / B'0'
[ ] 为每个 @KeySequence 在 PG 中创建对应 SEQUENCE
[ ] 确认所有 SEQUENCE 起始值 > 表中当前最大 ID
[ ] 检查 GROUP BY:SELECT 中非聚合列必须在 GROUP BY 或用聚合函数包裹
[ ] 包名 / namespace 从 dal.mysql 改为 dal.pg(可选)

八、常见运行时报错速查

报错关键词 根因 修复
operator does not exist: bit = integer BIT 列与整数比较 改为 B'0' / B'1'
relation "xxx_seq" does not exist Sequence 未创建 在对应 schema 建 Sequence
must appear in the GROUP BY clause SELECT 非聚合列未在 GROUP BY 加入 GROUP BY 或用聚合函数
SELECT 1 FROM DUAL 报错 DUAL 是 Oracle/MySQL 方言 改为 SELECT 1
function ifnull does not exist IFNULL 是 MySQL 方言 改为 COALESCE
function date_format does not exist DATE_FORMAT 是 MySQL 方言 改为 TO_CHAR
安全使用建议
This is an instructional, read-only guide that appears coherent and relevant to MySQL→PostgreSQL migrations for Java/Spring/MyBatis projects. Before using it in an automated agent workflow: 1) understand that actual migration operations (creating sequences, running SELECT MAX, applying SQL changes) require database credentials—do not supply production admin credentials to an automated agent; prefer a restricted/staging account and backups. 2) Perform full backups and test migrations in staging first. 3) Review any proposed global search-and-replace operations manually or via code-review (create PRs) to avoid accidental mass edits. 4) Pay special attention to BIT vs integer handling, sequence start values, and GROUP BY changes the guide highlights. 5) If you allow the agent to act autonomously, limit its access scope and require manual approval for destructive actions. Overall the skill is coherent, but human oversight is recommended when applying its recommendations to live data.
功能分析
Type: OpenClaw Skill Name: mysql2postgres Version: 1.0.1 The skill bundle is a legitimate technical guide for migrating Java applications from MySQL to PostgreSQL. It contains standard SQL dialect conversion rules, configuration examples for MyBatis/Spring Boot, and troubleshooting steps for common migration errors (e.g., BIT type handling and GROUP BY strictness) without any signs of malicious intent or data exfiltration.
能力评估
Purpose & Capability
The name and description match the SKILL.md content: a focused migration checklist and example fixes for JDBC, SQL dialects, types, sequences, GROUP BY, and MyBatis specifics. It does not request unrelated binaries, env vars, or access.
Instruction Scope
The instructions stay on-topic (code edits, SQL conversion patterns, config examples, run-time error troubleshooting). They recommend global search/replace and creating sequences or running SELECT MAX(id), which are expected migration activities and do not ask the agent to read unrelated system files or exfiltrate data.
Install Mechanism
No install spec or code is provided (instruction-only), so nothing is downloaded or written to disk. This minimizes execution risk.
Credentials
The skill declares no environment variables or credentials (appropriate for an instruction-only guide). In practice migrating databases requires DB credentials and access; the skill does not declare them but may lead the agent or user to supply credentials separately—this is expected but worth noting.
Persistence & Privilege
always is false and there is no installation that modifies agent/system configuration. The skill does not request persistent privileges or modify other skills.
如何使用
  1. 确保已安装 OpenClaw(本地或 Docker 部署)
  2. 在对话框中输入安装命令:/install mysql2postgres
  3. 安装完成后,直接呼叫该 Skill 的名称或使用 /mysql2postgres 触发
  4. 根据 Skill 的参数说明提供必要输入,即可获得结构化输出
版本历史
v1.0.1
- Updated documentation for improved generalization: examples and schema/config names now use placeholders (e.g., your_schema, demo_entity_seq) instead of project-specific names. - No changes to functionality, code, or configuration; this release is documentation-only.
v1.0.0
mysql2postgres 1.0.0 - Initial release of migration skill for moving Java (Spring Boot + MyBatis/MyBatis-Plus) projects from MySQL to PostgreSQL. - Covers JDBC connection setup, SQL dialect conversion (including MyBatis XML patterns), ORM configuration changes, and datatype compatibility pitfalls. - Provides step-by-step guidance for common migration obstacles, such as bit type handling, sequence management (@KeySequence), and stricter GROUP BY requirements in PostgreSQL. - Includes runtime error diagnosis and quick fixes for frequent MySQL-to-PG migration issues. - Offers a comprehensive checklist and best practices for a smooth migration process. - Targets Java / Spring Boot projects using MyBatis or MyBatis-Plus.
元数据
Slug mysql2postgres
版本 1.0.1
许可证 MIT-0
累计安装 0
当前安装数 0
历史版本数 2
常见问题

MySQL to PostgreSQL Migration 是什么?

Use this skill when migrating a project's database layer from MySQL to PostgreSQL. Covers SQL dialect conversion (MyBatis XML / raw SQL), ORM configuration (... 它是一个面向 Claude Code / OpenClaw 的 AI Agent Skill 插件,目前累计下载 183 次。

如何安装 MySQL to PostgreSQL Migration?

在 OpenClaw 或 Claude Code 对话框中运行命令「/install mysql2postgres」即可一键安装,无需额外配置。

MySQL to PostgreSQL Migration 是免费的吗?

是的,MySQL to PostgreSQL Migration 完全免费,采用 MIT-0 许可证,可自由下载、安装和使用。

MySQL to PostgreSQL Migration 支持哪些平台?

MySQL to PostgreSQL Migration 跨平台运行,可在任意部署了 OpenClaw / Claude Code 的环境中使用(cross-platform)。

谁开发了 MySQL to PostgreSQL Migration?

由 LawrenceHe(@lawrencehe)开发并维护,当前版本 v1.0.1。

💬 留言讨论