I have taken a dump of a database named temp1
, by using the follwing command
$ pg_dump -i -h localhost -U postgres -F c -b -v -f pub.backup temp1
Now I want to restore the dump in a different database called "db_temp" , but in that I just want that all the tables should be created in a "temp_schema" ( not the default schema which is in the fms temp1 database ) which is in the "db_temp" database.
Is there any way to do this using pg_restore
command?
Any other method also be appreciated!
A quick and dirty way:
1) rename default schema:
2) create new schema as default schema:
3) restore data
4) rename schemas according to need:
Rename the schema in a temporary database.
Export the schema:
Create a new database. Restore the export:
(delete the database)
For the data you can just modify the set
search_path
at the top.There is a simple solution:
--format=p
or-F p
)Now you can restore your backup dump with the command
The
set search_path to <schema>
command will set myschema as the default, so that new tables and other objects are created in this schema, independently of the "default" schema where they lived before.If you only have a few tables then you can restore one table at a time,
pg_restore
accepts-d database
when you specify-t tablename
. Of course, you'll have to set up the schema before restoring the tables and then sort out the indexes and constraints when you're done restoring the tables.Alternatively, set up another server on a different port, restore using the new PostgreSQL server, rename the schema, dump it, and restore into your original database. This is a bit of a kludge of course but it will get the job done.
If you're adventurous you might be able to change the database name in the dump file using a hex editor. I think it is only mentioned in one place in the dump and as long as the new and old database names are the same it should work. YMMV, don't do anything like this in a production environment, don't blame me if this blows up and levels your home town, and all the rest of the usual disclaimers.
There's no way in pg_restore itself. What you can do is use pg_restore to generate SQL output, and then send this through for example a sed script to change it. You need to be careful about how you write that sed script though, so it doesn't match and change things inside your data.
Probably the easiest method would be to simply rename the schema after restore, ie with the following SQL:
I believe that because you're using the compressed archive format for the output of pg_dump you can't alter it before restoring. The option would be to use the default output and do a search and replace on the schema name, but that would be risky and could perhaps cause data to be corrupted if you were not careful.