1.下载文件:https://ftp.postgresql.org/pub/source

选择需要的版本下载(本文以目前版本15.2为例)postgresql-15.2.tar.gz

2.解压:

tar -zxvf postgresql-15.2.tar.gz

3.安装依赖:

yum install -y bison flex readline-devel zlib-deve1 zlib zlib-devel gcc openssl-devel

4.创建用户和组并更改密码:

sudo groupadd -g 2023 postgres
sudo useradd -u 2023 -g postgres postgres
passwd postgres

5.创建使用目录:

[root@localhost ~]# mkdir -p /opt/postgresql15.2 -p

6.进行编译和安装:

[root@localhost postgresql-15.2]# cd /tmp/postgresql-15.2/
[root@localhost postgresql-15.2]# ./configure --prefix=/opt/postgresql15.2 --with-openssl
[root@localhost postgresql-15.2]# make
[root@localhost postgresql-15.2]# make install

7.授权目录及内容:

[root@localhost postgresql-15.2]# chown -R postgres. /opt/postgresql15.2

8.初始化:

[root@localhost postgresql-15.2]# sudo -iu postgres
postgres@localhost:~$ initdb -D /opt/postgresql15.2/data

9.启动数据库:

postgres@localhost:/opt/postgresql15.2/data$ pg_ctl -D /opt/postgresql15.2/data start

waiting for server to start....2023-04-13 22:07:29.326 CST [28999] LOG:  starting PostgreSQL 15.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit

2023-04-13 22:07:29.328 CST [28999] LOG:  listening on IPv6 address "::1", port 5432

2023-04-13 22:07:29.328 CST [28999] LOG:  listening on IPv4 address "127.0.0.1", port 5432

2023-04-13 22:07:29.331 CST [28999] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"

2023-04-13 22:07:29.334 CST [29002] LOG:  database system was shut down at 2023-04-13 22:00:25 CST

2023-04-13 22:07:29.339 CST [28999] LOG:  database system is ready to accept connections

 done

server started

注:可以配置环境变量,然后直接使用$PGDATA

vim /etc/profile
export PATH=/opt/postgresql15.2/bin:$PATH
export PGDATA=/opt/postgresql15.2/data
source /etc/profile

这样启动就可以使用如下命令

pg_ctl -D $PGDATA start

对于基础的搭建,至此已完成,如有其它请求,按情况进行修改。