pg_restore 仅某些表

Jac*_*lyn 4 postgresql restore postgresql-9.3

我正在寻找一种用于pg_restore从转储文件恢复新创建的数据库的方法,但只能从该文件中恢复某些表。(数据库中有很多额外的表恢复速度很慢,我不关心。)

我首先尝试pg_restore使用-t/--table标志,但它没有启用这些表所需的扩展。没什么大不了的; 我可以psql -c "CREATE EXTENSION ..."pg_restore命令之前手动运行。我更大的问题是该-t命令似乎跳过了与表相关的其他内容,例如约束和索引。

索引很烦人,但我可以运行pg_restore --list和使用awk/ grep/etc。获得索引列表,并将它们传递到pg_restore-I/--index标志,所以这个过程将是(我认为):

  1. pg_restore -t table1 -t table2 --schema-only ...
  2. pg_restore -I index1 -I index2 ...
  3. pg_restore -t table1 -t table2 --data-only ...

但是(a)这仍然不会为我设置约束,并且(b)感觉就像我正在使用这个迂回的解决方案陷入困境,因为我认为这很简单。

我知道你可以告诉pg_dump只转储某些表,但我希望避免这种情况,因为转储我的数据库需要很长时间。

Chr*_*vey 8

如果您使用“自定义”格式,则可以使用-l(ell) 和-L选项来执行此操作。

  1. 使用“自定义”格式创建转储
  2. pg_restore -l /path/to/dump/file > table_of_contents.txt
  3. 编辑 table_of_contents.txt 并删除对您不再需要的表的引用。(确保你得到了所有这些。你可以用分号注释掉一行。)
  4. 恢复你的数据库 pg_restore ... -L table_of_contents.txt /path/to/dump/file

利润!