使用命令行恢复postgres备份文件? [英] Restore a postgres backup file using the command line?

查看:434
本文介绍了使用命令行恢复postgres备份文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新到postgresql,在本地,我使用pgadmin3。然而,在远程服务器上,我没有这样的奢侈。



我已经创建了数据库的备份并复制它,但是,有一种方法从命令行恢复备份?我只看到与GUI或pg_dumps相关的东西,所以,如果有人可以告诉我如何去做,这将是可怕的!

解决方案<



您的第一个参考资料来源应该是手册页 pg_dump(1)因为这是创建转储本身。它说:


Dumps可以以脚本或
归档文件格式输出。脚本转储是
纯文本文件,包含重建
所需的SQL
命令,数据库保存时的状态为
。从
恢复从这样的脚本,将其馈送到
psql(1)。脚本文件可以使用
重建数据库,甚至在其他机器上使用
和其他
架构;

替代存档文件格式
必须与pg_restore(1)一起使用到
b重建数据库。它们允许
pg_restore选择性地恢复什么
,甚至在恢复之前重新排序
项目。
存档文件格式设计为
可跨架构移植。


倾销。你可以使用优秀的文件(1)命令 - 如果它提到ASCII文本和/或SQL,它应该用 psql 否则您应该可以使用 pg_restore



还原很简单:

  psql -U< username> -d< dbname> -1 -f< filename> .sql 

  pg_restore -U< username> -d< dbname> -1< filename> .dump 

查看他们各自的manpages - 恢复如何工作。您可能需要清理您的活数据库或从还原之前的template0(在注释中指出)重新创建它们,具体取决于转储的生成方式。


I'm new to postgresql, and locally, I use pgadmin3. On the remote server, however, I have no such luxury.

I've already created the backup of the database and copied it over, but, is there a way to restore a backup from the command line? I only see things related to GUI or to pg_dumps, so, if someone can tell me how to go about this, that'd be terrific!

解决方案

There are two tools to look at, depending on how you created the dump file.

Your first source of reference should be the man page pg_dump(1) as that is what creates the dump itself. It says:

Dumps can be output in script or archive file formats. Script dumps are plain-text files containing the SQL commands required to reconstruct the database to the state it was in at the time it was saved. To restore from such a script, feed it to psql(1). Script files can be used to reconstruct the database even on other machines and other architectures; with some modifications even on other SQL database products.

The alternative archive file formats must be used with pg_restore(1) to rebuild the database. They allow pg_restore to be selective about what is restored, or even to reorder the items prior to being restored. The archive file formats are designed to be portable across architectures.

So depends on the way it was dumped out. You can probably figure it out using the excellent file(1) command - if it mentions ASCII text and/or SQL, it should be restored with psql otherwise you should probably use pg_restore

Restoring is pretty easy:

psql -U <username> -d <dbname> -1 -f <filename>.sql

or

pg_restore -U <username> -d <dbname> -1 <filename>.dump

Check out their respective manpages - there's quite a few options that affect how the restore works. You may have to clean out your "live" databases or recreate them from template0 (as pointed out in a comment) before restoring, depending on how the dumps were generated.

这篇关于使用命令行恢复postgres备份文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