Skip to content

官网,

postgresql目标恢复-官网

postgresql连续归档与恢复-官网

1.概述

​ 任何系统都有崩溃的可能,数据库备份工作的重要性毋庸置疑。通过备份和恢复来保护数据,避免数据丢失,在发生灾难或人为误操作的情况下,能够进行恢复是DBA的日常最重要的工作。不仅要保证能够成功备份,还要保证备份数据能够恢复,如果能在更短的时间进行恢复更是锦上添花。利用现有资源,基于现实情况考虑,制定严谨、可靠的备份策略,应对可能出现的需要恢复的情况是每个DBA都应该掌握的基本技能

1.1pg_dump和pg_dumpall对比

备份工具:pg_dump和pg_dumpall

pg_dump可备份一个指定的database

pg_dumpall可一次性备份所有database的数据及系统全局数据

pg_dump比pg_dumpall更合适,因为前者支持精确指定要备份的表,schema和database,而后者不支持

pg_dump可以将数据备份为SQL文本文件格式,也支持备份为用户定义压缩格式或者是TAR包格式,在数据恢复时,对压缩格式和TAR包格式的备份文件可以实现并行恢复,该特性是从8.4版开始支持

pg_dumpall工具可以将当前postgresql服务实例中所有database的数据都导出为SQL文本(pg_dumpall不支持导出SQL文本以外的其他格式),也可以同时导出表间定义和角色等全局对象

2.pg_dump

pg_dump — 把PostgreSQL数据库抽取为一个脚本文件或其他归档文件。

pg_dump是用于备份一种PostgreSQL数据库的工具。即使数据库正在被并发使用,它也能创建一致的备份。pg_dump不阻塞其他用户访问数据库(读取或写入)。

pg_dump只转储单个数据库。要备份一个集簇中 对于所有数据库公共的全局对象(例如角色和表空间),应使用 pg_dumpall

2.1 pg_dump

Usage: pg_dump [OPTION]... [DBNAME] 数据库名放最后,不指定默认是系统变量PGDATABASE指定的数据库

General options:(一般选项)

  -f, --file=FILENAME          output file or directory name导出后保存的文件名

  -F, --format=c|d|t|p         output file format (custom, directory, tar,导出文件的格式

                               plain text (default))

  -j, --jobs=NUM               use this many parallel jobs to dump并行数

  -v, --verbose                verbose mode 
详细模式 

  -V, --version                output version information, then exit输出版本信息, 然后退出

  -Z, --compress=0-9           compression level for compressed formats被压缩格式的压缩级别

  --lock-wait-timeout=TIMEOUT  fail after waiting TIMEOUT for a table lock在等待表锁超时后操作失败

  -?, --help                   show this help, then exit显示此帮助信息, 然后退出



Options controlling the output content:(控制输出的选项)

  -a, --data-only              dump only the data, not the schema只导出数据,不包括模式

  -b, --blobs                  include large objects in dump在转储中包括大对象

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

  -C, --create                 include commands to create database in dump在转储中包括命令,以便创建数据库(包括建库语句,无需在导入之前先建数据库)

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

  -n, --schema=SCHEMA          dump the named schema(s) only只转储指定名称的模式

  -N, --exclude-schema=SCHEMA  do NOT dump the named schema(s)不转储已命名的模式

  -o, --oids                   include OIDs in dump在转储中包括 OID

  -O, --no-owner               skip restoration of object ownership in在明文格式中, 忽略恢复对象所属者

                               plain-text format 


  -s, --schema-only            dump only the schema, no data只转储模式, 不包括数据(不导出数据)

  -S, --superuser=NAME         superuser user name to use in plain-text format在转储中, 指定的超级用户名

  -t, --table=TABLE            dump the named table(s) only只转储指定名称的表

  -T, --exclude-table=TABLE    do NOT dump the named table(s)只转储指定名称的表

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

  --binary-upgrade             for use by upgrade utilities only只能由升级工具使用

  --column-inserts             dump data as INSERT commands with column names以带有列名的INSERT命令形式转储数据

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

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

  --exclude-table-data=TABLE   do NOT dump data for the named table(s)以INSERT命令,而不是COPY命令的形式转储数据

  --inserts                    dump data as INSERT commands, rather than COPY

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

  --no-synchronized-snapshots  do not use synchronized snapshots in parallel jobs

  --no-tablespaces             do not dump tablespace assignments不转储表空间分配信息

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

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

  --section=SECTION            dump named section (pre-data, data, or post-data)

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

  --use-set-session-authorization

                               use SET SESSION AUTHORIZATION commands instead of

                               ALTER OWNER commands to set ownership



