gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:04:00 28 4
gpt4 key购买 nike

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

我已经创建了数据库备份并将其复制过来,但是有没有办法从命令行恢复备份?我只看到与 GUI 或 pg_dumps 相关的内容。

最佳答案

根据您创建转储文件的方式,可以使用两种工具。

您的第一个引用来源应该是手册页 pg_dump因为这就是创建转储本身的原因。它说:

Dumps can be output in script orarchive file formats. Script dumps areplain-text files containing the SQLcommands required to reconstructthe database to the state it wasin at the time it was saved. Torestore from such a script, feed it topsql(1). Script files can be usedto reconstruct the database evenon other machines and otherarchitectures; with some modificationseven on other SQL database products.

The alternative archive file formatsmust be used with pg_restore(1) torebuild the database. They allowpg_restore to be selective about whatis restored, or even to reorder theitems prior to being restored. Thearchive file formats are designed tobe portable across architectures.

所以取决于它被倾倒的方式。如果使用 Linux/Unix,您可能可以使用优秀的 file(1) 命令来解决它 - 如果它提到 ASCII 文本和/或 SQL,它应该用 psql 恢复否则你应该使用 pg_restore .

恢复非常简单:

psql -U username -d dbname < filename.sql

-- For Postgres versions 9.0 or earlier
psql -U username -d dbname -1 -f filename.sql

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

查看它们各自的联机帮助页 - 有很多选项会影响恢复的工作方式。在恢复之前,您可能必须清理“实时”数据库或从 template0 重新创建它们(如评论中所指出的),具体取决于转储的生成方式。

关于postgresql - 使用命令行恢复 postgres 备份文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2732474/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com