項目使用pg,整理個初級的安裝教程,簡化實施。
部分内容參考來源:
https://www.jianshu.com/p/ba02513d4a24Postgresql RPM離線安裝
1. 安裝環境
RedHat 6.8/ 64位, 有ROOT權限
[TOC]
2. 準備工作
下載下傳RPM包
通路PostgreSQL網站,根據系統類别,系統版本選擇相關的包:
https://yum.postgresql.org/9.6/redhat/rhel-6-x86_64/repoview/postgresqldbserver96.group.html下載下傳本頁面下的全部檔案:
點選下載下傳連結:
https://yum.postgresql.org/9.6/redhat/rhel-6-x86_64/postgresql96-9.6.10-1PGDG.rhel6.x86_64.rpm https://yum.postgresql.org/9.6/redhat/rhel-6-x86_64/postgresql96-contrib-9.6.10-1PGDG.rhel6.x86_64.rpm https://yum.postgresql.org/9.6/redhat/rhel-6-x86_64/postgresql96-libs-9.6.10-1PGDG.rhel6.x86_64.rpm https://yum.postgresql.org/9.6/redhat/rhel-6-x86_64/postgresql96-server-9.6.10-1PGDG.rhel6.x86_64.rpm目錄檔案:
postgresql96-9.6.10-1PGDG.rhel6.x86_64.rpm
postgresql96-contrib-9.6.10-1PGDG.rhel6.x86_64.rpm
postgresql96-libs-9.6.10-1PGDG.rhel6.x86_64.rpm
postgresql96-server-9.6.10-1PGDG.rhel6.x86_64.rpm
本教程編寫時pg目前版本9.6.10. 在這裡以postgresql 9.6.10版本安裝為例。
3. 安裝PG
将上述Postgres安裝包放置在同一個檔案夾中, 執行安裝指令:
rpm -ivh postgresql96-*.rpm
通過RPM包安裝, Postgres安裝程式會在/var和/usr下建立檔案夾.
/var/lib/pgsql
: 用于存放Postgres資料庫預設的資料檔案夾
/usr/pgsql-9.6
: 用于存放Postgres資料庫的指令、依賴庫及文檔目錄等資訊
/var/lib/pgsql/9.6/data
: 使用者存放PG預設配置的
pg_hba.conf,postgresql.conf
配置檔案。(為什麼預設安裝不是在/etc呢?)
可選操作:
為了一些後期運維資料庫維護簡化操作,将postgresql 加入系統PATH,并測試postgres 指令是否有傳回,測試安裝是否成功。
vi /etc/profile
# 在檔案最後加入以下語句
export PATH=$PATH:/usr/pgsql-9.6/bin
source /etc/profile
postgres --version
# 傳回 postgres (PostgreSQL) 9.6.10
4. 自定義資料存儲目錄(可選)
在實際生産部署中,可能資料存儲路徑需要單獨定義。如要求放到:/data/postgres目錄,以友善定期資料備份,與磁盤空間管理。
這裡以将postgresql的data資料存儲到
/data/postgres
路徑為例。
A 建立檔案
修改目錄權限,需要将權限配置給使用者postgres使用者,(postgres使用者為安裝postgres的時候建立的使用者)
mkdir -p /data/postgres
chown -R postgres:postgres /data/postgres
B 修改PG啟動腳本目錄(可選)
編輯
/etc/init.d/postgresql-9.6
檔案, 找到PGDATA變量,設定成/data/postgres。
...
# Set defaults for configuration variables
PGENGINE=/usr/pgsql-9.6/bin
#PGDATA=/var/lib/pgsql/9.6/data -- PG預設配置的路徑,修改成/data/postgres。
PGDATA=/data/postgres
PGLOG=/var/lib/pgsql/9.6/pgstartup.log
# Log file for pg_upgrade
PGUPLOG=/var/lib/pgsql/$PGMAJORVERSION/pgupgrade.log
...
這裡也顯示PG預設的一些log路徑。
5. 初始化資料庫
配置好資料後(如果配置了自定義路徑)資料庫預設是未啟動狀态。 需要進行初始化。
CentOS 6/Redhat 6
service postgresql-9.6 initdb
初始化資料庫後,會預設在系統的目錄裡面初始化PG資料庫
如果自定義了目錄,會在/data/postgres裡面初始化檔案,并生成pg_hba.conf,postgresql.conf
預設情況:
/var/lib/pgsql: 用于存放Postgres資料庫預設的資料檔案夾
/usr/pgsql-9.6: 用于存放Postgres資料庫的指令、依賴庫及文檔目錄等資訊
資料庫隻能本機通路,127.0.0.1 預設沒有密碼
/var/lib/pgsql/9.6/data : 使用者存放PG預設配置的
配置檔案
pg_hba.conf,postgresql.conf
5. 配置資料庫遠端通路
PG預設隻能本機通路,但是實際情況中,應用伺服器多單獨部署,需要開通PG的遠端通路權限,且是需要配置使用者密碼的。
需要修改postgres.conf, pg_hba.conf檔案。
如果找不到檔案在哪,使用 Find指令搜尋:
find / -name postgresql.conf
# /usr/pgsql-9.6/share/postgresql.conf
# /var/lib/pgsql/9.6/data/postgresql.conf
5.1 修改postgresql.conf
主要配置參數詳解:
#listen_addresses='localhost'
listen_addresses='*' --- 修改成'*'全部ip都可以通路改資料庫。
其他參數:
Postgresql監聽的網卡ip,預設僅僅本地,可以配置多個,使用“,”分割。“*” 代表所有的網卡ip
port=5432
Postgres服務端口
max_connections=100
最大伺服器連接配接數
superuser_reserved_connections=3
為管理者保留的專用連接配接數,普通使用者無法使用這些連接配接,不能大于max_connections
authentication_timeout=60s
登入驗證逾時時間設定
ssl=false
是否使用SSL進行連接配接
password_encryption=true
當使用create user、alter user管理使用者時,如果沒有顯示進行加密與否的限定,postgresql伺服器是否自動進行密碼密
shared_buffers=32m
共享緩存,非常重要的性能參數,其最小值為(128k,16k*max_connections)
max_prepared_transactions=5
最大并行prepared 事務,如果為0,則禁止使用prepared事務,最大值與max_connections相同
temp_buffers=8m
每個會話可以使用的臨時(表)緩存大小
work_mem=1m
指定内部排序、連接配接、group等等時,postgresql可以使用的記憶體大小,超過該值,将使用磁盤臨時檔案; 實際使用的記憶體該類操作同時執行的數目相乘
maintenance_work_men=16m
維護語句vacuum、create index等等可以使用的記憶體大小; 實際使用的記憶體和該類操作同時執行的數目相乘
fsync=on
(實體資料)日志必須同步寫入磁盤
可能導緻嚴重的性能損失,卻能確定最高的日志資料安全。
synchronous_commit=on
(邏輯事務資料)日志必須同步寫入磁盤,如果設為on,會立即調用fsync,相當于設定了fsync=on
full_page_writes=on
寫整頁
wal_buffers=64K
WAL日志緩存大小
wal_writer_delay=200ms
将wal日志從wal_buffer中寫入磁盤的時間周期
commit_delay=0ms
事務日志commit後,寫入磁盤的延時。這個設定隻有在commit_sibings(并行的多個排隊事務)在延時記憶體在是才有效
commit_siblings=5
并行事務數
5.2 修改pg_hba.conf
[root@pgserver ~]# vi pg_hba.conf
... ...
·"local" is for Unix domain socket connections only
local all all trust
IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 0.0.0.0/0 md5 #-- 添加本行
添加
host all all 0.0.0.0/0 md5
意思是:
【主機】可以使用 【全部】資料庫 , 【全部】 使用者,使用IP【0.0.0.0】,通過MD5加密密碼通路。
連接配接資料庫是需要密碼的。
配置說明:
格式:TYPE DATABASE USER ADDRESS METHOD
參數:
TYPE: 值為local和host, Local值表示為主機Socket連接配接, host代表允許的主機位址連接配接
DATABASE: 允許通路的資料庫名, all代表允許全部資料庫
USER: 表示允許哪個使用者通路資料庫, all代表所有使用者都可以通路
ADDRESS: 表示允許連接配接的主機資訊,可以使用主機IP位址, 也可以使用網段來表示,如192.168.1.0/24表示192.168.1.0網段可以連接配接
METHOD: 連接配接方法, 通常使用的值為md5和trust
6. 啟動資料庫
啟動Postgres資料庫
service postgresql-9.6 start
#Starting postgresql-9.6 service: [ OK ]
7. 測試資料庫
PG模式是不能用root使用者通路資料庫,使用時需要用
su postgres
切換到postgres使用者下。
使用psql指令進行測試。
psql指令詳解:
-h host, 指定連接配接的Postgres資料庫IP位址
-U username: 指定連接配接資料庫的使用者名
-d database: 指定連接配接的資料庫名
-p port: 指定資料庫連接配接的服務端口
-w: 表示不提示使用者輸入密碼
-W : 表示驗證資料庫使用者密碼
-l : 表示列出Postgres可用的資料庫資訊
示例如下:
su - postgres # -- root 切換到postgres
psql -h localhost -U postgres -d postgres -W
#Password for user postgres:
#psql (9.6.10)
#Type "help" for help.
#
使用預設腳本service postgresql initdb或/usr/pgsql-9.6/bin/postgresql96-setup initdb初始化資料庫時,需要修改使用者的密碼。在修改Postgres使用者密碼時,需要確定以下條件:
配置"pg_hba.conf"時需要确定伺服器本位址的驗證方法為trust, 即:
host all all 127.0.0.1/32 trust
如果為peer|indent|md5方法時,需要将其修改為trust, 否則登入時會提醒輸入使用者密碼。
修改預設的postgres使用者密碼
切換系統使用者為postgres, 通過psql -h localhost -U postgres -d postgres登入Postgres資料庫,進入資料後,使用SQL
alter user postgres with password 'newpassword'
來為使用者修改預設密碼。
su - postgres # -- root 切換到postgres
psql -h localhost -U postgres -d postgres -w
postgres=# alter user postgres with password 'newpassword';
#ALTER ROLE
8. PG開機自啟動
postges預設不自動啟動,使用以下指令打開。
chkconfig postgresql-9.6 on
9. Postgres日常服務管理
啟動資料庫:pg_ctl start -D /data/postgres
重新開機資料庫: pg_ctl restart -D /data/postgres
停止資料庫:pg_ctl stop -D /data/postgres
強制重新開機:pg_ctl restart -D /data/postgres -m f
強制停止:pg_ctl stop -D /data/postgres -m f
加載配置:pg_ctl reload -D /data/postgres
顯示服務狀态:pg_ctl status -D /data/postgres
連接配接資料庫: psql -h 127.0.0.1 -U postgres -p 5432 -d postgres -W