postgres 数据库备份还原

# psql 连接数据库

psql -U auth_db -h 127.0.0.1 -p 5432

## 备份数据库
### 用 postgres账号 备份 zzb_db数据库到  zzb_db.sql文件中
pg_dump -h 127.0.0.1 -p 5432 -U postgres -f zzb_db.sql zzb_db

pg_dump -h 127.0.0.1 -p 5432 -U postgres -f auth_db.sql auth_db
pg_dump -h 47.106.147.83 -p 5432 -U postgres -f auth_db.sql auth_db

### 备份表
### 用 zzb_db 账号 备份 zzb_db数据库中  表tp_tmp  到 tp_tmp.sql文件中
pg_dump -h 127.0.0.1 -p 5432 -U zzb_db -t tp_tmp -f tp_tmp.sql zzb_db

### 还原表

psql -h 127.0.0.1 -p 5432 -U zzb_db -f tp_tmp.sql zzb_db

## 删除数据库、创建用户、授权

drop database zzb_db;
create user zzb_db superuser password '123456';
create database zzb_db owner zzb_db;
grant all on database zzb_db to zzb_db;

## 还原数据库
psql -h 127.0.0.1 -U postgres -d zzb_db < ~/Desktop/db_20200313.bak

原文地址:https://www.cnblogs.com/ITBread/p/14042188.html