我想用不同的模式还原数据库

我使用以下命令获取了一个名为 temp1的数据库的转储

$  pg_dump -i -h localhost  -U postgres -F c -b -v -f pub.backup temp1

现在我想在另一个名为“ db _ temp”的数据库中恢复转储,但是在这里,我只希望所有表都应该在“ temp _ schema”数据库中创建(而不是 fms tem1数据库中的默认模式) ,该数据库位于“ db _ temp”数据库中。

有什么办法可以用 pg_restore命令来完成这项工作吗?

任何其他的方法也是值得赞赏的!

84397 次浏览

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.

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.

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.

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;

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.

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.

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

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} | \
sed -e "s/OWNER TO ${source_owner}/OWNER TO ${target_owner}/" \
-e "s/${source_schema}/${target_schema}/" | \
psql -h ${pgserver} -d ${dbname} -U ${pguser}