Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PostgreSQL 数据迁移 #42

Open
rottenpen opened this issue Sep 22, 2021 · 0 comments
Open

PostgreSQL 数据迁移 #42

rottenpen opened this issue Sep 22, 2021 · 0 comments

Comments

@rottenpen
Copy link
Owner

rottenpen commented Sep 22, 2021

原计划

当我们希望把云下的数据迁移到云上时,我们可以使用 pg_dump 导出希望迁移/备份的数据库数据,通过以下命令,pg_dump 将会在当前文件夹下生成对应备份的 sql 文件。

## pg_dump [数据库名] -f [文件名]

pg_dump kooltest -f backup.sql

数据导入

## psql -d [数据库名] -U [用户名] -f [文件名]

psql -d kooltest -U root -f backup.sql

真实操作

虽然 SQL 被我们导出了,但是对接公司的数据库并不成功,因为 pg 跟 mysql 在 sql 格式上还是有不少区别的。

迁移方案

方案很快捷,不过只支持 windows 不支持 mac / linux: https://www.dbsofts.com/articles/postgresql_to_mysql/

需要在 window 安装应用,先填写旧数据库的基本信息和数据库类型,后填写新数据库的基本信息和类型,点击 submit 即可完成迁移。

转化后的差异

JSON 格式的数据会被转化成 LONGTEXT。

而且每张表都会多一个前缀是 trial 的 column,不过因为它可以为 null,这个缺点基本可以忽略。

node 端修改

幸亏之前有使用 ORM 来连接 pg,抹平了不少底层的数据差异,只需要修改基本的 db 信息,以及数据库类型即可。

new Sequelize({
      database: 'db',
      username: 'dbuser',
      password: 'xxx',
      host: 'ip地址', // 从旧ip地址改到新ip地址
      port: 端口号,
      dialect: 'mysql', // 原本是postgresql
      pool: {
        max: 10,
        min: 0,
        idle: 10000,
        acquire: 30000,
      },
      timezone: '+08:00',
      define: {
        timestamps: true,
        createdAt: 'created',
        updatedAt: 'updated',
        charset: 'utf8',
      },
    });

DataTypes.JSON -> DataTypes.TEXT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant