93

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!

12
  • 39
    i just can't accept that this is still happening in 2017, and the solution is to hack the dump file.
    – Sharky
    Commented Feb 28, 2017 at 15:40
  • 3
    2018 my friend! Commented Aug 23, 2018 at 17:46
  • 7
    2020 my friend! @user1880957 question is about automatic built-in way and not manual with dependency to some GUI tool Commented May 27, 2020 at 12:07
  • 6
    2021 now damn it x)
    – Nicoowr
    Commented Feb 22, 2021 at 9:38
  • 11
    2022 and still no fix for this
    – Bklyn
    Commented Jan 24, 2022 at 19:04

8 Answers 8

77

A quick and dirty way:

1) rename default schema:

alter schema public rename to public_save;

2) create new schema as default schema:

create schema public;

3) restore data

pg_restore -f pub.backup db_temp [and whatever other options]

4) rename schemas according to need:

alter schema public rename to temp_schema;
alter schema public_save rename to public;
71

There is a simple solution:

  • Create your backup dump in plain SQL format (format "p" using the parameter --format=p or -F p)
  • Edit your pub.backup.sql dump with your favorite editor and add the following two lines at the top of your file:

create schema myschema;

SET search_path TO myschema;

Now you can restore your backup dump with the command

psql -f pub.backup.sql

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.

8
  • Is there a neat way to actually edit only the head of a large dump file? Even things like this involve duplicating it: superuser.com/questions/246837/… Commented May 5, 2015 at 2:04
  • 1
    @PeterEhrlich Depending on how large your files are and available RAM, you can use vim to open a gzip'd dump file directly. It still gets expanded in memory but I was able to open, edit, and save a 750MB (.gz) file with only a short pause when saving. YMMV.
    – David
    Commented May 12, 2015 at 16:48
  • 2
    @PeterEhrlich You can use a subprocess to write something to a file before the contents of the dump. For example: ( echo "CREATE SCHEMA myschema; SET search_path TO myschema;" ; pg_dump -F p ... ) | gzip -9 > dump.sql.gz
    – gldnspud
    Commented Feb 18, 2016 at 22:59
  • 1
    @gldnspud but as I understand it this will just mean there's 2 "SET search_path"s in the file? The second one being the one we don't want.
    – Andy Smith
    Commented Jul 14, 2017 at 13:45
  • 35
    This won't work in Postgres 9.6.8 / 10.3: pg_dump no longer uses SET search_path; instead, it prefixes the schema name to all commands in the dump
    – user2384183
    Commented Mar 13, 2018 at 18:56
19

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.

1
  • 2
    I know the answer is old, but for someone that just wants to use this accepted answer: @shaunc is way more secure and robust, and works with compressed dumps.
    – bln_dev
    Commented Jun 17, 2022 at 16:02
16

Probably the easiest method would be to simply rename the schema after restore, ie with the following SQL:

ALTER SCHEMA my_schema RENAME TO temp_schema

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.

3
  • 1
    This solution can make a lot of mess isn't it
    – abubacker
    Commented Nov 16, 2010 at 12:21
  • 4
    In what way?? The SQL statement is the only safe way to change a schema name. The other solution is also suggested in another answer and we both explain that it's risky. Poor downvoting.
    – Hamish
    Commented Nov 16, 2010 at 18:33
  • 2
    I changed the schema name but, after that, I'm not able to change the serach path! User is not able to search any table after the rename! Commented Jan 27, 2012 at 11:25
4

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.

3

Rename the schema in a temporary database.

Export the schema:

pg_dump --schema-only --schema=prod > prod.sql

Create a new database. Restore the export:

psql -f prod.sql

ALTER SCHEMA prod RENAME TO somethingelse;

pg_dump --schema-only --schema=somethingelse > somethingelse.sql

(delete the database)

For the data you can just modify the set search_path at the top.

0
2

As noted, there's no direct support in pg_dump, psql or pg_restore to change the schema name during a dump/restore process. But it's fairly straightforward to export using "plain" format then modify the .sql file. This Bash script does the basics:

rename_schema () {

  # Change search path so by default everything will go into the specified schema
  perl -pi -e "s/SET search_path = $2, pg_catalog/SET search_path = $3, pg_catalog, $2;/" "$1"

  # Change 'ALTER FUNCTION foo.' to 'ALTER FUNCTION bar.'
  perl -pi -e 's/^([A-Z]+ [A-Z]+) '$2'\./$1 '$3'./' "$1"

  # Change the final GRANT ALL ON SCHEMA foo TO PUBLIC
  perl -pi -e 's/SCHEMA '$2'/SCHEMA '$3'/' "$1"

}

Usage:

pg_dump --format plain --schema=foo --file dump.sql MYDB
rename_schema dump.sql foo bar
psql -d MYDB -c 'CREATE SCHEMA bar;'
psql -d MYDB -f dumpsql
3
  • (Everyone: Please feel free to update this answer to improve the Bash.) Commented May 21, 2018 at 3:37
  • 1
    I would have done it with sed "s/\b$OLD_SCHEMA/$NEW_SCHEMA/g"
    – Pipo
    Commented Apr 23, 2019 at 14:41
  • 2
    As mentioned in one of the other answers, one needs to be careful if the schema name exists elsewhere (e.g. in the data). His perl script is slightly safer than plain substituting all appearances of $OLD_SCHEMA. Commented Oct 7, 2021 at 10:08
2

The question is pretty old, but maybe can help some one.

Streaming the output of pg_restore to sed and replace the schema name in order to import the dump to a different schema.

Something like:

pg_restore ${dumpfile} -f - | \
    sed -e "s/OWNER TO ${source_owner}/OWNER TO ${target_owner}/" \
        -e "s/${source_schema}/${target_schema}/" | \
       psql -h ${pgserver} -d ${dbname} -U ${pguser} 
3
  • you meant pg_dump as the first command opposed to pg_restore?
    – John Smith
    Commented Aug 10, 2021 at 9:41
  • @JohnSmith no I mean pg_restore. You should do your dump and then perform the replace schema while importing the dump on the target db.
    – Dwhitz
    Commented Aug 10, 2021 at 9:57
  • @Dwhitz there's small typo error; it should be {"source_schema"/"target_schema"/}
    – snackbar
    Commented Mar 11, 2022 at 4:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.