且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从 .sql postgresql 备份恢复单个表?

更新时间:2022-11-03 08:14:07

如果需要单表还原等,不要做 SQL 备份 使用 pg_dump-Fc 选项 - 自定义"格式.这可以使用 pg_restore 恢复.选择性恢复是可能的,还有各种其他方便的功能.pg_restore 可以在以后需要时将自定义格式的转储转换为 SQL 转储.

Don't do SQL backups if you need single table restore, etc. Use pg_dump's -Fc option - the "custom" format. This can be restored using pg_restore. Selective restore is possible, as are all sorts of other handy features. pg_restore can convert a custom-format dump into an SQL dump later if you need it.

如果您坚持使用现有的转储,您唯一的选择是:

If you're stuck with an existing dump, your only options are:

  • 使用文本编辑器将目标表数据提取到一个单独的文件中,然后将其恢复;或

  • Use a text editor to extract the target table data to a separate file and just restore that; or

将转储恢复到一次性数据库,然后使用 pg_dump 进行选择性转储,仅包括该表.由于它是一次性的,你可以在一些卸载的快速但不安全的机器上使用一个单独的 Pg 实例,在那里你打开所有让它快速但如果你喜欢吃我的数据"选项,比如 fsync=off.你应该永远在生产中设置它.

Restore the dump to a throwaway database then use pg_dump to take a selective dump including just that table. Since it's throwaway, you can use a separate Pg instance on some unloaded fast-but-unsafe machine where you turn on all the "make it fast but eat my data if you like" options like fsync=off. You should NEVER set that in production.