1.準備安裝源 下載地址: "https://www.postgresql.org/ftp/source/" 下載並解壓。 2.軟體編譯安裝 配置、檢查安裝環境 成功後,方可進入下一步。遇到問題參考 "[configure遇到的問題]" 編譯安裝 3.配置資料庫 內核參數配置 用戶配置 初始化資料庫 ...
1.準備安裝源
下載地址:https://www.postgresql.org/ftp/source/
下載並解壓。
2.軟體編譯安裝
配置、檢查安裝環境
./configure
--prefix=/opt/postgre94
--with-pgport=5432
--with-perl
--with-tcl
--with-python
--with-openssl
--with-pam
--without-ldap
--with-libxml
--with-libxslt
--enable-thread-safety
--with-wal-blocksize=16
--with-blocksize=16
--enable-dtrace
--enable-debug
成功後,方可進入下一步。遇到問題參考[configure遇到的問題]
編譯安裝
gmake world
gmake check-world (這個需要使用普通用戶執行,可選,耗時較長)
gmake install-world
3.配置資料庫
內核參數配置
# vi /etc/sysctl.conf #追加下麵內容
kernel.shmmni = 4096
kernel.sem = 50100 64128000 50100 1280
fs.file-max = 7672460
net.ipv4.ip_local_port_range = 9000 65000
net.core.rmem_default = 1048576
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 104857
# sysctl -p #使配置成效
用戶配置
# useradd postgres
# passwd postgres
# su - postgres
> vi .bash_profile #追加下麵內容
export PGPORT=5432
export PGDATA=/pgdata/pg_root
export LANG=en_US.utf8
export PGHOME=/opt/postgre94
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH
export PATH=$PGHOME/bin:$PATH:.
export MANPATH=$PGHOME/share/man:$MANPATH
export PGUSER=postgres
export PGHOST=$PGDATA
初始化資料庫
(配置一個資料庫實例)
# su - postgres
> initdb -D $PGDATA -E UTF8 --locale=C -U postgres -W
-U 為指定管理員用戶
-W 輸入管理員密碼
-D 資料庫目錄
配置資料庫訪問許可權
# vi $PGDATA/pg_hba.conf
host all all 192.168.25.0/24 md5
# vi $PGDATA/postgresql.conf
listen_addresses = '*'
如果要在外網訪問資料庫,需要關閉防火牆或者配置iptables,參見Linux>配置iptables
4.基本操作
起停服務
> pg_ctl start/stop -D PGDATA
查看運行情況
#監聽埠情況
> netstat -auntp | grep 5432
#進程情況
> ps -ef | grep postgres
連接資料庫
> psql -h localhost postgres
postgres=# \s #查看資料庫
5.同一主機多個postgresql資料庫
之前的第2步編譯和安裝,安裝的是postgresql這個軟體,裡面主要是postgresql軟體的可執行文件,比如:初始化資料庫、恢複數據庫和psql等。
如果需要在同一主機上同時跑多個資料庫實例,只需要再配置用戶,初始化資料庫及配置就可,註意的是每個實例監聽的埠應該都不一樣。