postgresql中可以使用pg_dump來(lái)備份數(shù)據(jù)庫(kù)。pg_dump是用于備份PostgreSQL數(shù)據(jù)庫(kù)的工具。它可以在數(shù)據(jù)庫(kù)正在使用的時(shí)候進(jìn)行完整一致的備份,并不阻塞其它用戶對(duì)數(shù)據(jù)庫(kù)的訪問(wèn)。
成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供托克托企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站建設(shè)、成都網(wǎng)站制作、H5高端網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為托克托眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進(jìn)行中。
用法:
pg_dump [選項(xiàng)]... [數(shù)據(jù)庫(kù)名字]
一般選項(xiàng):
-f, --file=FILENAME output file or directory name
-F, --format=c|d|t|p output file format (custom, directory, tar, plain text)
-v, --verbose 詳細(xì)模式
-Z, --compress=0-9 被壓縮格式的壓縮級(jí)別
--lock-wait-timeout=TIMEOUT 在等待表鎖超時(shí)后操作失敗
--help 顯示此幫助信息, 然后退出
--versoin 輸出版本信息, 然后退出
示例:
備份數(shù)據(jù)庫(kù),指令如下:
pg_dump -h 164.82.233.54 -U postgres databasename C:databasename.bak
開(kāi)始-運(yùn)行-cmd 彈出dos控制臺(tái);然后 在控制臺(tái)里,進(jìn)入PostgreSQL安裝目錄bin下:
cd C:Program FilesPostgreSQL9.0bin
最后執(zhí)行備份指令:
pg_dump -h 164.82.233.54 -U postgres databasename C:databasename.bak
指令解釋:
pg_dump 是備份數(shù)據(jù)庫(kù)指令,164.82.233.54是數(shù)據(jù)庫(kù)的ip地址(必須保證數(shù)據(jù)庫(kù)允許外部訪問(wèn)的權(quán)限哦~),當(dāng)然本地的數(shù)據(jù)庫(kù)ip寫 localhost;
postgres 是數(shù)據(jù)庫(kù)的用戶名;databasename 是數(shù)據(jù)庫(kù)名。
意思是導(dǎo)出到C:databasename.bak文件里,如果沒(méi)有寫路徑,單單寫databasename.bak文件名,那么備份文件會(huì)保存在C: Program FilesPostgreSQL9.0bin 文件夾里。
推薦學(xué)習(xí)《Python教程》。
壓縮表從名字上來(lái)看,簡(jiǎn)單理解為壓縮后的表,也就是把原始表根據(jù)一定的壓縮算法按照一定的壓縮比率壓縮后生成的表。
1.1 壓縮能力強(qiáng)的產(chǎn)品
表壓縮后從磁盤占用上看要比原始表要小很多。如果你熟悉列式數(shù)據(jù)庫(kù),那對(duì)這個(gè)概念一定不陌生。比如,基于 PostgreSQL 的列式數(shù)據(jù)庫(kù) Greenplum;早期基于 MySQL 的列式數(shù)據(jù)庫(kù) inforbright;或者 Percona 的產(chǎn)品 tokudb 等,都是有壓縮能力非常強(qiáng)的數(shù)據(jù)庫(kù)產(chǎn)品。
1.2 為什么要用壓縮表?
情景一:磁盤大小為 1T,不算其他的空間占用,只能存放 10 張 100G 大小的表。如果這些表以一定的比率壓縮后,比如每張表從 100G 壓縮到 10G,那同樣的磁盤可以存放 100 張表,表的容量是原來(lái)的 10 倍。情景二:默認(rèn) MySQL 頁(yè)大小 16K,而 OS 文件系統(tǒng)一般塊大小為 4K,所以在 MySQL 在刷臟頁(yè)的過(guò)程中,有一定的概率出現(xiàn)頁(yè)沒(méi)寫全而導(dǎo)致數(shù)據(jù)壞掉的情形。比如 16K 的頁(yè)寫了 12K,剩下 4K 沒(méi)寫成功,導(dǎo)致 MySQL 頁(yè)數(shù)據(jù)損壞。這個(gè)時(shí)候就算通過(guò) Redo Log 也恢復(fù)不了,因?yàn)閹缀跤兴械年P(guān)系數(shù)據(jù)庫(kù)采用的 Redo Log 都記錄了數(shù)據(jù)頁(yè)的偏移量,此時(shí)就算通過(guò) Redo Log 恢復(fù)后,數(shù)據(jù)也是錯(cuò)誤的。所以 MySQL 在刷臟數(shù)據(jù)之前,會(huì)把這部分?jǐn)?shù)據(jù)先寫入共享表空間里的 DOUBLE WRITE BUFFER 區(qū)域來(lái)避免這種異常。此時(shí)如果 MySQL 采用壓縮表,并且每張表頁(yè)大小和磁盤塊大小一致,比如也是 4K,那 DOUBLE WRITE BUFFER 就可以不需要,這部分開(kāi)銷就可以規(guī)避掉了。查看文件系統(tǒng)的塊大小:
root@ytt-pc:/home/ytt#??tune2fs?-l?/dev/mapper/ytt--pc--vg-root??|?grep?-i?'block?size'Block size: ? ? ? ? ? ? ? 4096
1.3 壓縮表的優(yōu)勢(shì)
壓縮表的優(yōu)點(diǎn)非常明顯,占用磁盤空間?。∮捎谡加每臻g小,從磁盤置換到內(nèi)存以及之后經(jīng)過(guò)網(wǎng)絡(luò)傳輸都非常節(jié)省資源。
簡(jiǎn)單來(lái)講:節(jié)省磁盤 IO,減少網(wǎng)絡(luò) IO。
1.4 壓縮表的缺陷
當(dāng)然壓縮表也有缺點(diǎn),壓縮表的寫入(INSERT,UPDATE,DELETE)比普通表要消耗更多的 CPU 資源。
壓縮表的寫入涉及到解壓數(shù)據(jù),更新數(shù)據(jù),再壓縮數(shù)據(jù),比普通表多了解壓和再壓縮兩個(gè)步驟,壓縮和解壓縮需要消耗一定的 CPU 資源。所以需要選擇一個(gè)比較優(yōu)化的壓縮算法。
1.5 MySQL 支持的壓縮算法
這塊是 MySQL 所有涉及到壓縮的基礎(chǔ),不僅僅用于壓縮表,也用于其它地方。比如客戶端請(qǐng)求到 MySQL 服務(wù)端的數(shù)據(jù)壓縮;主從之間的壓縮傳輸;利用克隆插件來(lái)復(fù)制數(shù)據(jù)庫(kù)操作的壓縮傳輸?shù)鹊取?/p>
從下面結(jié)果可以看到 MySQL 支持的壓縮算法為 zlib 和 zstd,MySQL 默認(rèn)壓縮算法為 zlib,當(dāng)然你也可以選擇非 zlib 算法,比如 zstd。至于哪種壓縮算法最優(yōu),暫時(shí)沒(méi)辦法簡(jiǎn)單量化,依賴表中的數(shù)據(jù)分布或者業(yè)務(wù)請(qǐng)求。
1、安裝postgresql
yum install postgresql postgresql-server
mysql占用端口3306 pgsql是5432
2、導(dǎo)入整個(gè)數(shù)據(jù)庫(kù)
psql -U postgres(用戶名) 數(shù)據(jù)庫(kù)名(缺省時(shí)同用戶名) /data/dum.sql
3、導(dǎo)出整個(gè)數(shù)據(jù)庫(kù)
pg_dump -h localhost -U postgres(用戶名) 數(shù)據(jù)庫(kù)名(缺省時(shí)同用戶名) /data/dum.sql
4、導(dǎo)出某個(gè)表
pg_dump -h localhost -U postgres(用戶名) 數(shù)據(jù)庫(kù)名(缺省時(shí)同用戶名) -t table(表名) /data/dum.sql
5、壓縮方法
一般用dump導(dǎo)出數(shù)據(jù)會(huì)比較大,推薦使用xz壓縮
壓縮方法 xz dum.sql 會(huì)生成 dum.sql.xz 的文件
6、xz壓縮數(shù)據(jù)倒數(shù)數(shù)據(jù)庫(kù)方法
xzcat /data/dum.sql.xz | psql -h localhost -U postgres(用戶名) 數(shù)據(jù)庫(kù)名(缺省時(shí)同用戶名)
免安裝版下載鏈接:
64位:
32位:
使用方法:
1. 解壓縮至某個(gè)目錄,例如:G:/pgsql
2. 創(chuàng)建一個(gè)用于存放數(shù)據(jù)文件的目錄,例如:G:/pgsql/data
3. 初始化數(shù)據(jù)庫(kù):G:/pgsql/bin/initdb.exe -D G:/pgsql/data
4. 啟動(dòng)數(shù)據(jù)庫(kù):G:/pgsql/bin/pg_ctl.exe start -D G:/pgsql/data -l g:/aaa.log
(其中aaa.log為數(shù)據(jù)庫(kù)日志文件名稱)
5. 登錄維護(hù)數(shù)據(jù)庫(kù):G:/pgsql/bin/psql.exe postgres
6. 創(chuàng)建登錄用戶:create user myuser;
然后就可以用pgadmin(大象)進(jìn)行登陸了,初次登陸時(shí)可以使用任意密碼,登陸之后可以再創(chuàng)建別的用戶,或者修改密碼。
用上述方法的話,重啟操作系統(tǒng)后需要重新執(zhí)行步驟4??梢宰龀梢粋€(gè)批處理。
附上:使PG注冊(cè)為系統(tǒng)服務(wù)的方法,我沒(méi)有試驗(yàn)成功(報(bào)錯(cuò)誤碼1063,感覺(jué)是權(quán)限方面的問(wèn)題)
1. 概述
cstore_fdw實(shí)現(xiàn)了 PostgreSQL 數(shù)據(jù)庫(kù)的列式存儲(chǔ)。列存儲(chǔ)非常適合用于數(shù)據(jù)分析的場(chǎng)景,數(shù)據(jù)分析的場(chǎng)景下數(shù)據(jù)是批量加載的。
這個(gè)擴(kuò)展使用了Optimized Row Columnar (ORC)數(shù)據(jù)存儲(chǔ)格式,ORC改進(jìn)了Facebook的RCFile格式,帶來(lái)如下好處:
壓縮:將內(nèi)存和磁盤中數(shù)據(jù)大小削減到2到4倍??梢詳U(kuò)展以支持不同壓縮算法。
列投影:只提取和查詢相關(guān)的列數(shù)據(jù)。提升IO敏感查詢的性能。
跳過(guò)索引:為行組存儲(chǔ)最大最小統(tǒng)計(jì)值,并利用它們跳過(guò)無(wú)關(guān)的行。
2. 使用
cstore_fdw的安裝和使用都非常簡(jiǎn)單,可以參考官方資料。
thub.com/citusdata/cstore_fdw
注)注意cstore_fdw只支持PostgreSQL9.3和9.4 。
下面做幾個(gè)簡(jiǎn)單的性能對(duì)比,看看cstore_fdw究竟能帶來(lái)多大的性能提升。
2.1 數(shù)據(jù)加載
2.1.1 普通表
CREATE TABLE tb1
(
id int,
c1 TEXT,
c2 TEXT,
c3 TEXT,
c4 TEXT,
c5 TEXT,
c6 TEXT,
c7 TEXT,
c8 TEXT,
c9 TEXT,
c10 TEXT
);
注:要和普通表的全表掃描作對(duì)比,所以不建主鍵和索引。
[postgres@node2 chenhj]$ time psql -p 40382 -At -F, -c "select id,id::text,id::text,id::text,id::text,id::text,id::text,id::text,id::text,id::text,id::text from generate_series(1,10000000) id"|time psql -p 40382 -c "copy tb1 from STDIN with CSV"
COPY 10000000
1.56user 1.00system 6:42.39elapsed 0%CPU (0avgtext+0avgdata 7632maxresident)k
776inputs+0outputs (17major+918minor)pagefaults 0swaps
real 6m42.402s
user 0m15.174s
sys 0m14.904s
postgres=# select pg_total_relation_size('tb1'::regclass);
pg_total_relation_size
------------------------
1161093120
(1 row)
postgres=# \timing
Timing is on.
postgres=# analyze tb1;
ANALYZE
Time: 11985.070 ms
插入1千萬(wàn)條記錄,數(shù)據(jù)占用存儲(chǔ)大小1.16G,插入耗時(shí)6分42秒,分析耗時(shí)12秒。
2.1.2 cstore表
$ mkdir -p /home/chenhj/data94/cstore
CREATE EXTENSION cstore_fdw;
CREATE SERVER cstore_server FOREIGN DATA WRAPPER cstore_fdw;
CREATE FOREIGN TABLE cstb1
(
id int,
c1 TEXT,
c2 TEXT,
c3 TEXT,
c4 TEXT,
c5 TEXT,
c6 TEXT,
c7 TEXT,
c8 TEXT,
c9 TEXT,
c10 TEXT
)
SERVER cstore_server
OPTIONS(filename '/home/chenhj/data94/cstore/cstb1.cstore',
compression 'pglz');
[postgres@node2 chenhj]$ time psql -p 40382 -At -F, -c "select id,id::text,id::text,id::text,id::text, id::text,id::text,id::text,id::text,id::text,id::text from generate_series(1,10000000) id"|time psql -p 40382 -c "copy cstb1 from STDIN with CSV"
COPY 10000000
1.53user 0.78system 7:35.15elapsed 0%CPU (0avgtext+0avgdata 7632maxresident)k
968inputs+0outputs (20major+920minor)pagefaults 0swaps
real 7m35.520s
user 0m14.809s
sys 0m14.170s
[postgres@node2 chenhj]$ ls -l /home/chenhj/data94/cstore/cstb1.cstore
-rw------- 1 postgres postgres 389583021 Jun 23 17:32 /home/chenhj/data94/cstore/cstb1.cstore
postgres=# \timing
Timing is on.
postgres=# analyze cstb1;
ANALYZE
Time: 5946.476 ms
插入1千萬(wàn)條記錄,數(shù)據(jù)占用存儲(chǔ)大小390M,插入耗時(shí)7分35秒,分析耗時(shí)6秒。
使用cstore列存儲(chǔ)后,數(shù)據(jù)占用存儲(chǔ)大小降到普通表的3分之1。需要說(shuō)明的是,由于所有TEXT列填充了隨機(jī)數(shù)據(jù),壓縮率不算高,某些實(shí)際的應(yīng)用場(chǎng)景下壓縮效果會(huì)比這更好。
2.2 Text列的like查詢性能對(duì)比
2.2.1 普通表
清除文件系統(tǒng)緩存,并重啟PostgreSQL
[postgres@node2 chenhj]$ pg_ctl -D /home/chenhj/data94 -l logfile94 restart
[root@node2 ~]# free
total used free shared buffers cached
Mem: 2055508 771356 1284152 0 9900 452256
-/+ buffers/cache: 309200 1746308
Swap: 4128760 387624 3741136
[root@node2 ~]# echo 1 /proc/sys/vm/drop_caches
[root@node2 ~]# free
total used free shared buffers cached
Mem: 2055508 326788 1728720 0 228 17636
-/+ buffers/cache: 308924 1746584
Swap: 4128760 381912 3746848
對(duì)Text列執(zhí)行l(wèi)ike查詢
[postgres@node2 chenhj]$ iostat -k dm-2
Linux 2.6.32-71.el6.x86_64 (node2) 06/23/14 _x86_64_ (2 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.80 0.00 0.38 3.42 0.00 95.40
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
dm-2 58.55 330.68 212.08 7351441 4714848
[postgres@node2 chenhj]$ time psql -p 40382 -c "select count(*) from tb1 where c1 like '%66'"
count
--------
100000
(1 row)
real 0m7.051s
user 0m0.001s
sys 0m0.004s
[postgres@node2 chenhj]$ iostat -k dm-2
Linux 2.6.32-71.el6.x86_64 (node2) 06/23/14 _x86_64_ (2 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.80 0.00 0.38 3.43 0.00 95.39
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
dm-2 58.90 381.53 211.90 8489597 4714956
耗時(shí)7.1秒,產(chǎn)生IO讀1.14G,IO寫108K。
不清文件系統(tǒng)緩存,不重啟PostgreSQL,再執(zhí)行一次。消耗時(shí)間降到1.6秒,幾乎不產(chǎn)生IO。
[postgres@node2 chenhj]$ iostat -k dm-2
Linux 2.6.32-71.el6.x86_64 (node2) 06/23/14 _x86_64_ (2 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.80 0.00 0.38 3.43 0.00 95.39
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
dm-2 58.81 332.20 213.06 7350301 4714364
[postgres@node2 chenhj]$ time psql -p 40382 -c "select count(*) from tb1 where c1 like '%66'"
count
--------
100000
(1 row)
real 0m1.601s
user 0m0.002s
sys 0m0.001s
[postgres@node2 chenhj]$ iostat -k dm-2
Linux 2.6.32-71.el6.x86_64 (node2) 06/23/14 _x86_64_ (2 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.80 0.00 0.38 3.43 0.00 95.38
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
dm-2 58.80 332.12 213.01 7350337 4714364
2.2.2 cstore表
清除文件系統(tǒng)緩存,并重啟PostgreSQL
[postgres@node2 chenhj]$ pg_ctl -D /home/chenhj/data94 -l logfile94 restart
[root@node2 ~]# echo 1 /proc/sys/vm/drop_caches
對(duì)Text列執(zhí)行l(wèi)ike查詢
[postgres@node2 chenhj]$ iostat -k dm-2
Linux 2.6.32-71.el6.x86_64 (node2) 06/23/14 _x86_64_ (2 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.80 0.00 0.38 3.38 0.00 95.45
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
dm-2 58.12 376.42 209.04 8492017 4716048
[postgres@node2 chenhj]$ time psql -p 40382 -c "select count(*) from cstb1 where c1 like '%66'"
count
--------
100000
(1 row)
real 0m2.786s
user 0m0.002s
sys 0m0.003s
[postgres@node2 chenhj]$ iostat -k dm-2
Linux 2.6.32-71.el6.x86_64 (node2) 06/23/14 _x86_64_ (2 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.80 0.00 0.38 3.38 0.00 95.44
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
dm-2 58.12 378.75 208.89 8550761 4716048
耗時(shí)2.8秒,產(chǎn)生IO讀59M,IO寫0K。執(zhí)行時(shí)間優(yōu)化的雖然不是太多,但I(xiàn)O大大減少,可見(jiàn)列投影起到了作用。
不清文件系統(tǒng)緩存,不重啟PostgreSQL,再執(zhí)行一次。消耗時(shí)間降到1.4秒,幾乎不產(chǎn)生IO。
[postgres@node2 chenhj]$ iostat -k dm-2
Linux 2.6.32-71.el6.x86_64 (node2) 06/23/14 _x86_64_ (2 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.80 0.00 0.38 3.36 0.00 95.47
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
dm-2 57.75 376.33 207.58 8550809 4716524
[postgres@node2 chenhj]$ time psql -p 40382 -c "select count(*) from cstb1 where c1 like '%66'"
count
--------
100000
(1 row)
real 0m1.424s
user 0m0.002s
sys 0m0.001s
[postgres@node2 chenhj]$ iostat -k dm-2
Linux 2.6.32-71.el6.x86_64 (node2) 06/23/14 _x86_64_ (2 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.80 0.00 0.38 3.36 0.00 95.47
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
dm-2 57.70 375.96 207.38 8550809 4716588
2.3 對(duì)Int列執(zhí)行=查詢
2.3.1 普通表
清除文件系統(tǒng)緩存,并重啟PostgreSQL后
[postgres@node2 chenhj]$ pg_ctl -D /home/chenhj/data94 -l logfile94 restart
[root@node2 ~]# echo 1 /proc/sys/vm/drop_caches
對(duì)Int列執(zhí)行=查詢
[postgres@node2 chenhj]$ iostat -k dm-2
Linux 2.6.32-71.el6.x86_64 (node2) 06/23/14 _x86_64_ (2 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.79 0.00 0.37 3.33 0.00 95.50
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
dm-2 57.25 373.21 205.67 8560897 4717624
[postgres@node2 chenhj]$ time psql -p 40382 -c "select count(*) from tb1 where id =666666"
count
-------
1
(1 row)
real 0m6.844s
user 0m0.002s
sys 0m0.006s
[postgres@node2 chenhj]$ iostat -k dm-2
Linux 2.6.32-71.el6.x86_64 (node2) 06/23/14 _x86_64_ (2 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.79 0.00 0.37 3.34 0.00 95.49
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
dm-2 57.60 422.57 205.54 9699161 4717708
耗時(shí)6.8秒,產(chǎn)生IO讀1.14G,IO寫84K
不清緩存,再執(zhí)行一次。消耗時(shí)間降到1.1秒,幾乎不產(chǎn)生IO。
[postgres@node2 chenhj]$ iostat -k dm-2
Linux 2.6.32-71.el6.x86_64 (node2) 06/23/14 _x86_64_ (2 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.79 0.00 0.37 3.33 0.00 95.50
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
dm-2 57.44 421.37 204.97 9699177 4718032
[postgres@node2 chenhj]$ time psql -p 40382 -c "select count(*) from tb1 where id =666666"
count
-------
當(dāng)前名稱:postgresql壓縮的簡(jiǎn)單介紹
本文路徑:http://jinyejixie.com/article12/dsdgogc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、軟件開(kāi)發(fā)、服務(wù)器托管、微信公眾號(hào)、動(dòng)態(tài)網(wǎng)站、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
移動(dòng)網(wǎng)站建設(shè)知識(shí)