gpt4 book ai didi

ruby-on-rails - psql 错误 : relation already exists

转载 作者:行者123 更新时间:2023-11-29 11:26:50 25 4
gpt4 key购买 nike

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

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

最佳答案

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

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

谨慎使用。

关于ruby-on-rails - psql 错误 : relation already exists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15000227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com