admin管理员组文章数量:1432178
I have a database with two schemas, say development and production. Now I dumped a few tables from schema development and wanted to restore the dumped tables into schema production. I thought it should be quite simple (may actually is), but I haven't been able to do so just with pg_dump and pg_restore. I thought this could be done in one of two ways: either 1, to dump the tables from development but have the schema changed to production in the dump file or 2, to restore the tables from dump file but have the schema changed to production (so the tables will be restored in production). I've read the man pages of pg_dump and pg_restore, but seems pg_dump doesn't allow me to do 1 and pg_restore doesn't allow me to do 2. I still suspect that I may have missed something. I'm using Postgres 15. Any help's appreciated. Thanks.
I have a database with two schemas, say development and production. Now I dumped a few tables from schema development and wanted to restore the dumped tables into schema production. I thought it should be quite simple (may actually is), but I haven't been able to do so just with pg_dump and pg_restore. I thought this could be done in one of two ways: either 1, to dump the tables from development but have the schema changed to production in the dump file or 2, to restore the tables from dump file but have the schema changed to production (so the tables will be restored in production). I've read the man pages of pg_dump and pg_restore, but seems pg_dump doesn't allow me to do 1 and pg_restore doesn't allow me to do 2. I still suspect that I may have missed something. I'm using Postgres 15. Any help's appreciated. Thanks.
Share Improve this question asked Nov 18, 2024 at 22:19 ShipingShiping 1,3372 gold badges14 silver badges24 bronze badges 2 |1 Answer
Reset to default 2There is no way to rename a schema during dump/restore.
I suggest the following workaround:
# 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
本文标签: postgresqlPostgres restore tables from a dump into a different schemaStack Overflow
版权声明:本文标题:postgresql - Postgres restore tables from a dump into a different schema - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745590540a2665169.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
pg_restore ... -f some_file
. This will create a plain text file SQL file where you can search and replace the schema name. Then dopsql ... -f some_file
to restore to database. Or justpg_dump ...
to plain text in the first place. In other words don't use-Fc
. Again you will need to restore withpsql
. 2) In source database copy/move tables toproduction
schema and then do dump. – Adrian Klaver Commented Nov 18, 2024 at 22:26LIKE
. Generally it is easier to set up separate dev and production databases and use the same naming in each. – Adrian Klaver Commented Nov 18, 2024 at 22:44