简体   繁体   English

如何在 Windows7 中备份和恢复 PostgreSQL 数据库?

[英]How to backup & Restore PostgreSQL database in Windows7?

I am new to Postgres database.我是 Postgres 数据库的新手。 I have to get the backup from Production Server (pgAdmin Version is 9.2.4) & restore it on my local machine (I have pgAdmin Version 9.4).我必须从生产服务器获取备份(pgAdmin 版本是 9.2.4)并在我的本地机器上恢复它(我有 pgAdmin 版本 9.4)。 I tried to get backup by right clicking on database -> Backup to get the .backup file.我试图通过右键单击数据库-> 备份来获取备份以获取 .backup 文件。 Like shown in below image:如下图所示:

在此处输入图片说明

But when I try to restore the backup file, I get many errors.但是当我尝试恢复备份文件时,出现了很多错误。

I also want to know whether having different ports at both system can also create issues while restoring backups.我还想知道在两个系统上使用不同的端口是否也会在恢复备份时产生问题。 As When I tried to restore backup of same system had no problems.因为当我尝试恢复相同系统的备份时没有问题。

To backup a database you can use pg_dump.exe :要备份数据库,您可以使用pg_dump.exe

  1. Open Powershell打开 Powershell

  2. Go to Postgres bin folder.转到 Postgres bin 文件夹。 For example:例如:

     cd "C:\\Program Files\\PostgreSQL\\9.6\\bin"
  3. Enter the command to dump your database.输入命令以转储数据库。 For example:例如:

     ./pg_dump.exe -U postgres -d my_database_name -f D:\\Backup\\<backup-file-name>.sql
  4. Type password for your postgres user为您的 postgres 用户键入密码

To restore a database you can use psql.exe .要恢复数据库,您可以使用psql.exe (Note, the following is extracted from Alexandr Omelchenko's helpful answer which has been deleted for reasons not clear to me.) (请注意,以下内容摘自 Alexandr Omelchenko 的有用答案,由于我不清楚原因,该答案已被删除。)

  1. Open Powershell打开 Powershell

  2. Go to Postgres bin folder.转到 Postgres bin 文件夹。 For example:例如:

     cd "C:\\ProgramFiles\\PostgreSQL\\9.6\\bin"
  3. Enter the command to restore your database.输入命令以恢复数据库。 For example:例如:

     ./psql.exe -U postgres -d my_database_name -f D:\\Backup\\<backup-file-name>.sql
  4. Type password for your postgres user为您的 postgres 用户键入密码

I was stuck here when creating the database dump file due to version mismatch.由于版本不匹配,我在创建数据库转储文件时被卡在这里。 So I follow the below command to get the backup and restore.所以我按照下面的命令来获取备份和恢复。

pg_dump -h localhost -U postgres -p 5432 YourDbName > BackupFileName.dump

As an alternative, you can try below cmd code.作为替代方案,您可以尝试下面的 cmd 代码。 If you trouble with restoring from pgAdmin4, you can try this.如果您无法从 pgAdmin4 恢复,您可以试试这个。 First you should find the path pg_restore.exe.首先,您应该找到路径 pg_restore.exe。 It is generally in它一般在

C:\\Program Files\\PostgreSQL\\12\\bin> C:\\Program Files\\PostgreSQL\\12\\bin>

run the below code after changing database name and backup path更改数据库名称和备份路径后运行以下代码

pg_restore.exe --verbose --clean -U "postgres" --dbname databaseName c:\database.backup

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM