postgresql 如何使用 Postgres 备份/恢复 Rails 数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18723675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:05:55  来源:igfitidea点击:

How to backup/restore Rails db with Postgres?

ruby-on-railspostgresql

提问by croceldon

I do the following on my server:

我在我的服务器上执行以下操作:

 pg_dump -O -c register_production > register.sql

Then, after copying register.sql to my local environment, I try:

然后,将 register.sql 复制到我的本地环境后,我尝试:

 psql register_development < register.sql

This appears to work, but when I try to launch the Rails site locally, I get this:

这似乎有效,但是当我尝试在本地启动 Rails 站点时,我得到以下信息:

 PG::UndefinedTable: ERROR:  relation "list_items" does not exist at character 28

How can I restore everything (including relations) from the server db to my local dev db?

如何将服务器数据库中的所有内容(包括关系)恢复到本地开发数据库?

回答by Veraticus

I use this command to save my database:

我使用这个命令来保存我的数据库:

pg_dump -F c -v -U postgres -h localhost <database_name> -f /tmp/<filename>.psql

And this to restore it:

这是恢复它:

pg_restore -c -C -F c -v -U postgres /tmp/<filename>.psql

This dumps the database in Postgres' custom format (-F c) which is compressed by default and allows for reordering of its contents. -C -cwill drop the database if it exists already and then recreate it, helpful in your case. And -vspecifies verbose so you can see exactly what's happening when this goes on.

这将以 Postgres 的自定义格式 ( -F c)转储数据库,默认情况下该格式是压缩的,并允许对其内容重新排序。-C -c如果数据库已经存在,则将删除该数据库,然后重新创建它,这对您的情况很有帮助。并-v指定详细信息,以便您可以准确了解发生这种情况时发生的情况。

回答by Bob Gilmore

Does the register_development database existbefore you run the psqlcommand? Because that form will not create it for you.

运行命令前register_development 数据库是否存在psql?因为那个表格不会为你创建它。

See http://www.postgresql.org/docs/8.1/static/backup.html#BACKUP-DUMP-RESTOREfor more information.

有关更多信息,请参阅http://www.postgresql.org/docs/8.1/static/backup.html#BACKUP-DUMP-RESTORE