PostgreSQL 數據庫源碼編譯安裝全流程詳解 Linux 8
- 1. 基礎環境配置
- 1.1 修改主機名
- 1.2 配置操作系統yum源
- 1.3 安裝操作系統依賴包
- 1.4 禁用SELINUX配置
- 1.5 關閉操作系統防火墻
- 1.6 創建用戶和組
- 1.7 建立安裝目錄
- 1.8 編輯環境變量
- 2. 源碼方式安裝(PG 16)
- 2.1 下載源碼安裝包
- 2.2 編譯安裝源碼包
- 2.3 初始化數據庫
- 2.4 啟動數據庫
- 2.5 開機自啟動
- 2.6 編輯環境變量
- 3. 安裝后配置
- 3.1 修改默認用戶postgres密碼
- 3.2 連接配置文件
- 3.3 數據庫啟動關閉
1. 基礎環境配置
1.1 修改主機名
hostnamectl set-hostname pgdb
# 配置hosts文件
cat >> /etc/hosts <<EOF
192.168.1.109 pgdb
EOF
1.2 配置操作系統yum源
創建掛載目錄
mkdir /mnt/iso
編輯yum源配置文件
cat > /etc/yum.repos.d/yum.repo<<EOF
[sourceOS]
name=yumserverOS
baseurl=file:///mnt/iso/BaseOS
gpgcheck=0
enabled=1[sourceAPP]
name=yumserverApp
baseurl=file:///mnt/iso/AppStream
gpgcheck=0
enabled=1
EOF
檢查yum源
yum clean all
yum makecache
yum repolist
1.3 安裝操作系統依賴包
yum install -y mke automake zlib zlib-devel bzip2 bzip2-devel bzip2-libs readline readline-devel gcc gcc-c++ bison ncurses ncurses-devel libaio-devel gmp gmp-devel mpfr mpfr-devel libmpc
1.4 禁用SELINUX配置
getenforce
setenforce 0
iptables -F
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
cat /etc/selinux/config
getenforcePermissive –輸出(表示臨時關閉)
Disabled –重啟服務器后輸出
1.5 關閉操作系統防火墻
systemctl stop firewalld.service
systemctl disable firewalld.service
systemctl status firewalld.service
1.6 創建用戶和組
# 創建pg用戶和組
groupadd -g 2000 postgres
useradd -g postgres -u 2000 postgres
echo "postgres123" | passwd --stdin postgres# 驗證創建成功
id postgres
1.7 建立安裝目錄
# 創建安裝目錄
mkdir -p /pgsql/app/pg16
mkdir -p /pgsql/soft
mkdir -p /pgdata/data/pg16
mkdir -p /pgdata/arch
chown -R postgres.postgres /pgsql
chown -R postgres.postgres /pgdata
chmod -R 775 /pgsql
chmod -R 775 /pgdata
1.8 編輯環境變量
# 編輯環境變量
vi /home/postgres/.bash_profile
export PGPORT=5432
export PGDATA=/pgdata/data/pg16
export PGHOME=/pgsql/app/pg16
export LANG=en_US.utf8
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH
export DATE=`date +"%Y%m%d%H%M"`
export PATH=$PGHOME/bin:$PATH:.
export MANPATH=$PGHOME/share/man:$MANPATH
export PGUSER=postgres
export PGDATABASE=postgres
2. 源碼方式安裝(PG 16)
2.1 下載源碼安裝包
官方下載地址:
https://www.postgresql.org/ftp/source/
[root@pgdb soft]# ll
total 31676
-rwxr-xr-x 1 root root 32433767 Mar 19 14:23 postgresql-16.1.tar.gz
2.2 編譯安裝源碼包
# 解壓源碼包
[root@pgdb soft]# tar -xvf postgresql-16.1.tar.gz
[root@pgdb soft]# cd postgresql-16.1
# 配置編譯選項 可以自定義端口號 默認5432
[root@pgdb postgresql-16.1]# ./configure --prefix=/pgsql/app/pg16 --with-pgport=5436
# 編譯源代碼
[root@pgdb postgresql-16.1]# make
# 編譯安裝
[root@pgdb postgresql-16.1]# make install
2.3 初始化數據庫
[postgres@pgdb ~]$ /pgsql/app/pg16/bin/initdb -D /pgdata/data/pg16
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".Data page checksums are disabled.fixing permissions on existing directory /pgdata/data/pg16 ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Shanghai
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... okinitdb: warning: enabling "trust" authentication for local connections
initdb: hint: 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 /pgdata/data/pg16 -l logfile start
2.4 啟動數據庫
[postgres@pgdb ~]$ pg_ctl -D /pgdata/data/pg16 -l logfile start
waiting for server to start.... done
server started
2.5 開機自啟動
- 添加配置文件 參考PG16 RPM安裝時配置文件
- Environment=PGDATA=/pgdata/data/pg16/
- ExecStart=/pgsql/app/pg16/bin/postgres -D ${PGDATA}
vi /usr/lib/systemd/system/postgresql-16.service
# It's not recommended to modify this file in-place, because it will be
# overwritten during package upgrades. It is recommended to use systemd
# "dropin" feature; i.e. create file with suffix .conf under
# /etc/systemd/system/postgresql-16.service.d directory overriding the
# unit's defaults. You can also use "systemctl edit postgresql-16"
# Look at systemd.unit(5) manual page for more info.# Note: changing PGDATA will typically require adjusting SELinux
# configuration as well.# Note: do not use a PGDATA pathname containing spaces, or you will
# break postgresql-16-setup.
[Unit]
Description=PostgreSQL 16 database server
Documentation=https://www.postgresql.org/docs/16/static/
After=syslog.target
After=network-online.target[Service]
Type=notifyUser=postgres
Group=postgres# Note: avoid inserting whitespace in these Environment= lines, or you may
# break postgresql-setup.# Location of database directory
#Environment=PGDATA=/var/lib/pgsql/16/data/
Environment=PGDATA=/pgdata/data/pg16/# Where to send early-startup messages from the server (before the logging
# options of postgresql.conf take effect)
# This is normally controlled by the global default set by systemd
# StandardOutput=syslog# Disable OOM kill on postgres main process
OOMScoreAdjust=-1000
Environment=PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj
Environment=PG_OOM_ADJUST_VALUE=0#ExecStartPre=/usr/pgsql-16/bin/postgresql-16-check-db-dir ${PGDATA}
ExecStart=/pgsql/app/pg16/bin/postgres -D ${PGDATA}
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=SIGINT# Do not set any timeout value, so that systemd will not kill postgres
# main process during crash recovery.
TimeoutSec=0# 0 is the same as infinity, but "infinity" needs systemd 229
TimeoutStartSec=0TimeoutStopSec=1h[Install]
WantedBy=multi-user.target
- 重新加載配置文件 systemctl start postgresql-16.service 不會結束命令,systemctl status postgresql-16.service Active: activating (start)顯示狀態非running
# reload配置文件
[root@pgdb ~]# systemctl daemon-reload
# 啟動數據庫
[root@pgdb ~]# systemctl start postgresql-16.service
# 開機自啟動
[root@pgdb ~]# systemctl enable postgresql-16.service
Created symlink /etc/systemd/system/multi-user.target.wants/postgresql-16.service → /usr/lib/systemd/system/postgresql-16.service.
# pg14 資源狀態
[root@pgdb ~]# systemctl status postgresql-16.service
● postgresql-16.service - PostgreSQL 16 database serverLoaded: loaded (/usr/lib/systemd/system/postgresql-16.service; enabled; vendor preset: disabled)Active: activating (start) since Wed 2025-03-19 16:21:17 CST; 3min 33s agoDocs: https://www.postgresql.org/docs/16/static/Main PID: 1062 (postgres)Tasks: 6 (limit: 37638)Memory: 11.9MCGroup: /system.slice/postgresql-16.service├─1062 /pgsql/app/pg16/bin/postgres -D /pgdata/data/pg16/├─1067 postgres: checkpointer ├─1068 postgres: background writer ├─1070 postgres: walwriter ├─1071 postgres: autovacuum launcher └─1072 postgres: logical replication launcher Mar 19 16:21:17 pgdb systemd[1]: Starting PostgreSQL 16 database server...
Mar 19 16:21:17 pgdb postgres[1062]: 2025-03-19 16:21:17.178 CST [1062] LOG: starting PostgreSQL 16.1 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 8.4.1 20200928 (Re>
Mar 19 16:21:17 pgdb postgres[1062]: 2025-03-19 16:21:17.179 CST [1062] LOG: listening on IPv6 address "::1", port 5436
Mar 19 16:21:17 pgdb postgres[1062]: 2025-03-19 16:21:17.179 CST [1062] LOG: listening on IPv4 address "127.0.0.1", port 5436
Mar 19 16:21:17 pgdb postgres[1062]: 2025-03-19 16:21:17.180 CST [1062] LOG: listening on Unix socket "/tmp/.s.PGSQL.5436"
Mar 19 16:21:17 pgdb postgres[1062]: 2025-03-19 16:21:17.184 CST [1069] LOG: database system was shut down at 2025-03-19 16:19:41 CST
Mar 19 16:21:17 pgdb postgres[1062]: 2025-03-19 16:21:17.187 CST [1062] LOG: database system is ready to accept connections
2.6 編輯環境變量
#PG 16
######
export PGPORT=5436
export PGDATA=/pgdata/data/pg16
export PGHOME=/pgsql/app/pg16
3. 安裝后配置
3.1 修改默認用戶postgres密碼
postgres=# alter user postgres with password 'postgres';
ALTER ROLE
3.2 連接配置文件
- postgresql.conf參數 監聽所有地址IP
[postgres@pgdb pg16]$ cd $PGDATA
[postgres@pgdb pg16]$ vi postgresql.conf
#listen_addresses = 'localhost' # what IP address(es) to listen on; #重啟生效
listen_addresses = '*'
- pg_hba.conf參數 實例訪問控制
[postgres@pgdb pg16]$ cd $PGDATA
[postgres@pgdb pg16]$ vi pg_hba.conf
# IPv4 local connections:
#host all all 127.0.0.1/32 trust #重加載配置生效
# 允許192.168.1.0網段連接數據庫
host all all 192.168.1.0/24 scram-sha-256
- 重啟數據庫
[postgres@pgdb ~]$ pg_ctl restart
- 遠程連接數據庫
[postgres@pgdb ~]$ psql -h 192.168.1.109 -p 5436 -U postgres -d postgres
3.3 數據庫啟動關閉
[postgres@pgdb ~]$ pg_ctl status
pg_ctl: server is running (PID: 1063)
/pgsql/app/pg16/bin/postgres "-D" "/pgdata/data/pg16/"
[postgres@pgdb ~]$ pg_ctl stop
waiting for server to shut down.... done
server stopped
[postgres@pgdb ~]$ pg_ctl start
waiting for server to start....2025-03-21 22:24:33.069 CST [1335] LOG: redirecting log output to logging collector process
2025-03-21 22:24:33.069 CST [1335] HINT: Future log output will appear in directory "log".done
server started
[postgres@pgdb ~]$ pg_ctl status
pg_ctl: server is running (PID: 1335)
/pgsql/app/pg16/bin/postgres