微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/postgresql/193407.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