我想用不同的模式恢复数据库

发布于 2024-10-02 15:24:16 字数 311 浏览 3 评论 0原文

我已经使用以下命令对名为 temp1 的数据库进行了转储

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

现在我想在名为“db_temp”的不同数据库中恢复转储,但我只希望所有表都应该在“db_temp”数据库​​中的“temp_schema”(不是 fms temp1 数据库中的默认模式)中创建。

有没有办法使用 pg_restore 命令来做到这一点?

任何其他方法也将受到赞赏!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

暮凉 2024-10-09 15:24:16

一种快速而肮脏的方法:

1)重命名默认模式:

alter schema public rename to public_save;

2)创建新模式作为默认模式:

create schema public;

3)恢复数据

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

4)根据需要重命名模式:

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

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;
烟沫凡尘 2024-10-09 15:24:16

有一个简单的解决方案:

  • 以纯 SQL 格式创建备份转储(使用参数 --format=p-F p 格式“p”)
  • 编辑您的 pub.txt 文件。使用您最喜欢的编辑器转储 backup.sql 并在文件顶部添加以下两行:

创建模式 myschema;

将 search_path 设置为 myschema;

现在您可以使用以下命令恢复备份转储

psql -f pub.backup.sql

set search_path to 命令会将 myschema 设置为默认值,以便在此架构中创建新表和其他对象,独立于他们之前居住的“默认”模式。

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.

z祗昰~ 2024-10-09 15:24:16

pg_restore 本身没有办法。您可以做的是使用 pg_restore 生成 SQL 输出,然后通过 sed 脚本等发送该输出来更改它。不过,您需要小心编写 sed 脚本的方式,因此它不会匹配并更改数据内的内容。

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.

剩余の解释 2024-10-09 15:24:16

也许最简单的方法是在恢复后简单地重命名架构,即使用以下 SQL:

ALTER SCHEMA my_schema RENAME TO temp_schema

我相信,因为您使用压缩存档格式作为 pg_dump 的输出,所以您无法在恢复之前更改它。该选项是使用默认输出并对架构名称进行搜索和替换,但这会有风险,如果您不小心,可能会导致数据损坏。

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.

只涨不跌 2024-10-09 15:24:16

如果您只有几个表,那么您可以一次恢复一个表,当您指定-t tablename时,pg_restore接受-d数据库。当然,您必须在恢复表之前设置架构,然后在完成恢复表后整理索引和约束。

或者,在不同的端口上设置另一台服务器,使用新的 PostgreSQL 服务器进行恢复,重命名架构,转储它,然后恢复到原始数据库中。当然,这有点麻烦,但它可以完成工作。

如果您喜欢冒险,您也许可以使用十六进制编辑器更改转储文件中的数据库名称。我认为它只在转储中的一处提到,只要新旧数据库名称相同,它就应该起作用。 YMMV,不要在生产环境中做这样的事情,如果这会炸毁并夷平你的家乡,请不要怪我,以及所有其他常见的免责声明。

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.

半边脸i 2024-10-09 15:24:16

重命名临时数据库中的架构。

导出架构:

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

创建一个新数据库。恢复导出:(

psql -f prod.sql

ALTER SCHEMA prod RENAME TO somethingelse;

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

删除数据库)

数据只需修改顶部设置的search_path即可。

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.

时光清浅 2024-10-09 15:24:16

如前所述,pg_dump、psql 或 pg_restore 不直接支持在转储/恢复过程中更改模式名称。但使用“普通”格式导出然后修改 .sql 文件相当简单。这个 Bash 脚本完成了基础工作:

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"

}

用法:

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

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
楠木可依 2024-10-09 15:24:16

这个问题很老了,但也许可以帮助某人。

pg_restore 的输出流式传输到 sed 并替换架构名称,以便将转储导入到不同的架构。

例如:

v.15.6 之前的 Pg 恢复

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} 

从 v.15.6 开始的 Pg 恢复

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} 

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 before v.15.6

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} 

Pg restore from v.15.6

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} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文