postgresql备份和恢复

79 阅读1分钟

记录备份postgresql 数据过程

背景

使用的操作系统是debian

有两台机器: AB
机器A是备份文件存储的机器
机器B是正在运行的,准备要备份机器

前置

机器A安装postgresql-client 来安装pg_dump

sudo apt-get install postgresql-client

开始备份:

pg_dump -h B机器ip  -U 用户名  -f back.sql -d 需要备份数据

输入上述命令后,就会生成back.sql 了
使用命令ls -all -h查看生成的sql

$ls -all -h                                                                                                                                        
total 259M                                                                                                                                                               
drwxr-xr-x  2 root root 4.0K Apr 29 11:38 .                                                                                                                              
drwx------ 13 root root 4.0K Apr 29 11:32 ..                                                                                                                             
-rw-r--r--  1 root root 259M Apr 29 11:38 back.sql 

恢复: 需要把back.sql复制到B本机,然后切换到postgres用户再执行以下命令

 psql -U postgres   -d article  < back.sql