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

发布于 2024-10-17 06:58:19 字数 59 浏览 1 评论 0 原文

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

Has anyone done this? Is it an easy process? We're thinking of switching over for transactions and because mysql seems to be "crapping out" lately.

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

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

发布评论

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

评论(8

寄风 2024-10-24 06:58:19

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

首先在 json 固定装置中备份旧 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

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

在 Postgresql 中创建表:

$ python manage.py migrate

现在删除迁移中自动生成的所有内容(django 内容类型、用户组等):

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

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

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

使用 Django==1.8 进行测试

Converting MySQL database to Postgres database with Django

First backup your data of the old Mysql database in json fixtures:

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

Then switch your settings.DATABASES to postgres settings.

Create the tables in Postgresql:

$ python manage.py migrate

Now delete all the content that is automatically made in the migrate (django contenttypes, usergroups etc):

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

And now you can safely import everything, and keep your pk's the same!

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

Tested with Django==1.8

下壹個目標 2024-10-24 06:58:19

我刚刚使用这个工具来迁移内部应用程序,效果非常好。 https://github.com/maxlapshin/mysql2postgres

I just used this tool to migrate an internal app and it worked wonderfully. https://github.com/maxlapshin/mysql2postgres

猫卆 2024-10-24 06:58:19

您可以使用 Django 序列化器将数据从 MySQL 格式输出到 JSON,然后返回到 Postgres。互联网上有一些关于此的好文章:

轻松地将 Django 从 MySQL 迁移到 PostgreSQL

将 Django 站点移至 PostgreSQL:检查

You can do that using Django serializers to output the data from MySQL's format into JSON and then back into Postgres. There are some good artices on internet about that:

Migrating Django from MySQL to PostgreSQL the Easy Way

Move a Django site to PostgreSQL: check

泛滥成性 2024-10-24 06:58:19

我从来没有亲自做过,但它似乎是 dumpdata

I've never done it personally, but it seems like a combination of the dumpdata and loaddata options of manage.py would be able to solve your problem quite easily. That is, unless you have a lot of database-specific things living outside the ORM such as stored procedures.

烈酒灼喉 2024-10-24 06:58:19

我也没有做过。
我首先遵循这个 迁移指南,有一个 MySql 部分 应该照顾所有您的数据。然后django只需在设置中将mysql切换为postgre即可。我想应该没问题。

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

I've not done it either.
I'd first follow this migration guide, there is a MySql section which should take care of all your data. Then django just switch the mysql to postgre in the settings. I think that should be ok.

I found another question on stackoverflow which should help with the converting mysql to postgre here.

国粹 2024-10-24 06:58:19
  1. python manage.py dump.data>> data.json

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

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

    pythonsyncdb [--database=your_postrgesql_database] --noinput

  5. 创建没有数据的转储,删除所有表并加载转储。或者截断所有表(表 django_content_type 的数据可能不等于您的旧数据 - 这会导致许多错误)。在这一步中,我们需要 postgresql-db 中的空表。

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

    python manage.py loaddata data.json

并且很有趣!

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

  2. Create database and user in postrgesql

  3. Set your just created database in postrgesql as default database in django settings or use param --database=your_postrgesql_database next steps
  4. Run syncdb for create tables.

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

  5. Create dump without data, drop all tables and load dump. Or truncate all tables (table django_content_type whith data which can be not equals your old data - it is way to many errors). At this step we need empty tables in postgresql-db.

  6. When you have empty tables in postgresql-db just load your data:

    python manage.py loaddata data.json

And be fun!

回梦 2024-10-24 06:58:19

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

您需要在设置并使用此命令:

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

此命令的优点在于它递归地复制依赖对象。例如,如果模型有 2 个外键和两个多对多关系,它将首先复制其他对象以确保您不会出现外键冲突错误。

I wrote a Django management command that copies one database to another:
https://gist.github.com/mturilin/1ed9763ab4aa98516a7d

You need to add both database in the settings and use this command:

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

What cool about this command is that it recursively copy dependent objects. For example, if the model have 2 foreign keys and two Many-to-Many relationships, it will copy the other objects first to ensure you won't get foreign key violation error.

硬不硬你别怂 2024-10-24 06:58:19

我刚刚完成了这个练习以及 michael- 提供的答案van-de-waeter 这里 非常完美。

但是,我想添加以下所需的步骤:

  1. 我必须首先删除此线程之后的ContentType:

python manage.py shell

from django.contrib.contenttypes.models import ContentType

ContentType.objects.all().delete()
  1. 我还必须在数据 json 中附加一个方括号:

    回显']'>> everything_else.json

I just went through this exercise and the answer provided by michael-van-de-waeter here is perfect.

However, I would like to add the following needed steps:

  1. I had to first delete ContentType following this thread:

python manage.py shell

from django.contrib.contenttypes.models import ContentType

ContentType.objects.all().delete()
  1. I had also to append a square bracket to the data json:

    echo ']' >> everything_else.json

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