Connection options:(控制连接的选项)

  -d, --dbname=DBNAME      database to dump 
数据库名

  -h, --host=HOSTNAME      database server host or socket directory数据库服务器的主机名或套接字目录

  -p, --port=PORT          database server port number数据库服务器的端口号

  -U, --username=NAME      connect as specified database user以指定的数据库用户联接

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


  -W, --password           force password prompt (should happen automatically)强制口令提示 (自动)


  --role=ROLENAME          do SET ROLE before dump
General options:(一般选项)

  -f, --file=FILENAME          output file or directory name导出后保存的文件名

  -F, --format=c|d|t|p         output file format (custom, directory, tar,导出文件的格式

                               plain text (default))

  -j, --jobs=NUM               use this many parallel jobs to dump并行数

  -v, --verbose                verbose mode 
详细模式 

  -V, --version                output version information, then exit输出版本信息, 然后退出

  -Z, --compress=0-9           compression level for compressed formats被压缩格式的压缩级别

  --lock-wait-timeout=TIMEOUT  fail after waiting TIMEOUT for a table lock在等待表锁超时后操作失败

  -?, --help                   show this help, then exit显示此帮助信息, 然后退出



Options controlling the output content:(控制输出的选项)

  -a, --data-only              dump only the data, not the schema只导出数据,不包括模式

  -b, --blobs                  include large objects in dump在转储中包括大对象

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

  -C, --create                 include commands to create database in dump在转储中包括命令,以便创建数据库(包括建库语句,无需在导入之前先建数据库)

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

  -n, --schema=SCHEMA          dump the named schema(s) only只转储指定名称的模式

  -N, --exclude-schema=SCHEMA  do NOT dump the named schema(s)不转储已命名的模式

  -o, --oids                   include OIDs in dump在转储中包括 OID

  -O, --no-owner               skip restoration of object ownership in在明文格式中, 忽略恢复对象所属者

                               plain-text format 


  -s, --schema-only            dump only the schema, no data只转储模式, 不包括数据(不导出数据)

  -S, --superuser=NAME         superuser user name to use in plain-text format在转储中, 指定的超级用户名

  -t, --table=TABLE            dump the named table(s) only只转储指定名称的表

  -T, --exclude-table=TABLE    do NOT dump the named table(s)只转储指定名称的表

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

  --binary-upgrade             for use by upgrade utilities only只能由升级工具使用

  --column-inserts             dump data as INSERT commands with column names以带有列名的INSERT命令形式转储数据

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

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

  --exclude-table-data=TABLE   do NOT dump data for the named table(s)以INSERT命令,而不是COPY命令的形式转储数据

  --inserts                    dump data as INSERT commands, rather than COPY

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

  --no-synchronized-snapshots  do not use synchronized snapshots in parallel jobs

  --no-tablespaces             do not dump tablespace assignments不转储表空间分配信息

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

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

  --section=SECTION            dump named section (pre-data, data, or post-data)

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

  --use-set-session-authorization

                               use SET SESSION AUTHORIZATION commands instead of

                               ALTER OWNER commands to set ownership



Connection options:(控制连接的选项)

  -d, --dbname=DBNAME      database to dump 
