当前位置:首页 > postgresql 备份/还原数据库的方法

postgresql 备份/还原数据库的方法

点击次数:1413  更新日期:2021-12-08

需要在postgres安装目录的bin目录下执行

备份命令:

pg_dump -U postgres -d postgres -h 127.0.0.1 -p 5432 -f D:\dump.sql -s


-U 用户名  

-d 数据库名称 

-h 服务器ip 

-p 端口 -f 文件名 

-s 只保存数据结构,需要保存数据则不加该参数

-F 保存格式 


还原命令:

psql -U postgres -d postgres -h 127.0.0.1 -p 5432 -f D:\dump.sql


如果保存格式为 -F c,需要用下面命令还原

pg_restore -U postgres --dbname=postgres --jobs=4 --verbose D:\dump.dmp



参数说明:

一般选项:

  -f, --file=FILENAME         output file or directory name

  -F, --format=c|d|t|p        output file format (custom, directory, tar, plain text)

  -v, --verbose            详细模式

  -Z, --compress=0-9       被压缩格式的压缩级别

--lock-wait-timeout=TIMEOUT 在等待表锁超时后操作失败

  --help                       显示此帮助信息, 然后退出

  --versoin                    输出版本信息, 然后退出

控制输出内容选项:

  -a, --data-only          只转储数据,不包括模式

  -b, --blobs              在转储中包括大对象

  -c, --clean              在重新创建之前,先清除(删除)数据库对象

  -C, --create             在转储中包括命令,以便创建数据库

  -E, --encoding=ENCODING     转储以ENCODING形式编码的数据

  -n, --schema=SCHEMA      只转储指定名称的模式

 -N, --exclude-schema=SCHEMA     不转储已命名的模式

  -o, --oids               在转储中包括 OID

  -O, --no-owner           在明文格式中, 忽略恢复对象所属者

  -s, --schema-only        只转储模式, 不包括数据

  -S, --superuser=NAME     在转储中, 指定的超级用户名

  -t, --table=TABLE        只转储指定名称的表

  -T, --exclude-table=TABLE       只转储指定名称的表

  -x, --no-privileges      不要转储权限 (grant/revoke)

  --binary-upgrade         只能由升级工具使用

  --column-inserts          以带有列名的INSERT命令形式转储数据

  --disable-dollar-quoting     取消美元 (符号) 引号, 使用 SQL 标准引号

  --disable-triggers         在只恢复数据的过程中禁用触发器

  --inserts                 以INSERT命令,而不是COPY命令的形式转储数据

  --no-security-labels        do not dump security label assignments

  --no-tablespaces           不转储表空间分配信息

  --no-unlogged-table-data    do not dump unlogged table data

  --quote-all-identifiers     quote all identifiers, even if not key words

  --serializable-deferrable   wait until the dump can run without anomalies

 --use-set-session-authorization

   使用 SESSION AUTHORIZATION 命令代替ALTER OWNER 命令来设置所有权

联接选项:

  -h, --host=主机名        数据库服务器的主机名或套接字目录

  -p, --port=端口号        数据库服务器的端口号

  -U, --username=名字      以指定的数据库用户联接

  -w, --no-password        永远不提示输入口令

  -W, --password           强制口令提示 (自动)

  --role=ROLENAME          do SET ROLE before dump