Toggle navigation
首页
技术
骑行
羽毛球
资讯
联络我
登录
postgres数据库的备份与恢复
2021-02-28
postgres
db
> 本文介绍数据库postgres的备份与恢复 ## 数据库备份 postgres数据库有2个指令: 1. pg_dump 2. pg_dumpall pg_dump用于备份单个数据库,可以备份为可读的纯文本(默认),也可以备份为自定义格式(-Fc/-Fd);pg_dumpall通过pg_dump命令,实现所有数据库的备份,并且额外包含数据库角色和表空间。 一个文本备份内容大致如下: ```sql -- -- PostgreSQL database dump -- -- Dumped from database version 13.1 -- Dumped by pg_dump version 13.0 (Debian 13.0-1.pgdg100+1) SET statement_timeout = 0; SET lock_timeout = 0; SET idle_in_transaction_session_timeout = 0; SET client_encoding = 'UTF8'; SET standard_conforming_strings = on; SELECT pg_catalog.set_config('search_path', '', false); SET check_function_bodies = false; SET xmloption = content; SET client_min_messages = warning; SET row_security = off; -- -- Name: public; Type: SCHEMA; Schema: -; Owner: postgres -- CREATE SCHEMA public; ALTER SCHEMA public OWNER TO postgres; -- -- Name: SCHEMA public; Type: COMMENT; Schema: -; Owner: postgres -- COMMENT ON SCHEMA public IS 'standard public schema'; SET default_tablespace = ''; SET default_table_access_method = heap; -- -- Name: xxxx; Type: TABLE; Schema: public; Owner: postgres -- CREATE TABLE public.xxxx ( ) COPY public.xxxx (id, xxxx) FROM stdin; 9a1b950a-aee2-4660-8ad4-712a1388160b \N d38e6300-7ce1-49db-bc30-0bf4a50bfbdd \N \. …… ``` 以文本格式备份数据库时,需考虑将备份文件压缩: pg_dump --username=xxx -Z6 -f dbname.sql.gz dbname 以自定义格式备份数据库,默认会压缩: pg_dump -Fc --username=postgres -f dbname.dump -d dbname 备份所有数据库: pg_dumpall > db.out | gzip > db.sql.gz ## 数据库恢复 对于文本格式的备份,可以用psql命令恢复数据(如果db已经存在并有数据,需要先将db删除重建) psql -d dbname -f xxx.sql 对于压缩的文件,可以用管道 zcat xxx.sql.gz | psql -d dbname -W 对于自定义备份文件,用pg_restore恢复数据库( --clean --create命令用于先删除db再创建) pg_restore --username=postgres -d dbname --clean --create .\dbname.dump ## 参考 * [pg_dump](https://www.postgresql.org/docs/current/app-pgdump.html) * [pg_dumpall](https://www.postgresql.org/docs/current/app-pg-dumpall.html) * [psql](https://www.postgresql.org/docs/current/app-psql.html) * [pg_restore](https://www.postgresql.org/docs/current/app-pgrestore.html)
×
本文为博主原创,如需转载,请注明出处:
http://www.supperxin.com
返回博客列表