在编译安装 PostgreSQL 之前,我们需要先安装一些依赖组件,否则编译的时候会因为缺少这些依赖包导致安装失败。
[root@localhost src]# yum -y install glibc* zlib* openssl* gcc* readline*
解压并编译安装 PostgreSQL 14.14
[root@localhost src]# tar -zxvf postgresql-14.14.tar.gz
[root@localhost src]# cd postgresql-14.14/
[root@localhost postgresql-14.14]# ./configure --prefix=/usr/local/pgsql-14
[root@localhost postgresql-14.14]# make world
[root@localhost postgresql-14.14]# make install-world
添加 postgres 用户
[root@localhost postgresql-14.14]# groupadd postgres
[root@localhost postgresql-14.14]# useradd -d /home/postgres -g postgres postgres
创建数据库的数据文件目录并修改其权限
[root@localhost postgresql-14.14]# mkdir -p /usr/local/pgsql-14/data
[root@localhost postgresql-14.14]# chown -R postgres:postgres /usr/local/pgsql-14/
配置环境变量
[root@localhost postgresql-14.14]# vi /etc/profile
export PGHOME=/usr/local/pgsql-14
export PGDATA=/usr/local/pgsql-14/data
export PATH="$PGHOME/bin:$PATH"
export MANPATH=$PGHOME/share/man:$MANPATH
export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH
[root@localhost postgresql-14.14]# source /etc/profile
[root@localhost postgresql-14.14]# su - postgres
[postgres@localhost ~]$ vi .bash_profile
export PGHOME=/usr/local/pgsql-14
export PGDATA=/usr/local/pgsql-14/data
export PATH="$PGHOME/bin:$PATH"
export MANPATH=$PGHOME/share/man:$MANPATH
export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH
[postgres@localhost ~]$ source .bash_profile
初始化数据库
[postgres@localhost ~]$ cd /usr/local/pgsql-14/data/
[postgres@localhost data]$ ../bin/initdb -D /usr/local/pgsql-14/data/
启动数据库
[postgres@localhost data]$ ../bin/pg_ctl -D /usr/local/pgsql-14/data/ -l logfile start
修改数据库用户密码
[postgres@localhost data]$ psql
postgres=# alter user postgres with password 'Anolis@2025';
修改配置文件
[postgres@localhost data]$ vi pg_hba.conf
[postgres@localhost data]$ vi postgresql.conf
重启数据库服务
[postgres@localhost data]$ ../bin/pg_ctl restart -D /usr/local/pgsql-14/data/
说明:在修改完配置文件之后,我们可以选择重载配置文件,特别是关于 pg_hba.conf 文件的修改。而关于 postgresql.conf 文件的修改,有一些参数是需要通过重启数据库服务才生效的,所以我们一般选择重启来完成。
配置免密登录
[postgres@localhost data]$ cd ~
[postgres@localhost ~]$ vi .pgpass
127.0.0.1:5432:*:postgres:Anolis@2025
[postgres@localhost ~]$ chmod 0600 ./.pgpass
说明:这个文件的格式是 hostname:port:database:username:password
验证免密登录
[postgres@localhost ~]$ psql -h 127.0.0.1 -U postgres
[postgres@localhost ~]$ psql -h 192.168.30.140 -U postgres
Password for user postgres:
说明:这里 192.168.30.140 是我本机的实体 IP 地址,可以从上面的测试中看到,在 127.0.0.1 的情况下是不需要输入密码的,但是如果换成实体的 IP 是需要输入密码的,那么如果在 .pgpass 中也配置了本机实体的 IP,再次登录是否也需要密码呢?
这个测试可以自己验证一下。