1、进入官网下载所需版本
https://www.postgresql.org/download/linux/redhat/
2、开始安装
yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm -y

yum install postgresql12 -y
yum install postgresql12-server -y
3、数据库初始化与设置自启动
/usr/pgsql-12/bin/postgresql-12-setup initdb
systemctl enable postgresql-12
systemctl start postgresql-12

这一步初始化数据库命令会在 /var/lib/pgsql 目录下创建名称为12文件夹,12为数据库版本。这里如果已经有对应版本的文件夹了,初始化这一步会报错,需要你自行去删除对应的文件夹再去初始化。
4、默认会创建一个名为postgres的linux登录用户,这里进行密码修改

5、修改配置文件

5.1修改postgresql.conf
修改:#listen_addresses = 'localhost'为listen_addresses = '*'

5.2修改pg_hba.conf
修改前

修改后

并重新启动
systemctl restart postgresql-12
6、设置登录数据库账密
su postgres #不能使用root用户登录,切换到普通用户
psql -U postgres
\password #设置用户密码
select version(); #查看版本

7、登录并测试数据库连接

