【问题标题】:psql ERROR: relation already existspsql 错误:关系已经存在
【发布时间】:2013-02-21 10:44:24
【问题描述】:

我正在使用 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}"

我成功获取了数据文件,但是当运行**restore script时出现一些ERRORs*:

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: ""
...

并且本地的数据库不会更新。 我想知道怎么解决?添加一些论据?提前谢谢你。

【问题讨论】:

    标签: ruby-on-rails postgresql capistrano


    【解决方案1】:

    您可以将-c 或--clean 参数用于pg_dump。该参数将在运行命令创建它们之前删除现有的数据库对象。

    另一种方法是在恢复之前自行删除这些对象。 (可能使用drop schema 或drop database。)

    谨慎使用。

    【讨论】:

    • 这个选项在pg_dump 中有什么作用?我希望将选项传递给pg_restore。
    • @JohnEye:“在输出创建数据库对象的命令之前,先输出命令来清理(删除)数据库对象。(除非还指定了 --if-exists,否则恢复可能会生成一些无害的错误消息,如果目标数据库中不存在任何对象。)发出存档(非文本)输出文件时忽略此选项。对于存档格式,您可以在调用 pg_restore 时指定该选项。 Current docs
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    • 1970-01-01
    • 2022-06-26
    • 2013-02-04
    • 2018-10-05
    • 2018-06-29
    • 2017-08-28
    相关资源
    最近更新 更多