5

我知道这是一个奇怪的请求,但出于一些我无法避免的奇怪原因,我希望能够始终将几个表从一个数据库同步到另一个数据库。我知道我可以自己在脚本中写出功能,但我认为并将对我自己不知道的过程应用很多优化pg_dumppg_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;

如果这不是很好,我也愿意接受替代方法。

4

2 回答 2

6

似乎您想要pg_restore 文档-c中指定的选项

-C

- 干净的

在重新创建它们之前清理(删除)数据库对象。(除非使用 --if-exists,否则如果目标数据库中不存在任何对象,这可能会生成一些无害的错误消息。)

您可以将其与-1标志一起使用以在一次事务中完成所有操作

-1

--单笔交易

将还原作为单个事务执行(即将发出的命令包装在 BEGIN/COMMIT 中)。这可确保所有命令都成功完成,或者不应用任何更改。此选项意味着 --exit-on-error。

于 2017-04-21T15:23:28.343 回答
2

这只是可能解决方案的示例:

将这些表从第一个 db 复制到 csv。并在事务中使用极快的复制:

begin;
truncate table to_restore;
truncate table to_restore2;
truncate table to_restore3;
  set commit_delay to 100000;
  set synchronous_commit to off;
copy to_restore from 'to_restore.csv';
copy to_restore2 from 'to_restore2.csv';
copy to_restore3 from 'to_restore3.csv';
commit;
于 2017-04-21T15:22:41.077 回答