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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28048412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:55:30  来源:igfitidea点击:

How to backup & Restore PostgreSQL database in Windows7?

windowspostgresqlpgadmin

提问by user1400290

I am new to Postgres database. 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). I tried to get backup by right clicking on database -> Backup to get the .backup file. Like shown in below image:

我是 Postgres 数据库的新手。我必须从生产服务器获取备份(pgAdmin 版本是 9.2.4)并在我的本地机器上恢复它(我有 pgAdmin 版本 9.4)。我试图通过右键单击数据库-> 备份来获取备份以获取 .backup 文件。如下图所示:

enter image description here

在此处输入图片说明

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.

我还想知道在两个系统上使用不同的端口是否也会在恢复备份时产生问题。因为当我尝试恢复相同系统的备份时没有问题。

回答by Stephen Paul

To backup a databaseyou can use pg_dump.exe:

要备份数据库,您可以使用pg_dump.exe

  1. Open command line window
  2. Go to Postgres bin folder. For example:

    cd "C:\Program Files\PostgreSQL.6\bin"
    
  3. Enter the command to dump your database. For example:

    pg_dump.exe -U postgres -d MediaData -f D:\Backup\<backup-file-name>.sql
    
  4. Type password for your postgres user

  1. 打开命令行窗口
  2. 转到 Postgres bin 文件夹。例如:

    cd "C:\Program Files\PostgreSQL.6\bin"
    
  3. 输入命令以转储数据库。例如:

    pg_dump.exe -U postgres -d MediaData -f D:\Backup\<backup-file-name>.sql
    
  4. 为您的 postgres 用户键入密码

To restore a databaseyou can use psql.exe. (Note, the following is extracted from Alexandr Omelchenko's helpful answer which has been deleted for reasons not clear to me.)

要恢复数据库,您可以使用psql.exe. (请注意,以下内容摘自 Alexandr Omelchenko 的有用答案,由于我不清楚原因,该答案已被删除。)

  1. Open command line window
  2. Go to Postgres bin folder. For example:

    cd "C:\ProgramFiles\PostgreSQL.5\bin"
    
  3. Enter the command to restore your database. For example:

    psql.exe -U postgres -d MediaData -f D:\Backup\<backup-file-name>.sql
    
  4. Type password for your postgres user

  1. 打开命令行窗口
  2. 转到 Postgres bin 文件夹。例如:

    cd "C:\ProgramFiles\PostgreSQL.5\bin"
    
  3. 输入命令以恢复数据库。例如:

    psql.exe -U postgres -d MediaData -f D:\Backup\<backup-file-name>.sql
    
  4. 为您的 postgres 用户键入密码

回答by Mohammad

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