[英]Using pg_restore to create or overwrite tables
I know this is a weird request, but for some hacky reasons I can't avoid, I'd like to be able to consistently sync a few tables from one database to another. 我知道这是一个奇怪的请求,但由于一些我无法避免的hacky原因,我希望能够始终如一地将一些表从一个数据库同步到另一个数据库。 I know I could write out the functionality myself in a script, but I figure
pg_dump
and pg_restore
will apply a lot of optimizations to the process that I'm not aware of myself. 我知道我可以在脚本中自己写出功能,但我认为
pg_dump
和pg_restore
会对我不了解的过程应用很多优化。
What I'm wondering is if there's a way to have pg_restore
overwrite the existing tables. 我想知道的是,是否有办法让
pg_restore
覆盖现有的表。 Basically, in pseudo-code something like: 基本上,在伪代码中,例如:
-- 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;
I'm also open to alternatives ways of doing this if this isn't so great. 如果不是这么好的话,我也愿意采取其他方式来做到这一点。
Seems like you want the -c
option specified in the pg_restore documentation 好像你想要pg_restore文档中指定的
-c
选项
-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.)
(除非使用--if-exists,否则如果目标数据库中不存在任何对象,则可能会生成一些无害的错误消息。)
which you can use with the -1
flag to do everything in one transaction 您可以使用
-1
标志在一个事务中执行所有操作
-1
-1
--single-transaction
--single事务
Execute the restore as a single transaction (that is, wrap the emitted commands in BEGIN/COMMIT).
将还原作为单个事务执行(即,将发出的命令包装在BEGIN / COMMIT中)。 This ensures that either all the commands complete successfully, or no changes are applied.
这可确保所有命令成功完成,或者不应用任何更改。 This option implies --exit-on-error.
此选项意味着--exit-on-error。
This is only example of possible solution: 这只是可能的解决方案的示例:
copy those tables from first db to csv. 将这些表从第一个db复制到csv。 and use extremely fast copy in transaction:
并在交易中使用极快的副本:
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.