使用 pg_restore 恢复表不包括主键或索引

mk2*_*000 5 postgresql pg-dump primary-key database-indexes pg-restore

所以我使用 pg_dump 备份了一个表:

pg_dump -U bob -F c -d commerce -t orders > orders.dump
Run Code Online (Sandbox Code Playgroud)

该表有几个列出的索引,例如主键

但是,当我使用 pg_restore 将此表恢复到另一个系统上的开发数据库时:

pg_restore -U bob -d commerce -t orders > orders.dump
Run Code Online (Sandbox Code Playgroud)

未列出主键或索引

我究竟做错了什么?

Edo*_*rdo 5

您没有做错任何事,不幸的是,pg_restore -t仅恢复表,而不恢复其他内容,无论您如何创建转储以及转储本身内部有什么。V12 PostgreSQL 文档已以某种方式澄清了这一点,其中指出:

该标志的行为与 pg_dump 的 -t 标志不同。目前 pg_restore 中没有任何通配符匹配的规定,也不能在其 -t 中包含模式名称。并且,虽然 pg_dump 的 -t 标志也会转储所选表的辅助对象(例如索引),但 pg_restore 的 -t 标志不包括此类辅助对象。

确保恢复表将包含所有索引的唯一方法是按名称寻址它们,例如:

pg_restore -U bob -d commerce -t orders -I index1 -I index2 -I index3 > orders.dump
Run Code Online (Sandbox Code Playgroud)