psql错误:关系已经存在。

7
我正在使用Postgres编写一个Rails项目,并且服务器上有一些数据。我想将数据从远程端转储到本地,因此我编写了脚本来执行此操作,但是出现了一些错误。
这是转储脚本:
run "PGPASSWORD='#{remote_settings['password']}' 
pg_dump -U #{remote_settings["username"]} #{"-h '#{remote_settings["host"]}'" 
if remote_settings["host"]} 
'#{remote_settings["database"]}' > #{remote_sql_file_path}"

有一些代码需要传输..

Transport codes

这是恢复脚本:

run_locally "PGPASSWORD='#{local_settings['password']}' psql -U 
#{local_settings["username"]} -d #{local_settings["database"]} 
-f #{local_sql_file_path}"

我已经成功获取数据文件,但在运行恢复脚本时出现了一些错误。
psql:tmp/production-ib5k_production-2013-02-21_18:42:09.sql:46: ERROR:  relation        "refinery_images" already exists
psql:tmp/production-ib5k_production-2013-02-21_18:42:09.sql:49: ERROR:  role "ib5k" does not exist
psql:tmp/production-ib5k_production-2013-02-21_18:42:09.sql:60: ERROR:  relation "refinery_images_id_seq" already exists
psql:tmp/production-ib5k_production-2013-02-21_18:42:09.sql:63: ERROR:  role "ib5k" does not exist
psql:tmp/production-ib5k_production-2013-02-21_18:42:09.sql:83: ERROR:  relation "refinery_page_part_translations" already exists
psql:tmp/production-ib5k_production-2013-02-21_18:42:09.sql:86: ERROR:  role "ib5k" does not exist
...
sql:tmp/production-ib5k_production-2013-02-21_18:42:09.sql:525: ERROR:  duplicate key  value violates unique constraint "refinery_images_pkey"
DETAIL:  Key (id)=(1) already exists.
CONTEXT:  COPY refinery_images, line 2: ""
psql:tmp/production-ib5k_production-2013-02-21_18:42:09.sql:547: ERROR:  duplicate key value violates unique constraint "refinery_page_part_translations_pkey"
DETAIL:  Key (id)=(1) already exists.
CONTEXT:  COPY refinery_page_part_translations, line 8: ""
psql:tmp/production-ib5k_production-2013-02-21_18:42:09.sql:569: ERROR:  duplicate key value violates unique constraint "refinery_page_parts_pkey"
DETAIL:  Key (id)=(1) already exists.
CONTEXT:  COPY refinery_page_parts, line 8: ""
...

本地数据库不会更新。 我想知道如何解决这个问题?需要添加一些参数吗?提前感谢您。
1个回答

22
你可以使用-c--clean参数来pg_dump。该参数将在运行创建命令之前删除现有的数据库对象。

另一种方法是在还原之前自己删除这些对象。(可能使用drop schemadrop database)。

请谨慎使用。


3
这个选项在 pg_dump 中是做什么的?我本来期望这个选项会传递给 pg_restore - JohnEye
@JohnEye:“在输出创建数据库对象的命令之前,输出清除(删除)数据库对象的命令。(除非还指定了--if-exists选项,否则如果目标数据库中不存在任何对象,则还原可能会生成一些无害的错误消息。)当发出存档(非文本)输出文件时,此选项将被忽略。对于存档格式,您可以在调用pg_restore时指定该选项。”[当前文档](https://www.postgresql.org/docs/current/app-pgdump.html) - Mike Sherrill 'Cat Recall'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接