PostgreSQL(后面簡稱pgsql),常用的也就三種安裝方式纯续,第一種為yum源直接下載安裝,第二種是二進(jìn)制包安裝拌蜘,第三種源碼編譯安裝杆烁,官方建議,在生產(chǎn)環(huán)境中简卧,安裝部署最好使用源碼的方式進(jìn)行安裝,可以更好的自定義一些參數(shù)和配置等烤芦。
官方源碼包下載地址:https://www.postgresql.org/ftp/source/
環(huán)境初始化
1.下載安裝包举娩,并安裝相關(guān)依賴
root # cd /opt
root # wget https://ftp.postgresql.org/pub/source/v14.13/postgresql-14.13.tar.gz
root # yum install -y perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++ openssl-devel cmake
2.創(chuàng)建postgres用戶和目錄,后期維護(hù)和登錄pgsql都需要在postgres用戶下進(jìn)行
root # groupadd postgres
root # useradd -g postgres postgres
root # mkdir /data/pgsql/base -p
root # mkdir /data/pgsql/data -p
編譯安裝pgsql
1.解壓安裝包构罗,編譯安裝pgsql
root # cd /opt
root # tar xf postgresql-14.13.tar.gz
root # cd postgresql-14.13/
root # ./configure --prefix=/data/pgsql/base
root # make && make install
2.對pgsql目錄進(jìn)行授權(quán)
root # chown postgres:postgres /data/pgsql/ -R
3.配置postgres用戶的環(huán)境變量
root # cd /home/postgres/
root # vim .bash_profile
...
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PATH
export PGHOME=/data/pgsql/base
export PGDATA=/data/pgsql/data
export PATH=$PATH:$HOME/bin:$PGHOME/bin
初始化pgsql實(shí)例
1.切換到postgres用戶下執(zhí)行初始化數(shù)據(jù)庫實(shí)例操作
root # su - postgres
postgres # initdb
編輯pgsql相關(guān)配置
1.修改pgsql啟動文件铜涉,postgresql.conf
psotgres # cd /data/pgsql/data/
psotgres # vim postgresql.conf
...
# Do not edit this file manually!
# It will be overwritten by Patroni!
listen_addresses = '*'
port = '5432'
data_directory = '/data/pgsql/data'
hba_file = '/data/pgsql/data/pg_hba.conf'
log_destination = 'stderr'
logging_collector = no
log_directory = '/data/pgsql/data'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
log_line_prefix = '%t-%d-%h-%a :'
shared_buffers = 128MB
max_connections = '100'
2.配置pgsql訪問控制文件,pg_hba.conf遂唧。
psotgres # cd /data/pgsql/data/
psotgres # vim pg_hba.conf
...
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 0.0.0.0/0 trust
...
配置pgsql啟動腳本
1.從pgsql安裝包中芙代,copy啟動腳本到/etc/init.d目錄。
root # cd /opt/postgresql-14.13/contrib/start-scripts
root # chmod +x linux
root # cp linux /etc/init.d/postgresql
2.編輯pgsql啟動腳本
root # vim /etc/init.d/postgresql
...
# Installation prefix
prefix=/data/pgsql/base
# Data directory
PGDATA="/data/pgsql/data"
# Who to run the postmaster as, usually "postgres". (NOT "root")
PGUSER=postgres
# Where to keep a log file
PGLOG="$PGDATA/serverlog"
...
3.將pgsql啟動腳本盖彭,加入service服務(wù)纹烹,管理postgresql啟動
root # chkconfig --add postgresql
啟動pgsql
1.pgsql啟動方式,因?yàn)榍懊鎴?zhí)行了chkconfig --add召边,所以systemctl也可以啟停pgsql铺呵。下面兩種方式選擇一種執(zhí)行,啟動即可隧熙。
#啟動方式1:
root # service postgresql start
#啟動方式2:
root # systemctl start postgresql
2.連接測試
root # su - postgres
postgres # psql
psql (14.13)
Type "help" for help.
postgres=#