一 硫痰、 準備
1.安裝環(huán)境 :CentOS-7.6
2.Pg安裝包下載地址:http://www.postgresql.org/ftp/source/
3.
4.安裝方法:離線安裝
二效斑、安裝
1).?在root權限下創(chuàng)建用戶組 postgres 并創(chuàng)建用戶postgres?
Demo:
```
[root@localhost postgresql-12.0]# groupadd postgres
[root@localhost postgresql-12.0]# useradd -g postgres postgres
[root@localhost postgresql-12.0]#?password?postgres
```
設置linux系統(tǒng)中postgres用戶的密碼鳍悠。
2) .創(chuàng)建postgreSQL的數(shù)據(jù)存儲目錄,這個需要根據(jù)磁盤的空間大小選擇(可以使用df -hT?來看)敬矩。我這里放置在/pgsql下面測試蠢挡。
[root@localhost ~]mkdir?/pgsql
[root@localhost ~] chown -R postgres:postgres? ?/pgsql
3)使用postgres用戶重新登錄并上傳安裝包业踏。
將postgresql-12.0.tar.gz上傳至/home/postgres下勤家。
4).解壓壓縮包
[ postgres @localhost ~]#tar -zxvf postgresql-12.0.tar.gz?
5)進入解壓后的文件夾?
[ postgres@localhost ~]#cd postgresql-12.0?
6)編譯 postgresql 源碼
[ postgres@localhost postgresql-12.0]#?./configure --prefix=/pgsql
在編譯過程中可能會報錯,請根據(jù)提示下載相關依賴包:
注: 根據(jù)Centos的版本下載rpm依賴包并安裝热幔。因每個系統(tǒng)的版本和系統(tǒng)安裝模式不一樣绎巨,缺少的包也不一致,無法做到問題的統(tǒng)一解決戈锻。
rpm? ?-ivh? ?xxx.rpm? ? ? ? ? ? ? ? ? ? ? ? # rpm包的安裝指令格遭。缺少的安裝包需要自行下載并安裝窗价!(安裝時需要root權限撼港,需要先切過去)
注:可以去下面這些網(wǎng)址搜索缺少的rpm包。
http://rpm.pbone.net/
http://www.rpmfind.net/linux/rpm2html/search.php?query
https://pkgs.org/
如果在安裝缺包后往毡,依然報上述錯誤靶溜,請在 ./configure --prefix=/pgsql? 后加上 --without-zlib 的指令。(以zlib為例)
[postgres@localhost postgresql-12.0]# make? ? ?
[postgres@localhost postgresql-12.0]# make install
[postgres@localhost~]# cd?/pgsql
[postgres@localhost ~]# ls -al
bin??
include??
lib??
share
7).? ? 創(chuàng)建 postgresql 數(shù)據(jù)庫的數(shù)據(jù)主目錄并修改文件所有者這個數(shù)據(jù)庫主目錄是隨實際情況而不同罩息,這里我們的主目錄是在/pgsql /data 目錄下:
[ postgres@localhost pgsql]# mkdir data
[postgres@localhost pgsql]#?ls -al
8). 配置環(huán)境變量 進入/home/postgres 目錄可以看到.bash_profile 文件嗤详。
[ postgres@localhost pgsql]# cd?~
[ postgres@localhost]# vi .bash_profile?
添加以下內容。
export PGHOME=/pgsql
export PGDATA=/pgsql/data
PATH=$PATH:$HOME/bin:$PGHOME/bin
[ postgres@localhost ]# source .bash_profile?
9.)? ? 切換用戶到 postgres 并使用 initdb 初使用化數(shù)據(jù)庫
[postgres@localhost ~]# initdb?
10).? 配置服務
[postgres@localhost]$?cd /pgsql/postgresql/data
[postgres@localhost ]$ vi postgresql.conf?
listen_addresses= '*'?????????????????????? #允許所有的IP連接PG? ? ?
port= 5432? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #開放5432端口???????????
注:?生產(chǎn)環(huán)境中PG的參數(shù)還需要另行優(yōu)化瓷炮。此處省略葱色,如需要交流請聯(lián)系博主。
注:其中娘香,參數(shù)“l(fā)isten_addresses”表示監(jiān)聽的 IP 地址苍狰,默認是在 localhost 處監(jiān)聽,也就是 127.0.0.1 的 ip 地址上監(jiān)聽烘绽,只接受來自本機 localhost 的連接請求,這會讓遠程的主機無法登陸這臺數(shù)據(jù)庫安接,如果想從其他的機器上登陸這臺數(shù)據(jù)庫翔忽,需要把監(jiān)聽地址改為實際網(wǎng)絡的地址,一種簡單的方法是,將行開頭的#去掉歇式,把這個地址改為*矢赁,表示在本地的所有地址上監(jiān)聽。?
[postgres@localhost data]$ vi pg_hba.conf?
在最后一行 贬丛,添加如下內容。(所有IP均可以登錄PG且需要做md5驗證登錄)
host??? all???????????? all???????????? 0.0.0.0/0???????????????? md5
11). 設置 PostgreSQL 開機自啟動给涕。 PostgreSQL 的開機自啟動腳本位于contrib/start-scripts 路徑下豺憔,linux 文件即為 linux 系統(tǒng)上的啟動腳本 (root權限)
[root@localhost]$ cd?/home/postgres/postgresql-12.0/contrib/start-scripts
修改 linux 文件屬性,添加 X 可執(zhí)行權限
[root@localhost start-scripts]# chmod a+x linux
12) 復制 linux 文件到/etc/init.d 目錄下够庙,更名為postgresql
[root@localhost start-scripts]# cp linux? /etc/init.d/postgresql
修改/etc/init.d/postgresql 文件的兩個變量 prefix 和PGDATA
[root@localhost ]?$ vi /etc/init.d/postgresql
修改為 自己的安裝路徑:
prefix=/pgsql
PGDATA="/pgsql/data"
設置 postgresql 服務開機自啟動:?
[root@localhost ~]#?? cd /etc/init.d
[root@localhost init.d]# chkconfig --add postgresql
[root@localhost init.d]# chkconfig??
查看開機自啟動服務設置成功恭应。
postgresql? ? 0:關閉??? 1:關閉??? 2:啟用??? 3:啟用??? 4:啟用??? 5:啟用??? 6:關閉
13)執(zhí)行 service
postgresql start,啟動 PostgreSQL 服務
[root@localhost init.d]# service postgresql? start
Starting PostgreSQL: ok? ?# 表示啟動成功
現(xiàn)在就可以通過客戶端進行連接訪問數(shù)據(jù)庫耘眨!??
14)驗證
切換用戶:????
[root@localhost ~]#? su - postgres??
[ postgres@localhost ~]#? psql? ? ? ? ? ? ? ? ? ? ?## (如果失敗再次執(zhí)行source .bash_profile)
postgres=#??ALTER USER postgres WITH?PASSWORD '你的密碼';
修改postgres的密碼
最后可通過遠端可視化工具登錄測試昼榛。