CentOS 7 安装&配置PostgreSQL 12(附图片详
2022-03-14 本文已影响0人
陈煦缘
linux版本:CentOS Linux release 7.6.1810 (Core)
PostgreSQL版本:12.x
1、导入yum源
[root@localhost java]# yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
2、安装PostgreSQL服务
[root@localhost java]# yum install -y postgresql12 postgresql12-server
3、初始化数据库
[root@localhost java]# /usr/pgsql-12/bin/postgresql-12-setup initdb

4、启动PostgreSQL服务并设置为开机启动
启动pg服务
[root@localhost java]# systemctl start postgresql-12
设置pg服务为开机启动
[root@localhost java]# systemctl enable postgresql-12

5、进入PostgreSQL命令行
[root@localhost java]# su postgres

6、启动SQL Shell
bash-4.2$ psql

7、修改密码
postgres=# ALTER USER postgres WITH PASSWORD '***newpassword***';

8、配置远程访问
[root@localhost ~]# netstat -anp | grep 5432
-bash: netstat: 未找到命令
[root@localhost ~]# yum install net-tools
再次执行
[root@localhost ~]# netstat -anp | grep 5432

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

开放端口
[root@localhost ~]# firewall-cmd --add-port=5432/tcp --permanent
[root@localhost ~]# firewall-cmd –reload

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

【解决办法】
修改/usr/bin/firewall-cmd 下文件在首行 python改为python2(因为之前安装过python3环境)
[root@localhost ~]# vi /usr/bin/firewall-cmd

修改配置文件
[root@localhost ~]# vi /var/lib/pgsql/12/data/postgresql.conf
将监听地址修改为*
默认listen_addresses配置是注释掉的,所以可以直接在配置文件开头加入该行
listen_addresses='*'

修改配置文件允许所有IP访问
[root@localhost ~]# vi /var/lib/pgsql/12/data/pg_hba.conf
在文件尾部加入
host all all 0.0.0.0/0 md5

或者加 ip网段

重启PostgreSQL服务
[root@localhost ~]# systemctl restart postgresql-12
配置完成后即可使用客户端进行连接

9、简单命令操作

大功告成~~
————————————————
参考 链接:https://blog.csdn.net/weixin_40983094/article/details/107956271