pg_恢复干净表psql
我正在尝试将我转储的表还原到其当前位置。该表是使用以下方法转储的:pg_恢复干净表psql,sql,postgresql,psql,pg-dump,pg-restore,Sql,Postgresql,Psql,Pg Dump,Pg Restore,我正在尝试将我转储的表还原到其当前位置。该表是使用以下方法转储的: pg_dump -t table db > table.sql -U username 正在尝试恢复,我正在使用: pg_restore -c --dbname=db --table=table table.sql 我使用-c,因为该表当前存在并且有数据,但是它返回: 我也试过: -bash-4.2$ psql -U username -d db -1 -f table.sql 但是由于数据已经存在,并且这个p
pg_dump -t table db > table.sql -U username
正在尝试恢复,我正在使用:
pg_restore -c --dbname=db --table=table table.sql
我使用-c,因为该表当前存在并且有数据,但是它返回:
我也试过:
-bash-4.2$ psql -U username -d db -1 -f table.sql
但是由于数据已经存在,并且这个psql命令(我相信)没有--clean
选项,因此它返回:
psql:table.sql:32: ERROR: relation "table" already exists
有没有办法正确使用pg_restore或使用带有--clean选项的psql?朋友,这对你来说是个坏消息。您无法从计划文本转储中还原
pgu
。在文档中:
“pg_restore是一个实用程序,用于从pg_dump以一种非纯文本格式创建的存档中恢复PostgreSQL数据库。”
如果您可以重新执行pg_dump
,则可以使用-Fc
标志来执行,这将生成一个工件,您可以使用pg_restore
进行还原。如果您一直使用纯文本格式的table.sql
,那么您确实有一些选择(在下面的两个示例中,我将您的目标表称为my_fancy_table
):
选项1:
删除表格:删除表格my_fancy_表格代码>
现在运行psql命令:psql-f table.sql
如果您具有引用完整性,而其他表需要有行,则可能无法删除该表
选项2:
从临时表中向上插入
您可以编辑sql文件(因为它是纯文本)并将表名更改为my\u fancy\u table\u temp
(或任何尚不存在的表名)。现在您将有两个表,一个是来自sql文件的临时表,另一个是一直存在的真实表。然后,您可以编写一些upsert SQL,用临时表中的行插入或更新实际表,如下所示:
insert into my_facy_table (column1, column2)
select column1, column2 from my_fancy_table_temp
on conflict (my_fancy_unique_index)
update my_fancy_table
set column1 = excluded.column1,
column2 = excluded.column2
既然您本来打算使用-c,那么有什么原因不能在表消失后手动删除表并执行psql-f table.sql
?这会影响其他连接的表吗?谢谢你的全面回答!看来省略-FC
是我的失败。让我尝试一下选项2。
insert into my_facy_table (column1, column2)
select column1, column2 from my_fancy_table_temp
on conflict (my_fancy_unique_index)
update my_fancy_table
set column1 = excluded.column1,
column2 = excluded.column2