我知道这是一个奇怪的请求,但出于一些我无法避免的怪异原因,我希望能够始终如一地将一些表从一个数据库同步到另一个数据库。我知道我可以自己在脚本中写出功能,但我认为 pg_dump
和 pg_restore
将对我自己不知道的过程应用大量优化.
我想知道是否有办法让 pg_restore
覆盖现有表。基本上,在伪代码中是这样的:
-- pseudo code
begin;
drop table to_restore;
drop table to_restore2;
drop table to_restore3;
-- etc
restore table to_restore;
restore table to_restore2;
restore table to_restore3;
-- etc
commit;
如果这不是很好,我也愿意接受其他方法。
最佳答案
似乎您想要在 pg_restore documentation 中指定的 -c
选项
-c
--clean
Clean (drop) database objects before recreating them. (Unless --if-exists is used, this might generate some harmless error messages, if any objects were not present in the destination database.)
您可以使用 -1
标志在一次交易中完成所有操作
-1
--single-transaction
Execute the restore as a single transaction (that is, wrap the emitted commands in BEGIN/COMMIT). This ensures that either all the commands complete successfully, or no changes are applied. This option implies --exit-on-error.
关于sql - 使用 pg_restore 创建或覆盖表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43546308/