1、下载源码包

https://ftp.postgresql.org/pub/source/v14.5/postgresql-14.5.tar.gz

2、安装所需的依赖包

使用操作系统用户 pg14 进行编译安装,软件安装目录为/home/pg14/soft,编译安装时需要开启 ssl 的支持

[root@cdh01 ~]# yum grouplist
Loaded plugins: product-id, search-disabled-repos
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Available Environment Groups:
   Minimal Install
   Infrastructure Server
   File and Print Server
   Basic Web Server
   Virtualization Host
   Server with GUI
Available Groups:
   Compatibility Libraries
   Console Internet Tools
   Development Tools
   Graphical Administration Tools
   Legacy UNIX Compatibility
   Scientific Support
   Security Tools
   Smart Card Support
   System Administration Tools
   System Management
Done
#挂载iso镜像,安装依赖包
[root@cdh01 pg14]# mount /dev/cdrom /mnt/disk/

[root@cdh01 pg14]# yum install -y readline readline-devel zlib zlib-devel openssl openssl-devel

[root@cdh01 ~]# useradd -m pg14

[root@cdh01 pg14]# su - pg14
[pg14@cdh01 ~]$tar xf postgresql-14.5.tar.gz

[pg14@cdh01 ~]$ cd postgresql-14.5

[pg14@cdh01 postgresql-14.5]$ ./configure --prefix=/home/pg14/soft --with-openssl
[pg14@cdh01 postgresql-14.5]$ make && make install

3、初始化数据库,启用checksum并配置免密登录

$PGDATA 目录是/home/pg14/data,PostgreSQ 数据库的超级用户是 postgres,postgres 用户的密码设置为 1qaz@WSX,并体现到.pgpass 文件中,以便postgres 免密登录,初始化时开启 checksum,监听端口号请设置为5666,然后启动数据库。

[pg14@cdh01 bin]$ ./initdb -Upostgres -W -D /home/pg14/data -k 
The files belonging to this database system will be owned by user "pg14".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are enabled.

Enter new superuser password: 1qaz@WSX
Enter it again: 1qaz@WSX

creating directory /home/pg14/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... PRC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: 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/pg14/data -l logfile start
vi /home/pg14/data/postgresql.conf

listen_addresses = '*'
port = 5666
[pg14@cdh01 ~]$ touch .pgpass
[pg14@cdh01 ~]$ vi .pgpass 

localhost:5666:pg14:postgres:1qaz@WSX
[pg14@cdh01 ~]$ chmod 0600 .pgpass
#vi  .bash_profile

PATH=$PATH:$HOME/.local/bin:$HOME/bin:/home/pg14/soft/bin
PGDATA=/home/pg14/data
export PGDATA
[pg14@cdh01 ~]$ pg_ctl start -D /home/pg14/data
waiting for server to start....2022-10-10 13:14:03.579 CST [34918] LOG:  starting PostgreSQL 14.5 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36), 64-bit
2022-10-10 13:14:03.580 CST [34918] LOG:  listening on IPv4 address "0.0.0.0", port 5666
2022-10-10 13:14:03.580 CST [34918] LOG:  listening on IPv6 address "::", port 5666
2022-10-10 13:14:03.580 CST [34918] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5666"
2022-10-10 13:14:03.582 CST [34919] LOG:  database system was shut down at 2022-10-10 13:13:56 CST
2022-10-10 13:14:03.583 CST [34918] LOG:  database system is ready to accept connections
 done
server started
[pg14@cdh01 ~]$ psql -Upostgres -p5666 postgres
psql (14.5)
Type "help" for help.
postgres=#

4、postgres配置开机启动

[root@cdh01 system]# vi /etc/systemd/system/postgresql.service 

[Unit]
Description=postgresql.service
After=network.target
[Service]
Type=forking
User=pg14
Group=pg14
WorkingDirectory=/home/pg14/soft
ExecStart=/home/pg14/soft/bin/pg_ctl start -D /home/pg14/data
ExecReload=/home/pg14/soft/bin/pg_ctl restart -D /home/pg14/data
ExecStop=/home/pg14/soft/bin/pg_ctl stop -D /home/pg14/data
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@cdh01 system]# systemctl enable postgresql.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql.service to /etc/systemd/system/postgresql.service.
[root@cdh01 system]# systemctl start postgresql.service 
[root@cdh01 system]# ps -ef|grep postgres
pg14       8610      1  0 10:30 ?        00:00:00 /home/pg14/soft/bin/postgres -D /home/pg14/data
pg14       8611   8610  0 10:30 ?        00:00:00 postgres: logger 
pg14       8612   8610  0 10:30 ?        00:00:00 postgres: startup recovering 000000020000000000000009
pg14       8613   8610  0 10:30 ?        00:00:00 postgres: checkpointer 
pg14       8614   8610  0 10:30 ?        00:00:00 postgres: background writer 
pg14       8615   8610  0 10:30 ?        00:00:00 postgres: stats collector 
pg14       8616   8610  0 10:30 ?        00:00:00 postgres: walreceiver streaming 0/9004650
root       8625   8051  0 10:30 pts/0    00:00:00 grep --color=auto postgres
[root@cdh01 system]# systemctl stop postgresql.service 
[root@cdh01 system]# ps -ef|grep postgres
root       8652   8051  0 10:30 pts/0    00:00:00 grep --color=auto postgres
-----------------------------------