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

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


当前回答

如果您使用pg_dump创建了备份,您可以通过以下方式轻松地恢复它:

打开命令行窗口 进入Postgres bin文件夹。例如:cd "C:\ProgramFiles\PostgreSQL\9.5\bin" 输入恢复数据库的命令。例如:psql.exe -U postgres -d YourDatabase -f D:\Backup\.sql 输入postgres用户的密码 检查恢复进程

其他回答

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

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

Backup:  $ pg_dump -U {user-name} {source_db} -f {dumpfilename.sql}

Restore: $ psql -U {user-name} -d {desintation_db} -f {dumpfilename.sql}

如果您使用pg_dump创建了备份,您可以通过以下方式轻松地恢复它:

打开命令行窗口 进入Postgres bin文件夹。例如:cd "C:\ProgramFiles\PostgreSQL\9.5\bin" 输入恢复数据库的命令。例如:psql.exe -U postgres -d YourDatabase -f D:\Backup\.sql 输入postgres用户的密码 检查恢复进程

try:

pg_restore -h localhost -p 5432 -U <username> -d <dbname> -1 <filename>

试试下面的命令是否能帮到你:

sudo su - yourdbuser
psql
\i yourbackupfile