postgresql安裝及性能測試
1. Postgresql介紹
Postgresql是一款功能強大的開源對象關系型數據庫管理系統(ORDBMS),以其穩定性、擴展性和標準的SQL支持而聞名。它支持復雜查詢、外鍵、觸發器、視圖、事務完整性、多版本并發控制(MVCC)等特性,且具有豐富的擴展能力,可以通過插件擴展其功能。Postgresql適用于各種場景,從小型應用到大規模企業級應用。
2. Postgresql安裝
Postgresql可以安裝在多種操作系統上,包括Linux、macOS和Windows。以下是不同系統的安裝步驟:
2.1 在Linux上安裝Postgresql(以Ubuntu為例)
-
更新包列表并安裝依賴:
sudo?apt-get?update
sudo?apt-get?install?wget?ca-certificates
-
添加Postgresql倉庫:
wget?-qO?-?https://www.Postgresql.org/media/keys/ACCC4CF8.asc?|?sudo?apt-key?add?-
echo?"deb?http://apt.Postgresql.org/pub/repos/apt/?$(lsb_release?-cs)-pgdg?main"?|?sudo?tee?/etc/apt/sources.list.d/pgdg.list
-
更新包列表并安裝Postgresql:
sudo?apt-get?update
sudo?apt-get?install?Postgresql?Postgresql-contrib
-
啟動Postgresql服務:
sudo?systemctl?start?Postgresql
-
設置Postgresql在系統啟動時自動啟動:
sudo?systemctl?enable?Postgresql
2.2 數據庫管理操作
-
查看數據庫版本:
test@ubuntu-svr:~$?sudo?-u?postgres?psql?-c?"SELECT?version();"
????????????????????????????????????????????????????????????????version
----------------------------------------------------------------------------------------------------------------------------------------
?Postgresql?14.11?(Ubuntu?14.11-0ubuntu0.22.04.1)?on?x86_64-pc-linux-gnu,?compiled?by?gcc?(Ubuntu?11.4.0-1ubuntu1~22.04)?11.4.0,?64-bit
(1?row)
-
登錄與登出數據庫
test@ubuntu-svr:~$?sudo?su?postgres?????#?切換到postgres用戶
postgres@ubuntu-svr:/home/test$?psql????#?通過psql工具進入數據庫
psql?(16.3?(Ubuntu?16.3-1.pgdg22.04+1),?server?14.11?(Ubuntu?14.11-0ubuntu0.22.04.1))
Type?"help"?for?help.
postgres=#?\q?????#?退出數據庫
-
查看數據庫與用戶
test@ubuntu-svr:~$?sudo?su?postgres?????#?切換到postgres用戶
postgres@ubuntu-svr:/home/test$?psql????#?通過psql工具進入數據庫
psql?(16.3?(Ubuntu?16.3-1.pgdg22.04+1),?server?14.11?(Ubuntu?14.11-0ubuntu0.22.04.1))
Type?"help"?for?help.
postgres=#?\l???#?查看數據庫列表,默認有三個
???????????????????????????????????????????????????????List?of?databases
???Name????|??Owner???|?Encoding?|?Locale?Provider?|???Collate???|????Ctype????|?ICU?Locale?|?ICU?Rules?|???Access?privileges
-----------+----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------
?postgres??|?postgres?|?UTF8?????|?libc????????????|?en_US.UTF-8?|?en_US.UTF-8?|????????????|???????????|
?template0?|?postgres?|?UTF8?????|?libc????????????|?en_US.UTF-8?|?en_US.UTF-8?|????????????|???????????|?=c/postgres??????????+
???????????|??????????|??????????|?????????????????|?????????????|?????????????|????????????|???????????|?postgres=CTc/postgres
?template1?|?postgres?|?UTF8?????|?libc????????????|?en_US.UTF-8?|?en_US.UTF-8?|????????????|???????????|?=c/postgres??????????+
???????????|??????????|??????????|?????????????????|?????????????|?????????????|????????????|???????????|?postgres=CTc/postgres
(3?rows)
postgres=#?\du????#?查看數據庫用戶
?????????????????????????????List?of?roles
?Role?name?|?????????????????????????Attributes
-----------+------------------------------------------------------------
?postgres??|?Superuser,?Create?role,?Create?DB,?Replication,?Bypass?RLS
-
創建數據庫用戶并授權
#?創建用戶并指定權限
postgres=#?CREATE?USER?test?WITH?PASSWORD?'123456';
CREATE?ROLE
postgres=#?ALTER?USER?test?WITH?SUPERUSER;
ALTER?ROLE
#?刪除用戶
postgres=#?DROP?USER?xxx;
-
查看數據庫配置文件路徑
postgres@ubuntu-svr:~$?psql
psql?(16.3?(Ubuntu?16.3-1.pgdg22.04+1),?server?14.11?(Ubuntu?14.11-0ubuntu0.22.04.1))
Type?"help"?for?help.
postgres=#?show?hba_file;
??????????????hba_file
-------------------------------------
?/etc/postgresql/14/main/pg_hba.conf
(1?row)
postgres=#?show?config_file;
???????????????config_file
-----------------------------------------
?/etc/postgresql/14/main/postgresql.conf
(1?row)
-
使用新創建用戶登錄
#?pg數據庫默認連接的認證方式是peer。在postgres用戶下無法使用test數據庫用戶連接
postgres@ubuntu-svr:/home/test$?psql?-U?test?-d?postgres
psql:?error:?connection?to?server?on?socket?"/var/run/postgresql/.s.PGSQL.5432"?failed:?FATAL:??Peer?authentication?failed?for?user?"test"
#?在test系統用戶下使用test用戶連接數據庫
test@ubuntu-svr:~$?psql?-U?test?-d?postgres
psql?(16.3?(Ubuntu?16.3-1.pgdg22.04+1),?server?14.11?(Ubuntu?14.11-0ubuntu0.22.04.1))
Type?"help"?for?help.
postgres=#
說明:
Peer認證方法的工作原理是:從內核中獲取客戶的操作系統用戶名,并將其作為允許的數據庫用戶名(可選擇用戶名映射)。這種方法只支持本地連接。
-
修改認證方式
修改配置文件改為,將認證方式改為md5認證:
#?修改第二條local配置中的method為md5
test@ubuntu-svr:~$?sudo?vim?/etc/postgresql/14/main/pg_hba.conf
...
#?Database?administrative?login?by?Unix?domain?socket
local???all?????????????postgres????????????????????????????????peer
#?TYPE??DATABASE????????USER????????????ADDRESS?????????????????METHOD
#?"local"?is?for?Unix?domain?socket?connections?only
local???all?????????????all?????????????????????????????????????md5
...
#?重啟數據庫
test@ubuntu-svr:~$?sudo?systemctl?restart?postgresql
#?再次在postgres系統用戶下使用test數據庫用戶連接成功。
test@ubuntu-svr:~$?sudo?su?postgres
postgres@ubuntu-svr:/home/test$?psql?-U?test?-d?postgres
Password?for?user?test:
psql?(16.3?(Ubuntu?16.3-1.pgdg22.04+1),?server?14.11?(Ubuntu?14.11-0ubuntu0.22.04.1))
Type?"help"?for?help.
postgres=#
-
配置遠程連接
#?修改pg_hba.conf在最后位置增加如下配置,允許所有遠程主機的數據庫用戶使用md5認證進行連接。
test@ubuntu-svr:~$?sudo?vim?/etc/postgresql/14/main/pg_hba.conf
...
#?Allow?all?hosts?connections?via?md5:
host????all?????????????all?????????????0.0.0.0/0???????????????md5
...
#?修改postgresql.conf,修改listen_addresses為*,并取消注釋。port和最大連接數根據需求進行自定義。
test@ubuntu-svr:~$?sudo?vim?/etc/postgresql/14/main/postgresql.conf
...
?60?listen_addresses?=?'*'??????????#?what?IP?address(es)?to?listen?on;
?61?????????????????????????????????????????#?comma-separated?list?of?addresses;
?62?????????????????????????????????????????#?defaults?to?'localhost';?use?'*'?for?all
?63?????????????????????????????????????????#?(change?requires?restart)
?64?port?=?5432?????????????????????????????#?(change?requires?restart)
?65?max_connections?=?100???????????????????#?(change?requires?restart)
...
#?重啟數據庫
test@ubuntu-svr:~$?sudo?systemctl?restart?postgresql
#?在另外一臺機器上測試連接。確認已經關閉數據庫所在機器的防火墻或者放行規則。
[root@localhost?test]#?psql?-h?192.168.226.128?-p?5432?-U?test?-d?postgres
Password?for?user?test:
psql?(9.2.24,?server?14.11?(Ubuntu?14.11-0ubuntu0.22.04.1))
WARNING:?psql?version?9.2,?server?version?14.0.
?????????Some?psql?features?might?not?work.
SSL?connection?(cipher:?ECDHE-RSA-AES256-GCM-SHA384,?bits:?256)
Type?"help"?for?help.
2.2 在macOS上安裝Postgresql
-
使用Homebrew安裝:
brew?update
brew?install?Postgresql
-
啟動Postgresql服務:
brew?services?start?Postgresql
2.3 在Windows上安裝Postgresql
-
下載Postgresql:從Postgresql官方網站下載Windows版的Postgresql安裝包。
-
運行安裝程序:按照安裝向導的指示進行安裝,選擇默認設置即可。
-
初始化數據庫集群:安裝程序會自動初始化數據庫集群并啟動Postgresql服務。
3. Postgresql性能測試方案
為了測試Postgresql的性能,可以使用內置的基準測試工具pgbench
,或者第三方工具如sysbench
。
3.1 使用pgbench進行測試
pgbench
是Postgresql自帶的基準測試工具,可以模擬多種負載來測試數據庫性能。
-
初始化測試數據庫:
test@ubuntu-svr:~$?sudo?-u?postgres?createdb?pgbench
test@ubuntu-svr:~$?sudo?-u?postgres?pgbench?-i?-s?10?pgbench
dropping?old?tables...
creating?tables...
generating?data?(client-side)...
1000000?of?1000000?tuples?(100%)?done?(elapsed?0.47?s,?remaining?0.00?s)
vacuuming...
creating?primary?keys...
done?in?0.85?s?(drop?tables?0.02?s,?create?tables?0.01?s,?client-side?generate?0.48?s,?vacuum?0.18?s,?primary?keys?0.17?s).
這個命令會創建一個名為pgbench
的測試數據庫,并初始化測試數據,-s 10
表示生成10倍于默認規模的數據。
-
運行基準測試:
test@ubuntu-svr:~$?sudo?-u?postgres?pgbench?-c?10?-j?2?-T?60?pgbench
pgbench?(14.11?(Ubuntu?14.11-0ubuntu0.22.04.1))
starting?vacuum...end.
transaction?type:?<builtin:?TPC-B?(sort?of)>
scaling?factor:?10
query?mode:?simple
number?of?clients:?10
number?of?threads:?2
duration:?60?s
number?of?transactions?actually?processed:?311888
latency?average?=?1.924?ms
initial?connection?time?=?9.363?ms
tps?=?5198.015078?(without?initial?connection?time)
這個命令會以10個并發連接、2個線程運行測試,持續時間為60秒。
-
查看測試結果: pgbench
會輸出每秒事務數(tps)等性能指標。
3.2 使用sysbench進行測試
sysbench
是一個多線程基準測試工具,支持多種數據庫的性能測試,包括Postgresql。
-
安裝sysbench:
sudo?apt-get?install?sysbench
-
準備測試數據:
#?創建sysbench測試庫
test@ubuntu-svr:~$?sudo?-u?postgres?createdb?testdb
#?生成測試數據,根據環境替換pgsql用戶名和密碼
sysbench?--db-driver=pgsql?--pgsql-host=localhost?--pgsql-user=test?--pgsql-password=123456?--pgsql-db=testdb?--tables=10?--table-size=1000000?/usr/share/sysbench/oltp_read_write.lua?prepare
這個命令會在名為testdb
的數據庫中創建10個表,每個表包含1,000,000行數據。
-
運行基準測試:
sysbench?--db-driver=pgsql?--pgsql-host=localhost?--pgsql-user=test?--pgsql-password=123456?--pgsql-db=testdb?--tables=10?--table-size=1000000?--threads=10?--time=60?/usr/share/sysbench/oltp_read_write.lua?run
這個命令會以10個線程運行讀寫測試,持續時間為60秒。測試結果如下:
sysbench?1.0.20?(using?system?LuaJIT?2.1.0-beta3)
Running?the?test?with?following?options:
Number?of?threads:?10
Initializing?random?number?generator?from?current?time
Initializing?worker?threads...
Threads?started!
SQL?statistics:
????queries?performed:
????????read:????????????????????????????1190546
????????write:???????????????????????????340154
????????other:???????????????????????????170080
????????total:???????????????????????????1700780
????transactions:????????????????????????85039??(1417.01?per?sec.)
????queries:?????????????????????????????1700780?(28340.26?per?sec.)
????ignored?errors:??????????????????????0??????(0.00?per?sec.)
????reconnects:??????????????????????????0??????(0.00?per?sec.)
General?statistics:
????total?time:??????????????????????????60.0121s
????total?number?of?events:??????????????85039
Latency?(ms):
?????????min:????????????????????????????????????1.05
?????????avg:????????????????????????????????????7.05
?????????max:???????????????????????????????????63.49
?????????95th?percentile:???????????????????????13.46
?????????sum:???????????????????????????????599901.95
Threads?fairness:
????events?(avg/stddev):???????????8503.9000/116.42
????execution?time?(avg/stddev):???59.9902/0.00
-
清理測試數據
sysbench?--db-driver=pgsql?--pgsql-host=localhost?--pgsql-user=test?--pgsql-password=123456?--pgsql-db=testdb?--tables=10?--table-size=1000000?--threads=10?--time=60?/usr/share/sysbench/oltp_read_write.lua?cleanup
4. 優化Postgresql性能的建議
-
硬件優化:使用SSD存儲、增加內存和高性能CPU以提升I/O和計算能力。
-
配置優化:調整Postgresql配置文件
Postgresql.conf
中的參數,如shared_buffers
、work_mem
、maintenance_work_mem
、effective_cache_size
、checkpoint_segments
等。 -
索引優化:創建合適的索引以加快查詢速度,避免過多的索引影響寫性能。
-
查詢優化:使用EXPLAIN分析查詢計劃,優化SQL查詢以減少不必要的開銷。
-
連接池:使用連接池(如PgBouncer)來減少連接創建和銷毀的開銷,提高并發處理能力。
-
分區:對于大表,可以使用表分區來提高查詢性能和管理效率。
-
VACUUM和ANALYZE:定期運行VACUUM和ANALYZE命令以維護數據庫統計信息和清理垃圾數據,提高查詢性能。
通過合理的配置和優化,可以顯著提升Postgresql的性能,滿足高并發、低延遲的數據處理需求。
本文由 mdnice 多平臺發布