使用命令行恢复postgres备份文件?

编程入门 行业动态 更新时间:2024-10-26 08:26:23
本文介绍了使用命令行恢复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备份文件?

本文发布于:2023-10-23 00:19:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519180.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令行   备份文件   postgres

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!