• postgresql逻辑备份工具pg_dump和pg_resotre学习


    (一)pg_dump备份

    pg提供了pg_dump和pg_dumpall命令进行数据库的备份,pg_dumpall是将整个pg集群转储到一个脚本文件中,而pg_dump命令可以选择一个数据库或者部分表进行备份。

    pg_dump 把一个数据库转储为纯文本文件或者是其它格式,使用方法如下:

    1. [postgres@pg01 ~]$ pg_dump --help
    2. 用法:
    3. pg_dump [选项]... [数据库名字]
    4. **一般选项**:
    5. -f, --file=FILENAME 输出文件或目录名
    6. -F, --format=c|d|t|p 输出文件格式 (c=custom, d=directory, t=tar,p=plain,plain就是sql纯文本 (默认值))
    7. -j, --jobs=NUM 执行多个并行任务进行备份转储工作
    8. -v, --verbose 详细模式
    9. -V, --version 输出版本信息,然后退出
    10. -Z, --compress=0-9 被压缩格式的压缩级别,0表示不压缩
    11. --lock-wait-timeout=TIMEOUT 在等待表锁超时后操作失败
    12. --no-sync 不用等待变化安全写入磁盘
    13. -?, --help 显示此帮助, 然后退出
    14. **控制输出内容选项(常用)**:
    15. -a, --data-only 只转储数据,不包括模式,只对纯文本输出有意义
    16. -s, --schema-only 只转储模式, 不包括数据
    17. -c, --clean 在重新创建之前,先清除(删除)数据库对象,如drop table。只对纯文本输出有意义
    18. -C, --create 指定输出文件中是否生成create database语句,只对纯文本输出有意义
    19. -n, --schema=PATTERN 指定要导出的schema,不指定则导出所有的非系统schema
    20. -N, --exclude-schema=PATTERN 排除导出哪些schema
    21. -O, --no-owner 在明文格式中, 忽略恢复对象所属者
    22. -t, --table=PATTERN 指定导出的表、视图、序列,可以使用多个-t匹配多个表,使用-t之后,-n和-N就失效了
    23. -T, --exclude-table=PATTERN 排除表
    24. -x, --no-privileges 不要转储权限 (grant/revoke)
    25. --disable-triggers 在只恢复数据的过程中禁用触发器
    26. --exclude-table-data=PATTERN do NOT dump data for the specified table(s)
    27. --if-exists 当删除对象时使用IF EXISTS
    28. --inserts 以INSERT命令,而不是COPY命令的形式转储数据,使用该选项可以把数据加载到非pg数据库,会使恢复非常慢
    29. 该选项为每行生成1个单独的insert命令,?在恢复过程中遇到错误,将会丢失1行而不是全部表数据
    30. --column-inserts 以带有列名的INSERT命令形式转储数据,例如insert into table_name(column,...) values(value1,...)
    31. --load-via-partition-root 通过根表加载分区
    32. --no-comments 不转储注释
    33. --no-tablespaces 不转储表空间分配信息
    34. --no-unlogged-table-data 不转储没有日志的表数据
    35. --on-conflict-do-nothing 将ON CONFLICT DO NOTHING添加到INSERT命令
    36. **控制输出内容选项(不常用)**:
    37. -S, --superuser=NAME 指定关闭触发器时需要用到的超级用户名。 它只有在使用了--disable-triggers时才有影响。一般情况下,最好不要输入该参数,而是用 超级用户启动生成的脚本。
    38. -b, --blobs 在转储中包括大对象
    39. -B, --no-blobs 排除转储中的大型对象
    40. -E, --encoding=ENCODING 转储以ENCODING形式编码的数据
    41. --binary-upgrade 只能由升级工具使用
    42. --enable-row-security 启用行安全性(只转储用户能够访问的内容)
    43. --extra-float-digits=NUM 覆盖extra_float_digits的默认设置
    44. --disable-dollar-quoting 取消美元 (符号) 引号, 使用 SQL 标准引号
    45. --no-publications 不转储发布
    46. --no-security-labels 不转储安全标签的分配
    47. --no-subscriptions 不转储订阅
    48. --no-synchronized-snapshots 在并行工作集中不使用同步快照
    49. --quote-all-identifiers 所有标识符加引号,即使不是关键字
    50. --rows-per-insert=NROWS 每个插入的行数;意味着--inserts
    51. --section=SECTION 备份命名的节 (数据前, 数据, 及 数据后)
    52. --serializable-deferrable 等到备份可以无异常运行
    53. --snapshot=SNAPSHOT 为转储使用给定的快照
    54. --strict-names 要求每个表和(或)schema包括模式以匹配至少一个实体
    55. --use-set-session-authorization
    56. 使用 SESSION AUTHORIZATION 命令代替
    57. ALTER OWNER 命令来设置所有权
    58. **联接选项**:
    59. -d, --dbname=DBNAME 对数据库 DBNAME备份
    60. -h, --host=主机名 数据库服务器的主机名或套接字目录
    61. -p, --port=端口号 数据库服务器的端口号
    62. -U, --username=名字 以指定的数据库用户联接
    63. -w, --no-password 永远不提示输入口令
    64. -W, --password 强制口令提示 (自动)
    65. --role=ROLENAME 在转储前运行SET ROLE

    (二)pg_restore恢复

    对于pg_dump的自定义备份custom和tar类型的备份,需要使用pg_restore进行恢复,pg_restore语法如下:

    1. [postgres@pg01 pg_backup]$ pg_restore --help
    2. pg_restore 从一个归档中恢复一个由 pg_dump 创建的 PostgreSQL 数据库.
    3. 用法:
    4. pg_restore [选项]... [文件名]
    5. 一般选项:
    6. -d, --dbname=名字 连接数据库名字
    7. -f, --file=文件名 输出文件名(- 对于stdout)
    8. -F, --format=c|d|t 备份文件格式(应该自动进行)
    9. -l, --list 打印归档文件的 TOC 概述
    10. -v, --verbose 详细模式
    11. -V, --version 输出版本信息, 然后退出
    12. -?, --help 显示此帮助, 然后退出
    13. 恢复控制选项:
    14. -a, --data-only 只恢复数据, 不包括模式
    15. -c, --clean 在重新创建之前,先清除(删除)数据库对象
    16. -C, --create 创建目标数据库
    17. -e, --exit-on-error 发生错误退出, 默认为继续
    18. -I, --index=NAME 恢复指定名称的索引
    19. -j, --jobs=NUM 执行多个并行任务进行恢复工作
    20. -L, --use-list=FILENAME 从这个文件中使用指定的内容表排序
    21. 输出
    22. -n, --schema=NAME 在这个模式中只恢复对象
    23. -N, --exclude-schema=NAME 不恢复此模式中的对象
    24. -O, --no-owner 不恢复对象所属者
    25. -P, --function=NAME(args) 恢复指定名字的函数
    26. -s, --schema-only 只恢复模式, 不包括数据
    27. -S, --superuser=NAME 使用指定的超级用户来禁用触发器
    28. -t, --table=NAME 恢复命名关系(表、视图等)
    29. -T, --trigger=NAME 恢复指定名字的触发器
    30. -x, --no-privileges 跳过处理权限的恢复 (grant/revoke)
    31. -1, --single-transaction 作为单个事务恢复
    32. --disable-triggers 在只恢复数据的过程中禁用触发器
    33. --enable-row-security 启用行安全性
    34. --if-exists 当删除对象时使用IF EXISTS
    35. --no-comments 不恢复注释
    36. --no-data-for-failed-tables 对那些无法创建的表不进行
    37. 数据恢复
    38. --no-publications 不恢复发行
    39. --no-security-labels 不恢复安全标签信息
    40. --no-subscriptions 不恢复订阅
    41. --no-tablespaces 不恢复表空间的分配信息
    42. --section=SECTION 恢复命名节 (数据前、数据及数据后)
    43. --strict-names 要求每个表和(或)schema包括模式以匹配至少一个实体
    44. --use-set-session-authorization
    45. 使用 SESSION AUTHORIZATION 命令代替
    46. ALTER OWNER 命令来设置所有权
    47. 联接选项:
    48. -h, --host=主机名 数据库服务器的主机名或套接字目录
    49. -p, --port=端口号 数据库服务器的端口号
    50. -U, --username=名字 以指定的数据库用户联接
    51. -w, --no-password 永远不提示输入口令
    52. -W, --password 强制口令提示 (自动)
    53. --role=ROLENAME 在恢复前执行SET ROLE操作
    54. 选项 -I, -n, -N, -P, -t, -T, 以及 --section 可以组合使用和指定
    55. 多次用于选择多个对象.
    56. 如果没有提供输入文件名, 则使用标准输入.

    (三)使用pg_dump备份的例子

    (3.1)对db1数据库进行备份,保存为转储dmp格式

    1. [postgres@pg01 pg_backup]$ pg_dump --username=lijiaman --password --host=192.168.10.11 --port=5432 --dbname=db1 --format=custom --file=/home/postgres/pg_backup/db1.dump --verbose
    2. 口令:
    3. pg_dump: 最后的内置 OID 是 16383
    4. pg_dump: 读扩展
    5. pg_dump: 识别扩展成员
    6. pg_dump: 读取模式
    7. pg_dump: 读取用户定义表
    8. pg_dump: 读取用户定义函数
    9. pg_dump: 读取用户定义类型
    10. pg_dump: 读取过程语言
    11. pg_dump: 读取用户定义聚集函数
    12. pg_dump: 读取用户定义操作符
    13. pg_dump: 读取用户定义的访问方法
    14. pg_dump: 读取用户定义操作符集
    15. pg_dump: 读取用户定义操作符
    16. pg_dump: 读取用户定义的文本搜索解析器
    17. pg_dump: 读取用户定义的文本搜索模板
    18. pg_dump: 读取用户定义的文本搜索字典
    19. pg_dump: 读取用户定义的文本搜索配置
    20. pg_dump: 读取用户定义外部数据封装器
    21. pg_dump: 读取用户定义的外部服务器
    22. pg_dump: 正在读取缺省权限
    23. pg_dump: 读取用户定义的校对函数
    24. pg_dump: 读取用户定义的字符集转换
    25. pg_dump: 读取类型转换
    26. pg_dump: 读取转换
    27. pg_dump: 读取表继承信息
    28. pg_dump: 读取事件触发器
    29. pg_dump: 查找扩展表
    30. pg_dump: 正在查找关系继承
    31. pg_dump: 正在读取感兴趣表的列信息
    32. pg_dump: 正在查找表"public.t1"的列和类型
    33. pg_dump: 正在查找表"public.v1"的列和类型
    34. pg_dump: 正在查找表"public.t2"的列和类型
    35. pg_dump: 正在查找表"public.t2"的默认表达式
    36. pg_dump: 正在查找表"schema1.t1"的列和类型
    37. pg_dump: 正在查找表"schema1.t2"的列和类型
    38. pg_dump: 正在查找表"schema1.v_t2"的列和类型
    39. pg_dump: 在子表里标记继承字段
    40. pg_dump: 读取索引
    41. pg_dump: 为表"public.t1"读取索引
    42. pg_dump: 为表"public.t2"读取索引
    43. pg_dump: 为表"schema1.t1"读取索引
    44. pg_dump: 为表"schema1.t2"读取索引
    45. pg_dump: 在分区表中标记索引
    46. pg_dump: 读取扩展统计信息
    47. pg_dump: 读取约束
    48. pg_dump: 读取触发器
    49. pg_dump: 读取重写规则
    50. pg_dump: 读取策略
    51. pg_dump: 为表"public.t1"读取行安全性启用状态
    52. pg_dump: 为表"public.t1"读取策略
    53. pg_dump: 为表"public.v1"读取行安全性启用状态
    54. pg_dump: 为表"public.v1"读取策略
    55. pg_dump: 为表"public.seq1"读取行安全性启用状态
    56. pg_dump: 为表"public.seq1"读取策略
    57. pg_dump: 为表"public.t2_id_seq"读取行安全性启用状态
    58. pg_dump: 为表"public.t2_id_seq"读取策略
    59. pg_dump: 为表"public.t2"读取行安全性启用状态
    60. pg_dump: 为表"public.t2"读取策略
    61. pg_dump: 为表"schema1.t1"读取行安全性启用状态
    62. pg_dump: 为表"schema1.t1"读取策略
    63. pg_dump: 为表"schema1.t2"读取行安全性启用状态
    64. pg_dump: 为表"schema1.t2"读取策略
    65. pg_dump: 为表"schema1.v_t2"读取行安全性启用状态
    66. pg_dump: 为表"schema1.v_t2"读取策略
    67. pg_dump: 为表"schema1.seq1"读取行安全性启用状态
    68. pg_dump: 为表"schema1.seq1"读取策略
    69. pg_dump: 读取发布
    70. pg_dump: 读取发布成员资格
    71. pg_dump: 为表"public.t1"读取发行会员资格
    72. pg_dump: 为表"public.t2"读取发行会员资格
    73. pg_dump: 为表"schema1.t1"读取发行会员资格
    74. pg_dump: 为表"schema1.t2"读取发行会员资格
    75. pg_dump: 读取订阅
    76. pg_dump: 正在读取大对象
    77. pg_dump: 读取从属数据
    78. pg_dump: 正在保存encoding = UTF8
    79. pg_dump: 正在保存standard_conforming_strings = on
    80. pg_dump: 正在保存search_path =
    81. pg_dump: 保存数据库定义
    82. pg_dump: 正在转储表"public.t1"的内容
    83. pg_dump: 正在转储表"public.t2"的内容
    84. pg_dump: 正在转储表"schema1.t1"的内容
    85. pg_dump: 正在转储表"schema1.t2"的内容

    (3.2)对db1数据库进行备份,保存为sql格式

    1. [postgres@pg01 pg_backup]$ pg_dump --username=lijiaman --password --host=192.168.10.11 --port=5432 --dbname=db1 --format=plain --file=/home/postgres/pg_backup/db1.sql --verbose
    2. 口令:
    3. pg_dump: 最后的内置 OID 是 16383
    4. pg_dump: 读扩展
    5. pg_dump: 识别扩展成员
    6. pg_dump: 读取模式
    7. pg_dump: 读取用户定义表
    8. pg_dump: 读取用户定义函数
    9. pg_dump: 读取用户定义类型
    10. pg_dump: 读取过程语言
    11. pg_dump: 读取用户定义聚集函数
    12. pg_dump: 读取用户定义操作符
    13. pg_dump: 读取用户定义的访问方法
    14. pg_dump: 读取用户定义操作符集
    15. pg_dump: 读取用户定义操作符
    16. pg_dump: 读取用户定义的文本搜索解析器
    17. pg_dump: 读取用户定义的文本搜索模板
    18. pg_dump: 读取用户定义的文本搜索字典
    19. pg_dump: 读取用户定义的文本搜索配置
    20. pg_dump: 读取用户定义外部数据封装器
    21. pg_dump: 读取用户定义的外部服务器
    22. pg_dump: 正在读取缺省权限
    23. pg_dump: 读取用户定义的校对函数
    24. pg_dump: 读取用户定义的字符集转换
    25. pg_dump: 读取类型转换
    26. pg_dump: 读取转换
    27. pg_dump: 读取表继承信息
    28. pg_dump: 读取事件触发器
    29. pg_dump: 查找扩展表
    30. pg_dump: 正在查找关系继承
    31. pg_dump: 正在读取感兴趣表的列信息
    32. pg_dump: 正在查找表"public.t1"的列和类型
    33. pg_dump: 正在查找表"public.v1"的列和类型
    34. pg_dump: 正在查找表"public.t2"的列和类型
    35. pg_dump: 正在查找表"public.t2"的默认表达式
    36. pg_dump: 正在查找表"schema1.t1"的列和类型
    37. pg_dump: 正在查找表"schema1.t2"的列和类型
    38. pg_dump: 正在查找表"schema1.v_t2"的列和类型
    39. pg_dump: 在子表里标记继承字段
    40. pg_dump: 读取索引
    41. pg_dump: 为表"public.t1"读取索引
    42. pg_dump: 为表"public.t2"读取索引
    43. pg_dump: 为表"schema1.t1"读取索引
    44. pg_dump: 为表"schema1.t2"读取索引
    45. pg_dump: 在分区表中标记索引
    46. pg_dump: 读取扩展统计信息
    47. pg_dump: 读取约束
    48. pg_dump: 读取触发器
    49. pg_dump: 读取重写规则
    50. pg_dump: 读取策略
    51. pg_dump: 为表"public.t1"读取行安全性启用状态
    52. pg_dump: 为表"public.t1"读取策略
    53. pg_dump: 为表"public.v1"读取行安全性启用状态
    54. pg_dump: 为表"public.v1"读取策略
    55. pg_dump: 为表"public.seq1"读取行安全性启用状态
    56. pg_dump: 为表"public.seq1"读取策略
    57. pg_dump: 为表"public.t2_id_seq"读取行安全性启用状态
    58. pg_dump: 为表"public.t2_id_seq"读取策略
    59. pg_dump: 为表"public.t2"读取行安全性启用状态
    60. pg_dump: 为表"public.t2"读取策略
    61. pg_dump: 为表"schema1.t1"读取行安全性启用状态
    62. pg_dump: 为表"schema1.t1"读取策略
    63. pg_dump: 为表"schema1.t2"读取行安全性启用状态
    64. pg_dump: 为表"schema1.t2"读取策略
    65. pg_dump: 为表"schema1.v_t2"读取行安全性启用状态
    66. pg_dump: 为表"schema1.v_t2"读取策略
    67. pg_dump: 为表"schema1.seq1"读取行安全性启用状态
    68. pg_dump: 为表"schema1.seq1"读取策略
    69. pg_dump: 读取发布
    70. pg_dump: 读取发布成员资格
    71. pg_dump: 为表"public.t1"读取发行会员资格
    72. pg_dump: 为表"public.t2"读取发行会员资格
    73. pg_dump: 为表"schema1.t1"读取发行会员资格
    74. pg_dump: 为表"schema1.t2"读取发行会员资格
    75. pg_dump: 读取订阅
    76. pg_dump: 正在读取大对象
    77. pg_dump: 读取从属数据
    78. pg_dump: 正在保存encoding = UTF8
    79. pg_dump: 正在保存standard_conforming_strings = on
    80. pg_dump: 正在保存search_path =
    81. pg_dump: 创建SCHEMA "schema1"
    82. pg_dump: 创建SCHEMA "schema2"
    83. pg_dump: 创建TYPE "public.mood"
    84. pg_dump: 创建FUNCTION "public.variadic_example(numeric[])"
    85. pg_dump: 创建SEQUENCE "public.seq1"
    86. pg_dump: 创建TABLE "public.t1"
    87. pg_dump: 创建TABLE "public.t2"
    88. pg_dump: 创建SEQUENCE "public.t2_id_seq"
    89. pg_dump: 创建SEQUENCE OWNED BY "public.t2_id_seq"
    90. pg_dump: 创建VIEW "public.v1"
    91. pg_dump: 创建SEQUENCE "schema1.seq1"
    92. pg_dump: 创建TABLE "schema1.t1"
    93. pg_dump: 创建TABLE "schema1.t2"
    94. pg_dump: 创建VIEW "schema1.v_t2"
    95. pg_dump: 创建DEFAULT "public.t2 id"
    96. pg_dump: 为表"public.t1"处理数据
    97. pg_dump: 正在转储表"public.t1"的内容
    98. pg_dump: 为表"public.t2"处理数据
    99. pg_dump: 正在转储表"public.t2"的内容
    100. pg_dump: 为表"schema1.t1"处理数据
    101. pg_dump: 正在转储表"schema1.t1"的内容
    102. pg_dump: 为表"schema1.t2"处理数据
    103. pg_dump: 正在转储表"schema1.t2"的内容
    104. pg_dump: 执行 SEQUENCE SET seq1
    105. pg_dump: 执行 SEQUENCE SET t2_id_seq
    106. pg_dump: 执行 SEQUENCE SET seq1
    107. pg_dump: 创建CONSTRAINT "public.t2 t2_pkey"
    108. pg_dump: 创建CONSTRAINT "schema1.t1 t1_pkey"
    109. pg_dump: 创建INDEX "public.idx_id"
    110. pg_dump: 创建INDEX "schema1.idx_age"
    111. pg_dump: 创建ACL "public.TABLE t1"

    查看其备份结果:

    1. [postgres@pg01 pg_backup]$ cat db1.sql
    2. --
    3. -- PostgreSQL database dump
    4. --
    5. -- Dumped from database version 12.5
    6. -- Dumped by pg_dump version 12.5
    7. -- Started on 2022-08-05 04:42:07 CST
    8. SET statement_timeout = 0;
    9. SET lock_timeout = 0;
    10. SET idle_in_transaction_session_timeout = 0;
    11. SET client_encoding = 'UTF8';
    12. SET standard_conforming_strings = on;
    13. SELECT pg_catalog.set_config('search_path', '', false);
    14. SET check_function_bodies = false;
    15. SET xmloption = content;
    16. SET client_min_messages = warning;
    17. SET row_security = off;
    18. --
    19. -- TOC entry 9 (class 2615 OID 16405)
    20. -- Name: schema1; Type: SCHEMA; Schema: -; Owner: postgres
    21. --
    22. CREATE SCHEMA schema1;
    23. ALTER SCHEMA schema1 OWNER TO postgres;
    24. --
    25. -- TOC entry 6 (class 2615 OID 16406)
    26. -- Name: schema2; Type: SCHEMA; Schema: -; Owner: postgres
    27. --
    28. CREATE SCHEMA schema2;
    29. ALTER SCHEMA schema2 OWNER TO postgres;
    30. --
    31. -- TOC entry 639 (class 1247 OID 16397)
    32. -- Name: mood; Type: TYPE; Schema: public; Owner: postgres
    33. --
    34. CREATE TYPE public.mood AS ENUM (
    35. 'sad',
    36. 'ok',
    37. 'happy'
    38. );
    39. ALTER TYPE public.mood OWNER TO postgres;
    40. --
    41. -- TOC entry 213 (class 1255 OID 16451)
    42. -- Name: variadic_example(numeric[]); Type: FUNCTION; Schema: public; Owner: postgres
    43. --
    44. CREATE FUNCTION public.variadic_example(VARIADIC numeric[]) RETURNS integer
    45. LANGUAGE sql
    46. AS $$SELECT 1$$;
    47. ALTER FUNCTION public.variadic_example(VARIADIC numeric[]) OWNER TO postgres;
    48. --
    49. -- TOC entry 206 (class 1259 OID 16392)
    50. -- Name: seq1; Type: SEQUENCE; Schema: public; Owner: postgres
    51. --
    52. CREATE SEQUENCE public.seq1
    53. AS integer
    54. START WITH 5
    55. INCREMENT BY 1
    56. NO MINVALUE
    57. MAXVALUE 10
    58. CACHE 2;
    59. ALTER TABLE public.seq1 OWNER TO postgres;
    60. SET default_tablespace = '';
    61. SET default_table_access_method = heap;
    62. --
    63. -- TOC entry 204 (class 1259 OID 16385)
    64. -- Name: t1; Type: TABLE; Schema: public; Owner: postgres
    65. --
    66. CREATE TABLE public.t1 (
    67. id integer,
    68. name character varying(50)
    69. );
    70. ALTER TABLE public.t1 OWNER TO postgres;
    71. --
    72. -- TOC entry 208 (class 1259 OID 16409)
    73. -- Name: t2; Type: TABLE; Schema: public; Owner: postgres
    74. --
    75. CREATE TABLE public.t2 (
    76. id integer NOT NULL,
    77. name character varying(20)
    78. );
    79. ALTER TABLE public.t2 OWNER TO postgres;
    80. --
    81. -- TOC entry 207 (class 1259 OID 16407)
    82. -- Name: t2_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
    83. --
    84. CREATE SEQUENCE public.t2_id_seq
    85. AS integer
    86. START WITH 1
    87. INCREMENT BY 1
    88. NO MINVALUE
    89. NO MAXVALUE
    90. CACHE 1;
    91. ALTER TABLE public.t2_id_seq OWNER TO postgres;
    92. --
    93. -- TOC entry 3734 (class 0 OID 0)
    94. -- Dependencies: 207
    95. -- Name: t2_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
    96. --
    97. ALTER SEQUENCE public.t2_id_seq OWNED BY public.t2.id;
    98. --
    99. -- TOC entry 205 (class 1259 OID 16388)
    100. -- Name: v1; Type: VIEW; Schema: public; Owner: postgres
    101. --
    102. CREATE VIEW public.v1 AS
    103. SELECT t1.id,
    104. t1.name
    105. FROM public.t1;
    106. ALTER TABLE public.v1 OWNER TO postgres;
    107. --
    108. -- TOC entry 212 (class 1259 OID 16452)
    109. -- Name: seq1; Type: SEQUENCE; Schema: schema1; Owner: postgres
    110. --
    111. CREATE SEQUENCE schema1.seq1
    112. START WITH 1
    113. INCREMENT BY 1
    114. NO MINVALUE
    115. NO MAXVALUE
    116. CACHE 1;
    117. ALTER TABLE schema1.seq1 OWNER TO postgres;
    118. --
    119. -- TOC entry 209 (class 1259 OID 16438)
    120. -- Name: t1; Type: TABLE; Schema: schema1; Owner: lijiaman
    121. --
    122. CREATE TABLE schema1.t1 (
    123. id integer NOT NULL,
    124. name character varying(50)
    125. );
    126. ALTER TABLE schema1.t1 OWNER TO lijiaman;
    127. --
    128. -- TOC entry 210 (class 1259 OID 16441)
    129. -- Name: t2; Type: TABLE; Schema: schema1; Owner: postgres
    130. --
    131. CREATE TABLE schema1.t2 (
    132. id integer,
    133. age integer,
    134. address character varying(100)
    135. );
    136. ALTER TABLE schema1.t2 OWNER TO postgres;
    137. --
    138. -- TOC entry 211 (class 1259 OID 16444)
    139. -- Name: v_t2; Type: VIEW; Schema: schema1; Owner: postgres
    140. --
    141. CREATE VIEW schema1.v_t2 AS
    142. SELECT t2.id,
    143. t2.age,
    144. t2.address
    145. FROM schema1.t2
    146. WHERE (t2.id > 1);
    147. ALTER TABLE schema1.v_t2 OWNER TO postgres;
    148. --
    149. -- TOC entry 3586 (class 2604 OID 16412)
    150. -- Name: t2 id; Type: DEFAULT; Schema: public; Owner: postgres
    151. --
    152. ALTER TABLE ONLY public.t2 ALTER COLUMN id SET DEFAULT nextval('public.t2_id_seq'::regclass);
    153. --
    154. -- TOC entry 3721 (class 0 OID 16385)
    155. -- Dependencies: 204
    156. -- Data for Name: t1; Type: TABLE DATA; Schema: public; Owner: postgres
    157. --
    158. COPY public.t1 (id, name) FROM stdin;
    159. 1 a
    160. \.
    161. --
    162. -- TOC entry 3724 (class 0 OID 16409)
    163. -- Dependencies: 208
    164. -- Data for Name: t2; Type: TABLE DATA; Schema: public; Owner: postgres
    165. --
    166. COPY public.t2 (id, name) FROM stdin;
    167. 1 a
    168. 2 b
    169. \.
    170. --
    171. -- TOC entry 3725 (class 0 OID 16438)
    172. -- Dependencies: 209
    173. -- Data for Name: t1; Type: TABLE DATA; Schema: schema1; Owner: lijiaman
    174. --
    175. COPY schema1.t1 (id, name) FROM stdin;
    176. 1 a
    177. 2 b
    178. \.
    179. --
    180. -- TOC entry 3726 (class 0 OID 16441)
    181. -- Dependencies: 210
    182. -- Data for Name: t2; Type: TABLE DATA; Schema: schema1; Owner: postgres
    183. --
    184. COPY schema1.t2 (id, age, address) FROM stdin;
    185. 1 11 beijing
    186. 2 12 shenzheng
    187. \.
    188. --
    189. -- TOC entry 3735 (class 0 OID 0)
    190. -- Dependencies: 206
    191. -- Name: seq1; Type: SEQUENCE SET; Schema: public; Owner: postgres
    192. --
    193. SELECT pg_catalog.setval('public.seq1', 5, false);
    194. --
    195. -- TOC entry 3736 (class 0 OID 0)
    196. -- Dependencies: 207
    197. -- Name: t2_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres
    198. --
    199. SELECT pg_catalog.setval('public.t2_id_seq', 2, true);
    200. --
    201. -- TOC entry 3737 (class 0 OID 0)
    202. -- Dependencies: 212
    203. -- Name: seq1; Type: SEQUENCE SET; Schema: schema1; Owner: postgres
    204. --
    205. SELECT pg_catalog.setval('schema1.seq1', 1, false);
    206. --
    207. -- TOC entry 3589 (class 2606 OID 16414)
    208. -- Name: t2 t2_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
    209. --
    210. ALTER TABLE ONLY public.t2
    211. ADD CONSTRAINT t2_pkey PRIMARY KEY (id);
    212. --
    213. -- TOC entry 3591 (class 2606 OID 16449)
    214. -- Name: t1 t1_pkey; Type: CONSTRAINT; Schema: schema1; Owner: lijiaman
    215. --
    216. ALTER TABLE ONLY schema1.t1
    217. ADD CONSTRAINT t1_pkey PRIMARY KEY (id);
    218. --
    219. -- TOC entry 3587 (class 1259 OID 16424)
    220. -- Name: idx_id; Type: INDEX; Schema: public; Owner: postgres
    221. --
    222. CREATE INDEX idx_id ON public.t1 USING btree (id);
    223. --
    224. -- TOC entry 3592 (class 1259 OID 16450)
    225. -- Name: idx_age; Type: INDEX; Schema: schema1; Owner: postgres
    226. --
    227. CREATE INDEX idx_age ON schema1.t2 USING btree (age);
    228. --
    229. -- TOC entry 3733 (class 0 OID 0)
    230. -- Dependencies: 204
    231. -- Name: TABLE t1; Type: ACL; Schema: public; Owner: postgres
    232. --
    233. GRANT ALL ON TABLE public.t1 TO lijiaman;
    234. -- Completed on 2022-08-05 04:42:10 CST
    235. --
    236. -- PostgreSQL database dump complete
    237. --

    (3.3)备份db1数据库为sql文件,并使用--create选项创建带有create database db1语句的文件

    1. [postgres@pg01 pg_backup]$ pg_dump --username=lijiaman --password --host=192.168.10.11 --port=5432 --dbname=db1 --format=plain --file=/home/postgres/pg_backup/db1_create.sql --create --verbose
    2. # 可以在导出的sql文件中看到创建数据库的语句
    3. CREATE DATABASE db1 WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'zh_CN.UTF-8' LC_CTYPE = 'zh_CN.UTF-8';
    4. ALTER DATABASE db1 OWNER TO postgres;

    (3.4)备份多个表

    备份db1数据库中的schema1.t1和schema.t2表为sql文件

    [postgres@pg01 pg_backup]$ pg_dump --username=lijiaman --password --host=192.168.10.11 --port=5432 --dbname=db1 --table=schema1.t1  --table=schema1.t2 --format=plain --file=/home/postgres/pg_backup/db1_schema1_t1_t2.sql --verbose

    结果如下:

    1. [postgres@pg01 pg_backup]$ cat db1_schema1_t1_t2.sql
    2. --
    3. -- PostgreSQL database dump
    4. --
    5. -- Dumped from database version 12.5
    6. -- Dumped by pg_dump version 12.5
    7. -- Started on 2022-08-05 05:03:36 CST
    8. SET statement_timeout = 0;
    9. SET lock_timeout = 0;
    10. SET idle_in_transaction_session_timeout = 0;
    11. SET client_encoding = 'UTF8';
    12. SET standard_conforming_strings = on;
    13. SELECT pg_catalog.set_config('search_path', '', false);
    14. SET check_function_bodies = false;
    15. SET xmloption = content;
    16. SET client_min_messages = warning;
    17. SET row_security = off;
    18. SET default_tablespace = '';
    19. SET default_table_access_method = heap;
    20. --
    21. -- TOC entry 209 (class 1259 OID 16438)
    22. -- Name: t1; Type: TABLE; Schema: schema1; Owner: lijiaman
    23. --
    24. CREATE TABLE schema1.t1 (
    25. id integer NOT NULL,
    26. name character varying(50)
    27. );
    28. ALTER TABLE schema1.t1 OWNER TO lijiaman;
    29. --
    30. -- TOC entry 210 (class 1259 OID 16441)
    31. -- Name: t2; Type: TABLE; Schema: schema1; Owner: postgres
    32. --
    33. CREATE TABLE schema1.t2 (
    34. id integer,
    35. age integer,
    36. address character varying(100)
    37. );
    38. ALTER TABLE schema1.t2 OWNER TO postgres;
    39. --
    40. -- TOC entry 3710 (class 0 OID 16438)
    41. -- Dependencies: 209
    42. -- Data for Name: t1; Type: TABLE DATA; Schema: schema1; Owner: lijiaman
    43. --
    44. COPY schema1.t1 (id, name) FROM stdin;
    45. 1 a
    46. 2 b
    47. \.
    48. --
    49. -- TOC entry 3711 (class 0 OID 16441)
    50. -- Dependencies: 210
    51. -- Data for Name: t2; Type: TABLE DATA; Schema: schema1; Owner: postgres
    52. --
    53. COPY schema1.t2 (id, age, address) FROM stdin;
    54. 1 11 beijing
    55. 2 12 shenzheng
    56. \.
    57. --
    58. -- TOC entry 3580 (class 2606 OID 16449)
    59. -- Name: t1 t1_pkey; Type: CONSTRAINT; Schema: schema1; Owner: lijiaman
    60. --
    61. ALTER TABLE ONLY schema1.t1
    62. ADD CONSTRAINT t1_pkey PRIMARY KEY (id);
    63. --
    64. -- TOC entry 3581 (class 1259 OID 16450)
    65. -- Name: idx_age; Type: INDEX; Schema: schema1; Owner: postgres
    66. --
    67. CREATE INDEX idx_age ON schema1.t2 USING btree (age);
    68. -- Completed on 2022-08-05 05:03:38 CST
    69. --
    70. -- PostgreSQL database dump complete
    71. --

    (3.5)备份db1数据库的表schema1.t1,并使用--clean选项创建带有drop table、drop index的sql文件

    [postgres@pg01 pg_backup]$ pg_dump --username=lijiaman --password --host=192.168.10.11 --port=5432 --dbname=db1 --table=schema1.t1 --format=plain --file=/home/postgres/pg_backup/db1_schema1_t1.sql --verbose --clean

    结果如下:

    1. [postgres@pg01 pg_backup]$ cat db1_schema1_t1.sql
    2. --
    3. -- PostgreSQL database dump
    4. --
    5. -- Dumped from database version 12.5
    6. -- Dumped by pg_dump version 12.5
    7. -- Started on 2022-08-05 05:12:33 CST
    8. SET statement_timeout = 0;
    9. SET lock_timeout = 0;
    10. SET idle_in_transaction_session_timeout = 0;
    11. SET client_encoding = 'UTF8';
    12. SET standard_conforming_strings = on;
    13. SELECT pg_catalog.set_config('search_path', '', false);
    14. SET check_function_bodies = false;
    15. SET xmloption = content;
    16. SET client_min_messages = warning;
    17. SET row_security = off;
    18. ALTER TABLE ONLY schema1.t1 DROP CONSTRAINT t1_pkey;
    19. DROP TABLE schema1.t1;
    20. SET default_tablespace = '';
    21. SET default_table_access_method = heap;
    22. --
    23. -- TOC entry 209 (class 1259 OID 16438)
    24. -- Name: t1; Type: TABLE; Schema: schema1; Owner: lijiaman
    25. --
    26. CREATE TABLE schema1.t1 (
    27. id integer NOT NULL,
    28. name character varying(50)
    29. );
    30. ALTER TABLE schema1.t1 OWNER TO lijiaman;
    31. --
    32. -- TOC entry 3709 (class 0 OID 16438)
    33. -- Dependencies: 209
    34. -- Data for Name: t1; Type: TABLE DATA; Schema: schema1; Owner: lijiaman
    35. --
    36. COPY schema1.t1 (id, name) FROM stdin;
    37. 1 a
    38. 2 b
    39. \.
    40. --
    41. -- TOC entry 3580 (class 2606 OID 16449)
    42. -- Name: t1 t1_pkey; Type: CONSTRAINT; Schema: schema1; Owner: lijiaman
    43. --
    44. ALTER TABLE ONLY schema1.t1
    45. ADD CONSTRAINT t1_pkey PRIMARY KEY (id);
    46. -- Completed on 2022-08-05 05:12:35 CST
    47. --
    48. -- PostgreSQL database dump complete
    49. --

    (四)使用pg_restore恢复的例子

    特别注意:pg_restore [选项]... [文件名],文件名直接跟在选项后面,不是--file来指定

    (4.1)恢复数据库db1

    pg_restore --username=lijiaman --password --host=192.168.10.11 --port=5432 --dbname=db1 --format=custom /home/postgres/pg_backup/db1.dump  --verbose

    (4.2)使用--clean选项导入时覆盖之前的表

    pg_restore --username=lijiaman --password --host=192.168.10.11 --port=5432 --dbname=db1 --format=custom /home/postgres/pg_backup/db1.dump  --verbose --clean

    (4.3)将之前导出的db1数据库恢复到db2

    1. -- 1.创建db2数据库
    2. db1=# create database db2;
    3. CREATE DATABASE
    4. -- 2.使用之前db1数据库的备份,执行恢复到db2
    5. pg_restore --username=lijiaman --password --host=192.168.10.11 --port=5432 --dbname=db2 --format=custom /home/postgres/pg_backup/db1.dump --verbose
    6. -- 3.查看恢复情况
    7. db1=# \c db2
    8. 您现在已经连接到数据库 "db2",用户 "postgres".
    9. db2=# \dn
    10. 架构模式列表
    11. 名称 | 拥有者
    12. ---------+----------
    13. public | postgres
    14. schema1 | postgres
    15. schema2 | postgres
    16. (3 行记录)
    17. db2=# \dt
    18. 关联列表
    19. 架构模式 | 名称 | 类型 | 拥有者
    20. ----------+------+--------+----------
    21. public | t1 | 数据表 | postgres
    22. public | t2 | 数据表 | postgres
    23. (2 行记录)
    24. db2=# set search_path to schema1
    25. db2-# ;
    26. SET
    27. db2=# \dt
    28. 关联列表
    29. 架构模式 | 名称 | 类型 | 拥有者
    30. ----------+------+--------+----------
    31. schema1 | t1 | 数据表 | lijiaman
    32. schema1 | t2 | 数据表 | postgres
    33. (2 行记录)
    34. db2=# \ds
    35. 关联列表
    36. 架构模式 | 名称 | 类型 | 拥有者
    37. ----------+------+--------+----------
    38. schema1 | seq1 | 序列数 | postgres
    39. (1 行记录)

    (4.4)使用--schema-only选项,只恢复schema

    1. -- 1.创建数据库db4
    2. db1=# create database db4;
    3. -- 2.只恢复表结构,视图等,不恢复里面的数据
    4. [postgres@pg01 pg_backup]$ pg_restore --username=lijiaman --password --host=192.168.10.11 --port=5432 --dbname=db4 --schema-only --format=custom /home/postgres/pg_backup/db1.dump --verbose
    5. -- 3.确认有表结构,无数据
    6. db1=# \c db4
    7. 您现在已经连接到数据库 "db4",用户 "lijiaman".
    8. db4=# \dt
    9. 关联列表
    10. 架构模式 | 名称 | 类型 | 拥有者
    11. ----------+------+--------+----------
    12. public | t1 | 数据表 | postgres
    13. public | t2 | 数据表 | postgres
    14. (2 行记录)
    15. db4=# set search_path to schema1
    16. db4-# ;
    17. SET
    18. db4=# select * from t1;
    19. id | name
    20. ----+------
    21. (0 行记录)

    (4.5)使用--data-only 选项,只恢复数据

    1. -- 1.在上一步基础上进行数据恢复
    2. [postgres@pg01 pg_backup]$ pg_restore --username=lijiaman --password --host=192.168.10.11 --port=5432 --dbname=db4 --data-only --format=custom /home/postgres/pg_backup/db1.dump --verbose
    3. pg_restore: 为恢复数据库与数据库联接
    4. 口令:
    5. pg_restore: 为表"public.t1"处理数据
    6. pg_restore: 为表"public.t2"处理数据
    7. pg_restore: 为表"schema1.t1"处理数据
    8. pg_restore: 为表"schema1.t2"处理数据
    9. pg_restore: 执行 SEQUENCE SET seq1
    10. pg_restore: 执行 SEQUENCE SET t2_id_seq
    11. pg_restore: 执行 SEQUENCE SET seq1
    12. [postgres@pg01 pg_backup]$
    13. -- 查看数据
    14. db4=# select * from t1;
    15. id | name
    16. ----+------
    17. 1 | a
    18. 2 | b

    (4.6)只恢复schema1.t1和schema1.t2表到db5的schema1下面

    1. -- 1.创建数据库db1和schema1
    2. postgres=# create database db5;
    3. CREATE DATABASE
    4. postgres=# \c db5
    5. 您现在已经连接到数据库 "db5",用户 "lijiaman".
    6. db5=# create schema schema1;
    7. CREATE SCHEMA
    8. -- 2.导入2个表
    9. [postgres@pg01 pg_backup]$ pg_restore --username=lijiaman --password --host=192.168.10.11 --port=5432 --dbname=db5 --schema=schema1 --table=t1 --table=t2 --format=custom /home/postgres/pg_backup/db1.dump --verbose
    10. pg_restore: 为恢复数据库与数据库联接
    11. 口令:
    12. pg_restore: 创建TABLE "schema1.t1"
    13. pg_restore: 创建TABLE "schema1.t2"
    14. pg_restore: 为表"schema1.t1"处理数据
    15. pg_restore: 为表"schema1.t2"处理数据
    16. [postgres@pg01 pg_backup]$

    (五)psql恢复sql文件

    对于pg_dump备份出来的sql文件,直接执行sql文件即可恢复

    psql --username=lijiaman --password --host=192.168.10.11 --port=5432 --dbname=db1 --file=db1.sql

    附录

    测试数据

    1. \c db1 -- 切换到db1数据库
    2. set search_path to schema1; -- 切换到schema1
    3. -- 创建表
    4. create table t1(id int,name varchar(50));
    5. insert into t1 values(1,'a');
    6. insert into t1 values(2,'b');
    7. -- 创建主键约束
    8. alter table t1 add primary key(id);
    9. create table t2(id int,age int,address varchar(100));
    10. insert into t2 values(1,11,'beijing');
    11. insert into t2 values(2,12,'shenzheng');
    12. -- 创建索引
    13. create index idx_age on t2(age);
    14. -- 创建视图
    15. create view v_t2 as select * from t2 where id > 1;
    16. -- 创建序列
    17. create sequence seq1;
    18. -- 创建函数
    19. CREATE FUNCTION public.variadic_example(VARIADIC numeric[]) RETURNS int
    20. LANGUAGE sql AS 'SELECT 1';
  • 相关阅读:
    第一百四十二回 如何使用intl插件实现国际化
    计算机网络复习笔记——运输层
    高性能日志脱敏组件:已支持 log4j2 和 logback 插件
    离线数仓 (四) --------- 用户行为数据采集模块
    初识HashMap
    可视化设计:一文读懂桑基图,从来处来,到去出去。
    AI:37-基于深度学习的安全帽检测方法研究
    MySQL-Explain详解
    【AI】行业消息精选和分析(11月21日 星期二)
    EN 12259-3喷水灭火系统干式报警阀组件—CE认证
  • 原文地址:https://blog.csdn.net/weixin_30895723/article/details/126210418