如何轻松地将Django应用程序从mySQL转换为PostgreSQL?

时间:2011-02-11 01:12:01

标签: mysql django postgresql

有人这样做过吗?这是一个简单的过程吗?我们正在考虑切换事务,因为mysql最近似乎在“走出去”。

7 个答案:

答案 0 :(得分:11)

使用Django将MySQL数据库转换为Postgres数据库

首先在json fixtures中备份旧Mysql数据库的数据:

$ python manage.py dumpdata contenttypes --indent=4 --natural-foreign > contenttype.json
$ python manage.py dumpdata --exclude contenttypes --indent=4 --natural-foreign > everything_else.json

然后将您的设置.DATABASES切换为postgres设置。

在Postgresql中创建表:

$ python manage.py migrate

现在删除在迁移中自动生成的所有内容(django contenttypes,usergroups等):

$ python manage.py sqlflush | ./manage.py dbshell

现在你可以安全地导入所有内容,并保持你的pk相同!

$ python manage.py loaddata contenttype.json
$ python manage.py loaddata everything_else.json

使用Django == 1.8

进行测试

答案 1 :(得分:4)

我刚刚使用此工具迁移内部应用程序,它运行得非常好。 https://github.com/maxlapshin/mysql2postgres

答案 2 :(得分:2)

你可以使用Django序列化程序将数据从MySQL格式输出到JSON然后再输回到Postgres。互联网上有一些很好的艺术:

Migrating Django from MySQL to PostgreSQL the Easy Way

Move a Django site to PostgreSQL: check

答案 3 :(得分:1)

我从未亲自完成过,但似乎manage.py的dumpdataloaddata选项的组合可以很容易地解决您的问题。也就是说,除非您在ORM之外存在许多特定于数据库的事物,例如存储过程。

答案 4 :(得分:0)

我也没有这样做过。 我首先按照migration guide, there is a MySql section来处理您的所有数据。然后django只需在设置中将mysql切换到postgre。我认为应该没问题。

我在stackoverflow上发现了另一个问题,它应该有助于将mysql转换为postgre here

答案 5 :(得分:0)

  1. python manage.py dump.data>> data.json

  2. 在postrgesql中创建数据库和用户

  3. 在django设置中将刚创建的数据库设置为postrgesql中的默认数据库,或者使用param --database = your_postrgesql_database后续步骤
  4. 为create table运行syncdb。

    python syncdb [--database = your_postrgesql_database] --noinput

  5. 创建没有数据的转储,删除所有表并加载转储。或截断所有表(表django_content_type whith数据,这些数据可能与您的旧数据不相同 - 这是许多错误的方法)。在这一步,我们需要postgresql-db中的空表。

  6. 当postgresql-db中有空表时,只需加载数据:

    python manage.py loaddata data.json

  7. 好玩!

答案 6 :(得分:0)

我编写了一个将一个数据库复制到另一个数据库的Django管理命令: https://gist.github.com/mturilin/1ed9763ab4aa98516a7d

您需要在设置中添加两个数据库并使用此命令:

./manage.py copy_db from_database to_database app1 app2 app3 --delete --ignore-errors

这个命令很酷的是它以递归方式复制依赖对象。例如,如果模型有2个外键和2个多对多关系,它将首先复制其他对象,以确保您不会获得外键冲突错误。