数据库名

  -h, --host=HOSTNAME      database server host or socket directory数据库服务器的主机名或套接字目录

  -p, --port=PORT          database server port number数据库服务器的端口号

  -U, --username=NAME      connect as specified database user以指定的数据库用户联接

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


  -W, --password           force password prompt (should happen automatically)强制口令提示 (自动)


  --role=ROLENAME          do SET ROLE before dump
sql
备份:
pg_dump -h localhost -p 5432 -U tradesns -W -F c -b -v -f "/root/us2010.backup" us2010

恢复:
pg_restore -h 192.168.0.100 -p 5432 -U postgres -W -d us2011 -v "/root/us2010.backup"
备份:
pg_dump -h localhost -p 5432 -U tradesns -W -F c -b -v -f "/root/us2010.backup" us2010

恢复:
pg_restore -h 192.168.0.100 -p 5432 -U postgres -W -d us2011 -v "/root/us2010.backup"

pg_dump备份是一致的备份

因为pg_dump备份时使用的是serializable隔离级别, 如在hot_standby上面备份将使用repeatable read隔离级别.(目前hot_standby数据库不支持serializable隔离级别). 使用这两种隔离级别进行备份,整个事务从开始时就是看到的一个数据库的snapshot

优点:

​ pg_dump不会堵塞对表的DML操作

缺点:

使用pg_dump备份的时候,这个过程产生的垃圾数据不可以被VACUUM掉,所以一个很长时间的pg_dump操作会带来更多的垃圾数据.换句话说表的膨胀

DML(Data Manipulation Language)数据操纵语言,对数据库中的数据进行一些简单操作,如insert、delete、update、select等。DML操作是可以手动控制事务的开启、提交和回滚的。

DDL(Data Definition Language)数据定义语言,对数据库中的某些对象(例如database、table)进行管理,如create、alter和drop。DDL操作是隐性提交的,不能rollback

3.案例

3.1 配置环境

bash
postgres=# create database db_name encoding UTF8;
CREATE DATABASE

#创建表
test=# CREATE TABLE adg (c1 bigint  primary key,c2 bigint,c3 time,c4 character varying(136));
CREATE TABLE

test=# insert into adg(c1,c2,c3,c4) values (random()*10000000, random()*1000,now(),md5(random()::text));
INSERT 0 1

test=# select * from adg;
   c1    | c2  |       c3        |                c4                
---------+-----+-----------------+----------------------------------
 1364770 | 587 | 11:31:52.513474 | 4fc3602caf806531a7b51bf20ad3421d
(1 row)
postgres=# create database db_name encoding UTF8;
CREATE DATABASE

#创建表
test=# CREATE TABLE adg (c1 bigint  primary key,c2 bigint,c3 time,c4 character varying(136));
CREATE TABLE

test=# insert into adg(c1,c2,c3,c4) values (random()*10000000, random()*1000,now(),md5(random()::text));
INSERT 0 1

test=# select * from adg;
   c1    | c2  |       c3        |                c4                
---------+-----+-----------------+----------------------------------
 1364770 | 587 | 11:31:52.513474 | 4fc3602caf806531a7b51bf20ad3421d
(1 row)

3.2备份单个数据库包含数据

bash
#生成文本模式
[postgres@pg01 ~]$ pg_dump -U postgres -h 127.0.0.1 -p 5532 -d test >test.sql

-U 指定用户名
-p 指定端口
-d 指定数据库
#生成文本模式
[postgres@pg01 ~]$ pg_dump -U postgres -h 127.0.0.1 -p 5532 -d test >test.sql

-U 指定用户名
-p 指定端口
-d 指定数据库

3.3只备份结构

