简体   繁体   English

使用 pg_dump 和 pg_restore 转储和恢复 postgres 数据库

[英]Dump and restore a postgres database using pg_dump and pg_restore

I'm trying to dump and restore a Postgres database, at once.It means that when I restore it, I have nothing to do but call pg_restore.我正在尝试立即转储和恢复 Postgres 数据库。这意味着当我恢复它时,我除了调用 pg_restore 之外别无他法。 To dump I tryed the follow two instructions:为了转储,我尝试了以下两个说明:

pg_dump -U postgres  -f noc_backup_2022_12_06.dump -Fc treebase

and with the option -C (include the create db instructions)并使用选项 -C (包括创建数据库指令)

pg_dump -U postgres  -C -f backup_2022_12_06.dump -Fc treebase

Notice that the file name in the first is noc_backup... in the second is backup... Now I try to restore: If I call the follow instructions:请注意,第一个文件名是 noc_backup...第二个文件名是 backup...现在我尝试恢复:如果我调用以下说明:

pg_restore  -U postgres -fnoc_backup_2022_12_06.dump
pg_restore  -U postgres -fbackup_2022_12_06.dump
pg_restore  -U postgres -C -fbackup_2022_12_06.dump
pg_restore  -U postgres -C -fnoc_backup_2022_12_06.dump

It get stuck, no request of password, no error message, nothing happen I have to CTRL+C to unlock the prompt.它卡住了,没有密码请求,没有错误消息,没有任何反应我必须按 CTRL+C 来解锁提示。

pg_restore  -U postgres -C  -dtreebase backup_2022_12_06.dump
pg_restore  -U postgres -C  -dtreebase noc_backup_2022_12_06.dump
pg_restore  -U postgres -dtreebase noc_backup_2022_12_06.dump
pg_restore  -U postgres -dtreebase backup_2022_12_06.dump

Each of them raise an error that the database "treebase" doesn't exists.他们每个人都提出了一个错误,即数据库“treebase”不存在。 To successfully restore a database I have to login with psql and CREATE DATABASE .要成功恢复数据库,我必须使用 psql 和CREATE DATABASE登录。 Then I launch:然后我启动:

pg_restore  -U postgres -dtreebase noc_backup_2022_12_06.dump

And it works.它有效。 Since the backup should be executed from the final user, I would make it easiest as possible, two instructions to copy-paste and stop.由于备份应该从最终用户执行,我会尽可能简单,两条指令复制粘贴和停止。 The question is: How do restore whole database only with pg_restore问题是:如何仅使用 pg_restore 恢复整个数据库

I solved, the winning combination is: dump the database with the following instruction, with -C option to include the create database instruction:我解决了,获胜的组合是:使用以下指令转储数据库,并使用 -C 选项来包含创建数据库指令:

pg_dump -U postgres -C  -f backup_2022_12_06.dump -Fc treebase

then to restore access to database postgres, not treebase and include the -C command to create the database然后恢复对数据库 postgres 的访问,而不是 treebase 并包含 -C 命令来创建数据库

pg_restore  -U postgres -C -dpostgres -Fc backup_2022_12_06.dump

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

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