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

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


当前回答

POSTGRESQL 9.1.12

转储文件:

pg_dump -U user db_name > archive_name.sql

输入用户密码并按enter键。

恢复:

psql -U user db_name < /directory/archive.sql

输入用户密码并按enter键。

其他回答

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

您的第一个引用源应该是手册页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重新创建它们(正如评论中指出的那样),这取决于转储是如何生成的。

恢复postgres备份文件取决于最初是如何进行备份的。

如果你使用pg_dump和-F c或-F d,你需要使用pg_restore,否则你可以使用

psql -h localhost -p 5432 -U postgres <备份文件

9种备份和恢复postgres数据库的方法

对不起,这些解决方案对我不起作用。我在10号邮差。在Linux上:

我必须将目录更改为pg_hba.conf。 我必须编辑该文件以将方法从peer更改为md5 重启服务:service postgresql-10 Restart 更改目录到我的备份。SQL被定位并执行: PSQL postgres -d database_name -1 -f backup.sql -database_name是数据库的名称 - backup。SQL是我的. SQL备份文件的名称。

如下面的链接所示,你可以使用psql命令来恢复转储文件:

https://www.postgresql.org/docs/8.1/static/backup.html#BACKUP-DUMP-RESTORE

psql dbname < infile

如果你需要设置用户名,只需在命令后添加用户名:

psql dbname < infile username

创建备份

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很重要