sernameRDS PostgreSQL实例高权限账号名称。
portRDS PostgreSQL实例端口。
table需要备份的表。可以用多个-t <table>选择多个表。
-Fc输出格式。-Fc表示输出自定义格式的存档,这个格式适合通过pg_restore进行导入还原。更多选项的详细说明请参见pg_dump
dbname目标数据库名称。
dumpdir导出的备份文件路径及名称
bash
#pg_dump -h '<hostname>' -U <username> -p <port> -t <table> -Fc <dbname> > <dumpdir>


[postgres@pg01 ~]$ pg_dump -U postgres -h 127.0.0.1 -p 5532 -s test >test_stru.sql

-s 只有结构

或者
[postgres@pg01 ~]$ pg_dump -U postgres -h 127.0.0.1 -p 5532 -s test -f test_stru.sql

-f 指定保存文件
#pg_dump -h '<hostname>' -U <username> -p <port> -t <table> -Fc <dbname> > <dumpdir>


[postgres@pg01 ~]$ pg_dump -U postgres -h 127.0.0.1 -p 5532 -s test >test_stru.sql

-s 只有结构

或者
[postgres@pg01 ~]$ pg_dump -U postgres -h 127.0.0.1 -p 5532 -s test -f test_stru.sql

-f 指定保存文件
bash
pg_dump -U afc edb -Fp -v --schema=afc --schema-only --no-owner --no-tablespaces  --no-privileges -T c_card_trade* -f 19_edb_schema_afc.sql 

-- 例子
pg_dump -U postgres -d usercenter -h 127.0.0.1 -p 5532 -Fp   --schema-only --no-owner --no-tablespaces --no-privileges > hello.sql

或者
pg_dump -U postgres -h 127.0.0.1 -p 5532 -s -d usercenter >usercenter_stru_2020_08_12.sql

这个方式含有用户
pg_dump -U afc edb -Fp -v --schema=afc --schema-only --no-owner --no-tablespaces  --no-privileges -T c_card_trade* -f 19_edb_schema_afc.sql 

-- 例子
pg_dump -U postgres -d usercenter -h 127.0.0.1 -p 5532 -Fp   --schema-only --no-owner --no-tablespaces --no-privileges > hello.sql

或者
pg_dump -U postgres -h 127.0.0.1 -p 5532 -s -d usercenter >usercenter_stru_2020_08_12.sql

这个方式含有用户

3.4备份单表

bash
#包含表结构和数据
[postgres@pg01 ~]$ pg_dump -U postgres -h 127.0.0.1 -p 5532 -t table_name -d db_name -x -O -f adg_data.sql

-t 指定表名字
-x  --no-privileges
-O  --no-owner

#只备份表结构,-s
[postgres@pg01 ~]$ pg_dump -U postgres -h 127.0.0.1 -p 5532 -s -t table_name db_name -f adg_str.sql

#备份某个模式所有表
[postgres@pg01 ~]$ pg_dump -U postgres -h 127.0.0.1 -p 5532 -s -t 'adg.*' test01 -f all_table_str.sql

#只备份表数据,不包含表结构,-a
pg_dump -U postgres -h 172.19.149.143 -p 5432  -W -t table_name -a db_name -x -O -f tb.sql
#包含表结构和数据
[postgres@pg01 ~]$ pg_dump -U postgres -h 127.0.0.1 -p 5532 -t table_name -d db_name -x -O -f adg_data.sql

-t 指定表名字
-x  --no-privileges
-O  --no-owner

#只备份表结构,-s
[postgres@pg01 ~]$ pg_dump -U postgres -h 127.0.0.1 -p 5532 -s -t table_name db_name -f adg_str.sql

#备份某个模式所有表
[postgres@pg01 ~]$ pg_dump -U postgres -h 127.0.0.1 -p 5532 -s -t 'adg.*' test01 -f all_table_str.sql

#只备份表数据,不包含表结构,-a
pg_dump -U postgres -h 172.19.149.143 -p 5432  -W -t table_name -a db_name -x -O -f tb.sql
bash
#在数据库里面备份一张表结构
create table test11 as select * from test15 where 1=2 ;
#在数据库里面备份一张表结构
create table test11 as select * from test15 where 1=2 ;

