我有一个具有两种模式的数据库,即开发和生产。现在,我从模式开发中转储了一些表,并希望将转储的表恢复到模式生产中。 我认为它应该非常简单(实际上可能是),但我无法仅使用 pg_dump 和 pg_restore 来做到这一点。 我认为这可以通过以下两种方式之一完成:1,转储开发中的表,但将转储文件中的架构更改为生产;或 2,从转储文件中恢复表,但将架构更改为生产(因此这些表将在生产中恢复)。我已经阅读了 pg_dump 和 pg_restore 的手册页,但似乎 pg_dump 不允许我做 1,而 pg_restore 不允许我做 2。我仍然怀疑我可能错过了一些东西。 我正在使用 Postgres 15。如有任何帮助,我们将不胜感激。 谢谢。
在转储/恢复期间无法重命名架构。
我建议采用以下解决方法:
# dump the schema
pg_dump -F c -U postgres -n myschema -f dumpfile dbname
createdb -U postgres scratch
# restore the schema to a scratch database
pg_restore -d scratch -U postgres dumpfile
# rename the schema in the scratch database
psql -d scratch -U postgres -c 'ALTER SCHEMA myschema RENAME TO newschema'
# dump the renamed schema from the scratch database
pg_dump -F c -U postgres -n newschema -f dumpfile scratch
# restore the renamed schema to the original database
pg_restore -d dmname -U postgres dumpfile
dropdb -U postgres scratch