PostgreSQL七备份还原

postgresql 官方给出以下三种方法进行数据备份:

1. sql dump(sql 转储)
2. File system level backup(文件系统级别备份)
3. Continuous archiving(连续归档)


【备份】

pg_dump dbname > outfile

pg_dump 是客户端应用,可以备份任何本地或远程ps数据库。若要备份整个数据库,最好拥有数据库最大权限superuser。 当然也可设置选项 -n schema 或 -t table 备份部分数据。 使用 pg_dump 备份数据库时,连接到数据库的选项与 psql 一样,可连接到指定的主机和端口( -h host and -p port)。 pg_dump 当前是主要的备份方法。对于转移数据来说还是用pg_dump,相比其他方法对原服务器的依赖性没那么强。 pg_dump 备份在内部是一致的。dump 出的数据为当时数据库的快照,并不堵塞其他对数据库的操作(除了那些需要排它锁的操作,如 ALTER TABLE)。

示例:

pg_dump testdb > /home/postgres/testdb.dmp
pg_dump -h 127.0.0.1 -Upostgres testdb > /home/postgres/testdb2.dmp

【还原】
psql dbname < infile

这里的 infile 为 pg_dump 的导出文件,此处数据库不会被创建,创建数据库需要从模板库 template0 创建。
createdb -T template0 dbname

在恢复数据库前,那些在转储数据库中拥有对象或权限的用户,必须已经存在,否则还原会失败,因为恢复过程相当于重新执行一遍sql操作。认情况下,psql 脚本在执行遇到sql错误时仍然继续。可设置 ON_ERROR_STOP 中断。
psql --set ON_ERROR_STOP=on dbname < infile

若要保证数据的完整性,还原时可指定 -1 或 --single-transaction 参数。使用该参数,即使遇到一个微小的错误,还原都会回滚。

示例:

dropdb -h 127.0.0.1 -Upostgres testdb
createdb -h 127.0.0.1 -Upostgres  -T template0 testdb
psql -h 127.0.0.1 -Upostgres testdb < /home/postgres/testdb.dmp

--还原为另一个新的数据库 newdb
createdb -h 127.0.0.1 -Upostgres  -T template0 newdb
psql -h 127.0.0.1 -Upostgres newdb < /home/postgres/testdb.dmp

可以使用管道直接复制数据库到其他服务器上:

pg_dump -h host1 dbname | psql -h host2 dbname
示例:
createdb -h 127.0.0.1 -Upostgres  -T template0 testdb2
pg_dump -h 127.0.0.1 -Upostgres testdb | psql -h 127.0.0.1 -Upostgres testdb2

还原好数据库库后,应该刷新统计信息。ANALYZE 收集表内容统计信息,然后把结果保存在系统表 pg_statistic 里。
ANALYZE [ VERBOSE ] [ table [ (column [,...] ) ] ]

【备份所有:pg_dumpall】

pg_dump 只能备份单个数据库,也没有 dump 相关的角色和表空间信息,因为这些信息是群集级别的,不是数据库级别内的。postgresql 提供了完整信息的备份命令 pg_dumpall。

转储备份:
pg_dumpall > outfile
pg_dumpall -h 127.0.0.1 -Upostgres -f /home/postgres/dumpall.sql

导入备份:
psql -f infile postgres
psql -h 127.0.0.1 -Upostgres -f /home/postgres/dumpall.sql

可以使用选项 --globals-only 备份部分实例级别的数据。
导出角色和表空间创建脚本
pg_dumpall -h 127.0.0.1 -Upostgres -f /home/postgres/globals.sql --globals-only

导出角色创建脚本
pg_dumpall -h 127.0.0.1 -Upostgres -f /home/postgres/roles.sql --roles-only

执行导入:
psql -h 127.0.0.1 -Upostgres -f /home/postgres/globals.sql
psql -h 127.0.0.1 -Upostgres -f /home/postgres/roles.sql

数据量或数据库较大的时候,还是使用 pg_dump 备份还原快一些。同时使用 pg_dumpall 备份角色和表空间脚本。数据量大时使用管道进一步压缩文件
转储并压缩:pg_dump dbname | gzip > filename.gz
解压并导入:gunzip -c filename.gz | psql dbname
指定块大小:pg_dump dbname | split -b 1m - filename

并行导出:pg_dump -j num -F d -f out.dir dbname
并行导入:pg_restore -j -d dbname filename

并行job参数 "-j" 指定并行度,并行只支持目录格式(format = d),可指定一个不存在的目录。
示例:
pg_dump -h 127.0.0.1 -Upostgres -j 2 -F d -E UTF8 -f /home/postgres/demodb demodb
pg_restore -h 127.0.0.1 -Upostgres -j 2 -d demodb /home/postgres/demodb/toc.dat


备份与恢复 :http://www.jb51.cc.com/manual/postgresql/backup.html
Backup and Restore :https://www.postgresql.org/docs/10/static/backup.html

相关文章

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