排除指定表

bash
pg_dump -h '<hostname>' -U <username> -p <port> -T <table> -Fc <dbname> > <dumpdir>

示例:
pg_dump -h 'pgm-bpxxxxx.pg.rds.aliyuncs.com' -U test123 -p 3433 -T products1 -Fc testdb2 > /tmp/testdb2.dump

--备份某个模式emp开头表,排除一张表
pg_dump -t 'mysch.t*' -T mysch.t1 mydb > /pgbackup/sche1_t_2019.sql
pg_dump -h '<hostname>' -U <username> -p <port> -T <table> -Fc <dbname> > <dumpdir>

示例:
pg_dump -h 'pgm-bpxxxxx.pg.rds.aliyuncs.com' -U test123 -p 3433 -T products1 -Fc testdb2 > /tmp/testdb2.dump

--备份某个模式emp开头表,排除一张表
pg_dump -t 'mysch.t*' -T mysch.t1 mydb > /pgbackup/sche1_t_2019.sql

导出insert模式

导出test数据库中表adg的数据,以insert语句的形式,默认是copy形式

bash
[postgres@pg01 ~]$ pg_dump -U postgres -h 127.0.0.1 -p 5532 -t adg test --column-inserts -f adg_data_in.sql
[postgres@pg01 ~]$ pg_dump -U postgres -h 127.0.0.1 -p 5532 -t adg test --column-inserts -f adg_data_in.sql

文件不落地,实现两个数据库迁移

  • 配置两个数据库之间的信任关系 (必须)
host all all 192.168.18.0/24 trust
host all all 192.168.18.0/24 trust
  • 数据库之间进行某个数据库迁移
bash
pg_dump -h pg1 -p 1922 -U postgres testdblpsql -h pg2 -p 5432 -U postgres testdb
pg_dump -h pg1 -p 1922 -U postgres testdblpsql -h pg2 -p 5432 -U postgres testdb
  • 迁移所有的数据库到目标数据库
bash
pg_dumpall -h pg1 -p 1922 -U postgres Ipsql-h pg2 -p 5432 -U postgres
pg_dumpall -h pg1 -p 1922 -U postgres Ipsql-h pg2 -p 5432 -U postgres

注意:

1、PG数据库之间迁移无需配置dblink,只要指定主机名和端口即可。

2、由于用的是管道符,所以必须配置两个数据库远程登录信任关系。

3、迁移时两个数据库必须要有相同的表空间,需要提前创建。

3.5恢复

普通文本模式

bash
 pg_dump -U postgres -h 127.0.0.1 -p 5532  -d db_name -x -O -f dataName.sql
 pg_dump -U postgres -h 127.0.0.1 -p 5532  -d db_name -x -O -f dataName.sql

split切割文件

bash
[postgres@pg01 ~]$ pg_dump -U postgres -h 127.0.0.1 -p 5532 -d test01  | split -b 100k -  ~/test01
[postgres@pg01 ~]$ ls
test01aa

#恢复
 cat /test01* | psql   -U postgres -h 127.0.0.1 -p 5532 -d test01
[postgres@pg01 ~]$ pg_dump -U postgres -h 127.0.0.1 -p 5532 -d test01  | split -b 100k -  ~/test01
[postgres@pg01 ~]$ ls
test01aa

#恢复
 cat /test01* | psql   -U postgres -h 127.0.0.1 -p 5532 -d test01

二进制模式

bash
1. 先备份: 

   pg_dump -U postgres  -h 127.0.0.1 -p 5532 -F t -f /vendemo.tar db_name

 . 恢复:

   pg_restore -U postgres  -h 127.0.0.1 -p 5532 -d bk01 /vendemo.tar 

