CentOS 7 安装&配置PostgreSQL 12(附图片详解)

linux版本:CentOS Linux release 7.6.1810 (Core)

PostgreSQL版本:12.x

1、导入yum

[[email protected] java]# yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

2、安装PostgreSQL服务

[[email protected] java]# yum install -y postgresql12 postgresql12-server

3、初始化数据库

[[email protected] java]# /usr/pgsql-12/bin/postgresql-12-setup initdb

CentOS 7 安装&配置PostgreSQL 12(附图片详解)

4、启动PostgreSQL服务并设置为开机启动

#启动pg服务

[[email protected] java]# systemctl start postgresql-12

#设置pg服务为开机启动

[[email protected] java]# systemctl enable postgresql-12

 

CentOS 7 安装&配置PostgreSQL 12(附图片详解)

5、进入PostgreSQL命令行

[[email protected] java]# su postgres

CentOS 7 安装&配置PostgreSQL 12(附图片详解)

6、启动SQL Shell

bash-4.2$ psql

CentOS 7 安装&配置PostgreSQL 12(附图片详解)

7、修改密码

postgres=# ALTER USER postgres WITH PASSWORD '***newpassword***';

CentOS 7 安装&配置PostgreSQL 12(附图片详解)

8、配置远程访问

[[email protected] ~]# netstat -anp | grep 5432

-bash: netstat: 未找到命令

[[email protected] ~]# yum install net-tools

再次执行

[[email protected] ~]# netstat -anp | grep 5432

CentOS 7 安装&配置PostgreSQL 12(附图片详解)

使用navicat连接是无法连接的。

CentOS 7 安装&配置PostgreSQL 12(附图片详解)

#开放端口

[[email protected] ~]# firewall-cmd --add-port=5432/tcp --permanent

[[email protected] ~]# firewall-cmd –reload

CentOS 7 安装&配置PostgreSQL 12(附图片详解)

【遇到的问题】若没有可以跳过

CentOS 7 安装&配置PostgreSQL 12(附图片详解)

【解决办法】

修改/usr/bin/firewall-cmd 下文件在首行 python改为python2(因为之前安装过python3环境)

[[email protected] ~]# vi /usr/bin/firewall-cmd

CentOS 7 安装&配置PostgreSQL 12(附图片详解)

#修改配置文件

[[email protected] ~]# vi /var/lib/pgsql/12/data/postgresql.conf

#将监听地址修改为*

#默认listen_addresses配置是注释掉的,所以可以直接在配置文件开头加入该行

listen_addresses='*'

CentOS 7 安装&配置PostgreSQL 12(附图片详解)

#修改配置文件允许所有IP访问

[[email protected] ~]# vi /var/lib/pgsql/12/data/pg_hba.conf

#在文件尾部加入

host  all  all 0.0.0.0/0 md5

CentOS 7 安装&配置PostgreSQL 12(附图片详解)

或者加 ip网段

CentOS 7 安装&配置PostgreSQL 12(附图片详解)

#重启PostgreSQL服务

[[email protected] ~]# systemctl restart postgresql-12

配置完成后即可使用客户端进行连接

CentOS 7 安装&配置PostgreSQL 12(附图片详解)

9、简单命令操作

CentOS 7 安装&配置PostgreSQL 12(附图片详解)

大功告成~~

 

参考链接:

https://www.postgresql.org/download/linux/redhat/

https://www.runoob.com/postgresql/linux-install-postgresql.html