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

我已经创建了数据库备份并复制了它,但是是否有一种方法可以从命令行恢复备份?我只看到与GUI或pg_dumps相关的东西。


当前回答

我没有看到这里提到转储文件扩展名(*.dump)。

这个解决方案对我很有效:

我有一个转储文件,需要恢复它。

首先,我尝试用pg_restore这样做,并得到:

pg_restore: error: input file appears to be a text format dump. Please use psql.

我用psql做了,工作得很好:

psql -U myUser -d myDataBase < path_to_the_file/file.dump

其他回答

创建备份

pg_dump -h localhost -p 5432 -U postgres -F c -b -v -f 
"/usr/local/backup/10.70.0.61.backup" old_db

-F c是自定义格式(压缩后,可以与-j N并行)-b是包含blobs, -v是verbose, -F是备份文件名。

从备份恢复

pg_restore -h localhost -p 5432 -U postgres -d old_db -v 
"/usr/local/backup/10.70.0.61.backup"

设置-h localhost - option很重要

1. 打开终端。

2. 使用以下命令备份数据库

your postgres bin -> /opt/PostgreSQL/9.1/bin/

源数据库服务器-> 192.168.1.111

您的备份文件位置和名称-> /home/dinesh/db/mydb.backup

您的源数据库名称-> mydatabase

/opt/PostgreSQL/9.1/bin/pg_dump --host '192.168.1.111' --port 5432 --username "postgres" --no-password  --format custom --blobs --file "/home/dinesh/db/mydb.backup" "mydatabase"

3.恢复mydb。备份文件到目标。

您的目标服务器-> localhost

您的目标数据库名称-> mydatabase

创建用于恢复备份的数据库。

/opt/PostgreSQL/9.1/bin/psql -h 'localhost' -p 5432 -U postgres -c "CREATE DATABASE mydatabase"

恢复备份。

/opt/PostgreSQL/9.1/bin/pg_restore --host 'localhost' --port 5432 --username "postgres" --dbname "mydatabase" --no-password --clean "/home/dinesh/db/mydb.backup"

如果你正在使用docker,这个答案可能会有帮助。

启动容器 Docker start <postgres_container_id> .使用实例 访问容器内的bash Docker exec -it <postgres_container_id> bash 将.tar备份文件复制到docker容器(在另一个窗口中) Docker cp postgres_dump.tar <postgres_container_id>:/ 恢复备份 pg_restore -c -U < postgres_user > -d <password> -v "postgres_dump.tar 输入密码

根据创建转储文件的方式,有两个工具可供参考。

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

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.

所以这取决于它被倾倒的方式。如果使用Linux/Unix,您可能可以使用excellent 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

or

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

查看它们各自的手册页-有相当多的选项会影响恢复的工作方式。在恢复之前,您可能必须清除“活动”数据库或从template0重新创建它们(正如评论中指出的那样),这取决于转储是如何生成的。

请看下面的例子,它的工作

C:/Program Files/PostgreSQL/9.4/bin\pg_restore.exe——host localhost——port 5432——username "postgres"——dbname "newDatabase"——no-password——verbose

“C: \用户下载\优\ \新下载\ DB.backup”