PostgreSQL:仅在不存在时恢复数据

问题描述 投票:0回答:1

我将表的数据转储到 .tar 文件中,并将其恢复到不同数据库上的同一个表中。

C:\Program Files\PostgreSQL\12\bin\pg_dump.exe --file "C:\\SQL_ST~1\\pg_dump.tar" --host "localhost" --port "5432" --username "postgres" --no-password --verbose --format=c --blobs --data-only --table "runtime.batches" "sdm_kunde"
C:\Program Files\PostgreSQL\12\bin\pg_restore.exe --host "localhost" --port "5432" --username "postgres" --no-password --dbname "sdm_wir" --data-only --verbose --schema "runtime" --table "batches" "C:\\SQL_ST~1\\pg_dump.tar"

问题在于数据库内部存储了几乎相同的信息,由于其中一列的独特限制,这与恢复发生冲突。有没有办法只恢复目标数据库中不存在的数据?

postgresql cmd pg-dump postgresql-12 pg-restore
1个回答
0
投票

如果您的表上有主键或唯一约束来识别重复项,则可以

pg_dump --format=custom --inserts --on-conflict-do-nothing --data-only --blobs --table=mytab mydb

恢复该转储将默默地跳过目标表中已有的行。请注意,大对象将被无条件替换 - 但无论如何,您都不应该使用大对象。

© www.soinside.com 2019 - 2024. All rights reserved.