pg_restore 干净 table psql

pg_restore clean table psql

我正在尝试恢复我转储到其当前位置的 table。 table 被转储使用:

pg_dump -t table db > table.sql -U username

尝试恢复,我正在使用:

pg_restore -c --dbname=db --table=table table.sql

我使用 -c 因为 table 当前存在并且有数据,但是它 returns:

pg_restore: [archiver] input file appears to be a text format dump. Please use psql.

我也试过:

-bash-4.2$ psql -U username -d db -1 -f table.sql

但是由于数据已经存在并且没有 --clean 这个 psql 命令的选项(我相信),它 returns:

psql:table.sql:32: ERROR:  relation "table" already exists

有没有办法正确使用 pg_restore 或使用带有 --clean 选项的 psql?

对你来说是个坏消息,朋友。您不能从计划文本转储中 pg_restore。它在文档中:

“pg_restore 是一个实用程序,用于从 pg_dump 创建的非纯文本格式之一的存档中恢复 PostgreSQL 数据库。” https://www.postgresql.org/docs/9.6/static/app-pgrestore.html

如果您可以重新执行 pg_dump,您可以使用 -Fc 标志执行此操作,这将生成一个您可以使用 pg_restore 恢复的工件。如果您坚持使用纯文本格式的 table.sql,您确实有几个选择(在下面的两个示例中我将您的目标称为 table my_fancy_table):

选项 1:

删除 table:drop table my_fancy_table;

现在 运行 你的 psql 命令:psql <options> -f table.sql

如果您在其他 table 需要行的情况下具有参照完整性,您可能无法删除 table.

选项 2: 从临时 table

更新

您可以编辑 sql 文件(因为它是纯文本格式)并将 table 名称更改为 my_fancy_table_temp(或任何尚未更改的 table 名称存在)。现在您将拥有两个 table,一个来自 sql 文件的临时文件和一个一直存在的真实文件。然后你可以写一些 upsert SQL 来插入或更新真正的 table 和来自临时 table 的行,像这样:

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