軟件說(shuō)明
- 源碼包 : postgresql-11.3.tar.gz
- 客戶端 : postgresql11-11.3-1PGDG.rhel7.x86_64.rpm
- 附件模塊 : postgresql11-contrib-11.3-1PGDG.rhel7.x86_64.rpm
- 庫(kù)文件 : postgresql11-libs-11.3-1PGDG.rhel7.x86_64.rpm
- 服務(wù)器 : postgresql11-server-11.3-1PGDG.rhel7.x86_64.rpm
通過(guò) yum 源安裝
地址: PostgreSQL: Linux downloads (Red Hat family)
根據(jù)提示安裝 Yum 源
yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
安裝服務(wù)器和客戶端
yum install postgresql11
yum install postgresql11-server
初始化數(shù)據(jù)庫(kù)
/usr/pgsql-11/bin/postgresql-11-setup initdb
開啟自動(dòng)啟動(dòng)
systemctl enable postgresql-11
systemctl start postgresql-11
RPM 包直接下載
PostgreSQL RPM Repository (with Yum)
源碼包安裝
下載源碼秆乳,上傳懦鼠,并解壓
tar -zxvf postgresql-11.3.tar.gz
配置編譯選項(xiàng)
cd postgresql-11.3
./configure --help # 查看編譯選項(xiàng)
mkdir /home/pgdb/pgsql11
./configure --prefix=/home/pgdb/pgsql11
常用編譯選項(xiàng)(其中 4-7 只能在編譯時(shí)指定):
1. —prefix : 指定安裝目錄,默認(rèn) /usr/local/pgsql
2. —includedir : 指定 C 和 C++ 頭文件目錄屹堰,默認(rèn) <PREFIX>/include
3. —with-pgport : 指定實(shí)例的默認(rèn)監(jiān)聽端口肛冶,默認(rèn) 5432
4. —with-blocksize : 指定數(shù)據(jù)文件的塊大小,默認(rèn) 8K双藕, OLAP 建議增大到 32K
5. —with-segsize : 指定單個(gè)文件的大小淑趾,默認(rèn) 1GB
6. —with-wal-blocksize : 指定 WAL 文件的塊大小,默認(rèn) 8K
7. —with-wal-segsize : 指定單個(gè) WAL 文件的大小,默認(rèn) 16MB
編譯
gmake
gmake world # 一次性將文檔及附加慕課全部編譯
安裝
gmake install
gmake install-world
查看版本
/home/pgdb/pgsql11/bin/postgres --version
初始化數(shù)據(jù)目錄
initdb --help
initdb -D /home/pgdb/pgdata -E UTF8 -U pguser -W
or
pg_ctl -D /usr/local/pgsql/data initdb
The files belonging to this database system will be owned by user "pgdb".
This user must also own the server process.
The database cluster will be initialized with locale "zh_CN.UTF-8".
The default database encoding has accordingly been set to "UTF8".
initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8"
The default text search configuration will be set to "simple".
Data page checksums are disabled.
Enter new superuser password:
Enter it again:
fixing permissions on existing directory /home/pgdb/pgdata ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:
pg_ctl -D /home/pgdb/pgdata -l logfile start
啟動(dòng)服務(wù)器
pg_ctl start -D /home/pgdb/pgdata -l logfile
# 下面命令也可啟動(dòng)
postgres -D /usr/local/pgsql/data # Foreground
postgres -D /usr/local/pgsql/data >logfile 2>&1 & # Background
查看服務(wù)器狀態(tài)
pg_ctl status -D /home/pgdb/pgdata
pg_isready -p 5432
關(guān)閉服務(wù)器
pg_ctl stop -D /home/pgdb/pgdata -m fast
pg_ctl stop -D /home/pgdb/pgdata -mf
kill -sigterm `head -1 /home/pgdb/pgdata/postmaster.pid` # 不推薦使用
-m 參數(shù)指定關(guān)庫(kù)的模式:
* smart : -ms, SIGTERM, 等待活動(dòng)事務(wù)提交盾计,等待客戶端主動(dòng)斷開連接流炕,然后關(guān)閉數(shù)據(jù)庫(kù)
* fast : -mf, SIGINT, 回滾所有活動(dòng)事務(wù),強(qiáng)制斷開客戶端連接延蟹,然后關(guān)閉數(shù)據(jù)庫(kù)
* immediate : -mi, SIGQUIT, 立即終止所有服務(wù)器進(jìn)程,下次啟動(dòng)時(shí)首先進(jìn)入恢復(fù)狀態(tài)叶堆,不推薦使用
配置開機(jī)啟動(dòng)
cp /home/pgdb/postgresql-11.3/contrib/start-scripts/linux /etc/init.d/postgresql11
chmod +x postgresql11
cd /etc/init.d/
chkconfig --add postgresql11
chkconfig postgresql11 on
環(huán)境變量
export PGHOME=/home/pgdb/pgsql11
export PGDATA=/home/pgdb/pgdata # 可以在相關(guān)命令中阱飘,不用額外指定 -D 目錄參數(shù)
export PGUSER=puser
export PGPORT=5432
export LD_LIBRARY_PATH=$PGHOME/lib:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib
export MANPATH=$PGHOME/share/man:$MANPATH
export PATH=$PGHOME/bin:$PATH
服務(wù)器程序
- initdb : 用于創(chuàng)建新的數(shù)據(jù)庫(kù)目錄
- pg_archivecleanup : 清理 PG WAL 附檔文件
- pg_controldata : 顯示數(shù)據(jù)庫(kù)服務(wù)器的控制信息
- pg_ctl : 初始化,啟動(dòng),停止沥匈,控制數(shù)據(jù)庫(kù)服務(wù)器的工具
- pg_resetwal : 清除預(yù)寫日志蔗喂,并有選擇的重置 pg_control 中的一些信息,在控制文件損壞時(shí)最后手段
- pg_rewind : 在主備切換時(shí)高帖,將原 Master 通過(guò)同步模式恢復(fù)
- pg_test_timing : 度量系統(tǒng)計(jì)時(shí)開銷
- pg_upgrade : PG 升級(jí)工具
- pg_waldump : 將 WAL 日志解析為可讀的格式
- postgres : PG 服務(wù)器程序
- postmaster : 指向 postgres 服務(wù)器程序的一個(gè)軟連接