简体   繁体   English

Django 的“dumpdata”还是 Postgres 的“pg_dump”?

[英]Django's "dumpdata" or Postgres' "pg_dump"?

I'm unsure as to whether this question should be posted in the Database Administrators ' section or here, so please advise if I got it wrong.我不确定这个问题是否应该发布在“数据库管理员”部分或此处,所以如果我弄错了,请告知。

I have a Django-based website which doesn't change much.我有一个基于 Django 的网站,没有太大变化。 I use python manage.py dumpdata --all --indent=2 > backup.json and reload the data with loaddata if I need to redeploy or the db gets corrupted.如果需要重新部署或数据库损坏,我使用python manage.py dumpdata --all --indent=2 > backup.json并使用loaddata重新加载数据。 (I'm aware about integrity errors that have occurred when not excluding auth and content_types ) (我知道不排除authcontent_types时发生的完整性错误)

Since I'm using PostgreSQL on the backend, is it "best practise" or "wiser" for me to use pg_dump instead, and then pg_restore if something goes wrong or if I need to redeploy?由于我在后端使用 PostgreSQL,对我来说,使用pg_dump是“最佳实践”还是“更明智”,如果出现问题或者我需要重新部署,然后使用pg_restore

So dumpdata dumps all data associated with the selected apps (and/or models), and pg_dump performs a full dump of the db.因此, dumpdata转储与所选应用程序(和/或模型)相关的所有数据,而pg_dump会执行数据库的完整转储。 Is this the same thing or is there a fundamental difference that I've missed (mind you I have 0 experience with DBA)?这是同一件事还是我错过了根本的区别(请注意,我对 DBA 的经验为零)?

Which option do I go for and why?我应该选择哪个选项,为什么?

It is both best practice and wiser for you to use pg_dump instead of dumpdata .使用pg_dump而不是dumpdata既是最佳实践,也是更明智的做法。

There are many reasons for this.这件事情是由很多原因导致的。

  • pg_dump is faster and the output is more compact (particularly with the -Fc option) than with dumpdata. pg_dump比 dumpdata 更快,输出更紧凑(特别是使用 -Fc 选项)。

  • Importing the data back into the db with pg_restore will also be faster than django's loaddata .使用pg_restore将数据导入数据库也将比 django 的loaddata更快。

  • pg_restore is available on any postgresql installation but django and it's dependencies you will have to install. pg_restore 在任何 postgresql 安装中都可用,但 django 及其依赖项必须安装。

  • Last but not least the integrity errors that you spoke of will not happen with pg_dump/pg_restore.最后但并非最不重要的一点是,您提到的完整性错误不会发生在 pg_dump/pg_restore 中。

Generally pg_dump is used to dump the entire database however the -t option allows you to dump one or few tables at a time通常 pg_dump 用于转储整个数据库,但是 -t 选项允许您一次转储一个或几个表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM