备份与恢复

备份数据库
cmd打开终端,进入postgresql的bin目录找倒pg_dump.exe,用这个备份。可以用pg_dump.exe –help查看使用方法
其中:
-F 后面跟要存储的格式,c|d|t|p 分别代表四中格式:定制、目录、(压缩文件)tar、明文(认值),
-f后面跟所存储的目录或者文件。可以相对路径”..\dir”,绝对路径”d:\dir”
-d后面跟要备份的数据库名称
-j 后面跟线程数,可以多线程备份
-n 后面跟指定的schema,如果多个schema,那么多次用就行,比如-n schema1 -n schema2

  • 定制(即custom,自定义压缩格式)
pg_dump.exe -h localhost -p 5432 -U postgres -F c -b -v -d dbname -f dbnamec.backup
  • 目录(目录下面有每张表的压缩文件支持并行备份,加-j num)
pg_dump.exe -h localhost -p 5432 -U postgres -F d -b -v -d dbname -f dbname
  • 压缩文件(占用空间很大!)
pg_dump.exe -h localhost -p 5432 -U postgres -F t -b -v -d dbname -f dbname.tar
pg_dump.exe -h localhost -p 5432 -U postgres -F p -b -v -d dbname -f dbnamep.backup

其中,tar和明文占用空间最大,而目录和定制明显更小。

恢复数据库

  • 从tar、定制、目录恢复
pg_restore.exe -h localhost -p 5432 -U postgres -w -v -d dbname “dbname.tar”
pg_restore.exe -h localhost -p 5432 -U postgres -w -v -d dbname “dbnamec.backup”
pg_restore.exe -h localhost -p 5432 -U postgres -w -v -d dbname “dbname”
  • 从明文恢复
psql.exe -h localhost -p 5432 -U postgres -d dbname -fdbname.backup”

说明:这种使用起来最不方便,但是最通用,无法选择性地仅恢复部分数据。

详细信息参照 pg_restore.exe –help psql.exe –help

相关文章

项目需要,有个数据需要导入,拿到手一开始以为是mysql,结果...
本文小编为大家详细介绍“怎么查看PostgreSQL数据库中所有表...
错误现象问题原因这是在远程连接时pg_hba.conf文件没有配置正...
因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本...
wamp 环境 这个提示就是说你的版本低于10了。 先打印ph...
psycopg2.OperationalError: SSL SYSCALL error: EOF detect...