2. 先备份: 

   pg_dump -U postgres  -h 127.0.0.1 -p 5532 -F c -f /vendemo.tar vendemo  备份下来有300多k

 . 恢复:

   pg_restore -U postgres  -h 127.0.0.1 -p 5532 -d bk01 /vendemo.tar
1. 先备份: 

   pg_dump -U postgres  -h 127.0.0.1 -p 5532 -F t -f /vendemo.tar db_name

 . 恢复:

   pg_restore -U postgres  -h 127.0.0.1 -p 5532 -d bk01 /vendemo.tar 

2. 先备份: 

   pg_dump -U postgres  -h 127.0.0.1 -p 5532 -F c -f /vendemo.tar vendemo  备份下来有300多k

 . 恢复:

   pg_restore -U postgres  -h 127.0.0.1 -p 5532 -d bk01 /vendemo.tar

压缩备份与恢复

bash
[postgres@pg01 ~]$ pg_dump -U postgres -h 127.0.0.1 -p 5532 test01 |gzip > test01.gz

#恢复
[postgres@pg01 ~]$ gunzip  -c test01.gz |pg_dump -U postgres -h 127.0.0.1 -p 5532 test01
或者
[postgres@pg01 ~]$ cat test01.gz |gunzip  |pg_dump -U postgres -h 127.0.0.1 -p 5532 test01
[postgres@pg01 ~]$ pg_dump -U postgres -h 127.0.0.1 -p 5532 test01 |gzip > test01.gz

#恢复
[postgres@pg01 ~]$ gunzip  -c test01.gz |pg_dump -U postgres -h 127.0.0.1 -p 5532 test01
或者
[postgres@pg01 ~]$ cat test01.gz |gunzip  |pg_dump -U postgres -h 127.0.0.1 -p 5532 test01

恢复指定表数据

usernameRDS PostgreSQL实例高权限账号名称。
portRDS PostgreSQL实例端口。
dbname待恢复数据的数据库名称。
table待恢复的表名。
-c-c表示在恢复时,会先删除数据库对象再恢复。更多选项的详细说明请参见pg_restore
dumpdir备份文件路径及名称
bash
#语法:
pg_restore -h '<hostname>' -U <username> -p <port> -d <dbname> -t <table> -c <dumpdir>

#示例
[postgres@pg01 ~]$ pg_restore -h '127.0.0.1' -U test123 -p 3433 -d testdb2 -t products -c /tmp/testdb.dump
#语法:
pg_restore -h '<hostname>' -U <username> -p <port> -d <dbname> -t <table> -c <dumpdir>

#示例
[postgres@pg01 ~]$ pg_restore -h '127.0.0.1' -U test123 -p 3433 -d testdb2 -t products -c /tmp/testdb.dump

恢复数据库结构

usernameRDS PostgreSQL实例高权限账号名称。
portRDS PostgreSQL实例端口。
dbname待恢复模式的数据库名称。
-s-s表示仅恢复模式,不恢复数据。更多选项的详细说明请参见pg_restore
dumpdir备份文件路径及名称
bash
#语法:
pg_restore -h '<hostname>' -U <username> -p <port> -d <dbname> -s <dumpdir>

[postgres@pg01 ~]$pg_restore -h '127.0.0.1' -U test123 -p 3433 -d testdb4 -s /tmp/testdb2.dump
#语法:
pg_restore -h '<hostname>' -U <username> -p <port> -d <dbname> -s <dumpdir>

[postgres@pg01 ~]$pg_restore -h '127.0.0.1' -U test123 -p 3433 -d testdb4 -s /tmp/testdb2.dump

toc文件

  • 实现精细度恢复
bash
#生成备份
pg_dump -U postgres  -h 127.0.0.1 -p 5532 -Fc  -f test.dump test

#产生toc文件
pg_restore -l -f test.toc  test.dump

