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

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


当前回答

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

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

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

其他回答

遵循以下3个步骤:

启动postgresql server - sudo systemctl Start postgresql 启用same - sudo systemctl Enable postgresql restore命令- pg_restore -h localhost -p 5432 - u postgres -d old_db . sh

假设转储文件在同一目录中

链接:

https://www.postgresqltutorial.com/postgresql-restore-database https://askubuntu.com/questions/50621/cannot-connect-to-postgresql-on-port-5432

您可能需要以postgres身份登录,以便对数据库具有完全的权限。

su - postgres
psql -l                      # will list all databases on Postgres cluster

pg_dump / pg_restore

  pg_dump -U username -f backup.dump database_name -Fc 

指定备份文件的格式:

c将使用自定义的PostgreSQL格式,压缩后的备份文件大小最小 D表示目录,其中每个文件是一个表 t用于TAR存档(大于自定义格式) -h/——host运行服务器的机器的主机名 -W/——password在连接数据库之前强制pg_dump提示输入密码

恢复备份:

   pg_restore -d database_name -U username -C backup.dump

参数-C应该在导入数据之前创建数据库。如果它不工作,你总是可以创建数据库,如。使用命令(以postgres用户或其他有创建数据库权限的用户)createdb db_name -O owner

pg_dump - psql

如果你没有指定参数-F默认的纯文本SQL格式被使用(或与-F p).那么你不能使用pg_restore。可以使用psql导入数据。

备份:

pg_dump -U username -f backup.sql database_name

恢复:

psql -d database_name -f backup.sql

恢复转储文件

psql -d [Dbname] -U [UserName] -p 5432 < [FileLocation]

恢复一个.SQL文件

pg_restore -U [Username] -d [Dbname] -1 [FileLocation]

如果出现用户身份验证错误,请转到程序文件PSQL/data文件夹中的pg_hba.conf文件,并将“METHOD”更改为“Trust”。 重新启动windows服务中的psql serive (Win + R—> services.msc)。

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

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

如果您已经创建了一个名为mydb的新数据库,使用psql将.sql转储恢复到该数据库,

psql --file=dump.sql --username=postgres --host=localhost --port=5432 mydb

密码将由PSQL提示

连接选项为

  -h, --host=HOSTNAME      database server host or socket directory (default: "/var/run/postgresql")
  -p, --port=PORT          database server port (default: "5432")
  -U, --username=USERNAME  database user name (default: "xyz")
  -w, --no-password        never prompt for password
  -W, --password           force password prompt (should happen automatically)