sql - 使用 pg_restore 创建或覆盖表

标签 sql database postgresql pg-dump pg-restore

我知道这是一个奇怪的请求,但出于一些我无法避免的怪异原因,我希望能够始终如一地将一些表从一个数据库同步到另一个数据库。我知道我可以自己在脚本中写出功能,但我认为 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;

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

最佳答案

似乎您想要在 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/

相关文章:

sql-server - SQL 事务日志传送无法将数据库恢复到备用状态

php - MySQL- 无法跳转到 MySQL 结果索引的第 0 行

macos - 如何从 mac 连接到 aws Redshift db

MySql 不同结果的一组不同结果

php - 选择带有运算符 php 名​​称的行

mysql - 如何使用 Mysql rand() 结果两次?

sql - c中sqlite的问题

asp.net - 将附加用户信息添加到 asp.net 中的 aspnetuser 表

postgresql - 在 Minikube 上挂载 Postgres 的主机目录 - 权限问题

python - 使用 Peewee 和 Marshmallow 序列化多对多关系