#查看toc文件内容,适合数据量小的文件
cat test.toc
; Selected TOC Entries:
;
215; 1259 40977 TABLE public adg postgres
4053; 0 40977 TABLE DATA public adg postgres
3909; 2606 40981 CONSTRAINT public adg adg_pkey postgres


#开始恢复
pg_restore -F c -L test.toc -d test test.dump
#生成备份
pg_dump -U postgres  -h 127.0.0.1 -p 5532 -Fc  -f test.dump test

#产生toc文件
pg_restore -l -f test.toc  test.dump

#查看toc文件内容,适合数据量小的文件
cat test.toc
; Selected TOC Entries:
;
215; 1259 40977 TABLE public adg postgres
4053; 0 40977 TABLE DATA public adg postgres
3909; 2606 40981 CONSTRAINT public adg adg_pkey postgres


#开始恢复
pg_restore -F c -L test.toc -d test test.dump

导出数据直接进行恢复

bash
pg_dump -h 127.0.0.1 -U postgres -d testdb|psql -h 127.0.0.1 -U postgres -d testdb1
pg_dump -h 127.0.0.1 -U postgres -d testdb|psql -h 127.0.0.1 -U postgres -d testdb1

并行导入导出

-j4

4.常用维护语句

sql
 pg_dump -U postgres -h 127.0.0.1 -p 5532 -d fllowordersystem  --no-owner --no-tablespaces --no-privileges >fll.sql
 
 pg_dump -U postgres -h 127.0.0.1 -p 5532  -t fllowordersetting fllowordersystem  --no-owner --no-tablespaces --no-privileges >fllowordersetting.sql
 pg_dump -U postgres -h 127.0.0.1 -p 5532 -d fllowordersystem  --no-owner --no-tablespaces --no-privileges >fll.sql
 
 pg_dump -U postgres -h 127.0.0.1 -p 5532  -t fllowordersetting fllowordersystem  --no-owner --no-tablespaces --no-privileges >fllowordersetting.sql
  • 创建用户和密码
sql
postgres@postgres=>create user videoh5 with password 'genngiff61';
postgres@postgres=>create database videosh5 owner videoh5;
postgres@postgres=>grant all privileges on database videosh5 to videoh5;

GRANT SELECT,update,INSERT,delete ON ALL TABLES IN SCHEMA public TO gendanuser ;
postgres@postgres=>create user videoh5 with password 'genngiff61';
postgres@postgres=>create database videosh5 owner videoh5;
postgres@postgres=>grant all privileges on database videosh5 to videoh5;

GRANT SELECT,update,INSERT,delete ON ALL TABLES IN SCHEMA public TO gendanuser ;
  • import
sql
psql -U lianghua -h 127.0.0.1 -p 5532 -f my.sql db_name
psql -U lianghua -h 127.0.0.1 -p 5532 -f my.sql db_name
  • 删除日志脚本
sql
reserved_num=10  #保留文件数
file_dir=/data/pgdata/data/log
date=$(date "+%Y-%m-%d")

cd $file_dir   #进入备份目录
file_num=$(ls -l | wc -l)   #当前有几个文件夹,即几个备份

while(( $file_num > $reserved_num ))
do
    old_file=$(ls -rt | head -1)         #获取最旧的那个备份文件夹
    echo  $date "Delete File:"$old_file
    rm -f "${file_dir}/$old_file"
    let "file_num--"
done 

ls -l $file_dir
reserved_num=10  #保留文件数
file_dir=/data/pgdata/data/log
date=$(date "+%Y-%m-%d")

cd $file_dir   #进入备份目录
file_num=$(ls -l | wc -l)   #当前有几个文件夹,即几个备份

while(( $file_num > $reserved_num ))
do
    old_file=$(ls -rt | head -1)         #获取最旧的那个备份文件夹
    echo  $date "Delete File:"$old_file
    rm -f "${file_dir}/$old_file"
    let "file_num--"
done 

ls -l $file_dir