简体   繁体   English

使用pg_restore创建或覆盖表

[英]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_dumppg_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.

相关问题 PostgreSQL,使用pg_restore更新现有行 - PostgreSQL, update existing rows with pg_restore 如果 pg_restore 正在进行,我们会在应用程序中遇到任何运行时问题吗? - Will we face any runtime issue in application if pg_restore is in progress? 在Postgres中,在截断或删除行之后,在pg_dump之前执行reindex是否可以优化pg_restore? - In Postgres, after truncating or deleting rows, does executing reindex before pg_dump optimize pg_restore? 调用postgres命令,例如pg_restore或pg_dump形式的sql文件 - Calling postgres command like pg_restore or pg_dump form sql file pg_restore: [目录归档程序] 无法打开输入文件。 尝试恢复数据库时出错 - pg_restore: [directory archiver] could not open input file. Error while trying to restore DB 是否可以指示pg_dump创建“IF NOT EXISTS”表? - Can pg_dump be instructed to create tables with “IF NOT EXISTS”? 如何使用备份和还原创建数据库副本 - How to create copy of database using backup and restore 使用DoCmd.Transferdatabase命令时更新表,而不是覆盖它们 - Update tables, not overwrite them when using the DoCmd.Transferdatabase command 如何使用Powershell Restore-SqlDatabase cmdlet可靠地覆盖现有数据库 - How to reliably overwrite existing database using Powershell Restore-SqlDatabase cmdlet Hive 如何在使用插入覆盖目录时创建表? - Hive How to create a table when using insert overwrite directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM