简体   繁体   English

使用 pg_restore 通过 SSH 恢复 Postgres 数据库

[英]Restore Postgres database using pg_restore over SSH

I have a database server without much disk space, so I took a backup of the entire db (let's just call it redblue) and saved it locally using the following command (I don't have pg running on my computer):我有一个没有太多磁盘空间的数据库服务器,所以我备份了整个数据库(让我们称之为 redblue)并使用以下命令将其保存在本地(我的计算机上没有运行 pg):

ssh admin@w.x.y.z "pg_dump -U postgres redblue -h localhost " \
 >> db_backup_redblue.sql

I'd like to now restore it to another server (1.2.3.4) which contains an older version of "redblue" database - however wanted to ask if this is right before I try it:我现在想将它恢复到另一台服务器(1.2.3.4),其中包含旧版本的“redblue”数据库 - 但是在我尝试之前想问问这是否正确:

ssh admin@1.2.3.4 "pg_restore -U postgres -C redblue" \
<< db_backup_redblue.sql

I wasn't sure if I need to do -C with the name of the db or not?我不确定是否需要使用数据库名称执行 -C ?

Will the above command overwrite/restore the remote database with the file I have locally?上面的命令会用我本地的文件覆盖/恢复远程数据库吗?

Thanks!谢谢!

No, that will do nothing good.不,那不会有什么好处。

You have to start pg_restore on the machine where the dump is.您必须在转储所在的机器上启动pg_restore Actually, since this is a plain format dump, you have to use psql rather than pg_restore :实际上,由于这是一个普通格式的转储,您必须使用psql而不是pg_restore

psql -h 1.2.3.4 -U postgres -d redblue -f db_backup_redblue.sql

That requires that there is already an empty database redblue on the target system.这要求目标系统上已经有一个空的数据库redblue

If you want to replace an existing database, you have to use the --clean and --create options with pg_dump .如果要替换现有数据库,则必须将--clean--create选项与pg_dump

If you want to use SSL, you'll have to configure the PostgreSQL server to accept SSL connections, see the documentation .如果要使用 SSL,则必须将 PostgreSQL 服务器配置为接受 SSL 连接,请参阅文档

I'd recommend the “custom” format of pg_dump .我推荐pg_dump的“自定义”格式。

Of course, you can do this :) Assuming you use ssh keys to authorize user from source host to destination host.当然,您可以这样做:) 假设您使用 ssh 密钥授权用户从源主机到目标主机。

On the source host you do the pg_dump, then pipe through ssh to destination host like this:在源主机上执行 pg_dump,然后通过 ssh 管道传输到目标主机,如下所示:

pg_dump -C nextcloud | ssh -i .ssh/pg_nextcloud_key postgres@192.168.0.54 psql -d template1

Hope that helps ;)希望有帮助;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM