天天看点

记一次 Centos7.4 使用 yum 安装 PostgreSql v11

一、基础信息说明

官网:https://www.postgresql.org/

下载说明地址:https://www.postgresql.org/download/linux/redhat/

中文社区 http://www.postgres.cn/index.php/v2/home

中文网 https://postgres.fun/

易百教程 https://www.yiibai.com/postgresql

二、系统、工具说明

1、系统版本 Centos7.4    CentOS-7-x86_64-Minimal-1804

下载地址:  http://archive.kernel.org/centos-vault/7.4.1708/isos/x86_64/   

2、VMware 版本:VMware Workstation Pro15

虚拟机安装过程可参考:https://blog.csdn.net/llwy1428/article/details/89328381

3、工具:xshell5

三、安装、部署

1、配置虚拟机网络,每台虚拟机均接入互联网

参考:

https://blog.csdn.net/llwy1428/article/details/85058028

2、设置静态IP

[[email protected] ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
           
记一次 Centos7.4 使用 yum 安装 PostgreSql v11
[[email protected] ~]# service network restart
           

3、安装基本工具

[[email protected] ~]# yum install -y vim lrzsz tree wget rpm net-tools
[[email protected] ~]# yum update -y
           

4、浏览器打开 https://www.postgresql.org/download/linux/redhat/ 

如下图:

记一次 Centos7.4 使用 yum 安装 PostgreSql v11

5、安装资源 rpm 、客户端、服务端

[[email protected] ~]# yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
           
[[email protected] ~]# yum install -y postgresql11
           
记一次 Centos7.4 使用 yum 安装 PostgreSql v11
[[email protected] ~]# yum install -y postgresql11-server
           
记一次 Centos7.4 使用 yum 安装 PostgreSql v11

6、数据库初始化

[[email protected] ~]# /usr/pgsql-11/bin/postgresql-11-setup initdb
效果:
Initializing database ... OK
           

7、设置允许外部连接

查看目录

记一次 Centos7.4 使用 yum 安装 PostgreSql v11
[[email protected] ~]# vim /var/lib/pgsql/11/data/postgresql.conf
           
记一次 Centos7.4 使用 yum 安装 PostgreSql v11
[[email protected] ~]# vim /var/lib/pgsql/11/data/pg_hba.conf
           
host    all             all             0.0.0.0/0               md5
           
记一次 Centos7.4 使用 yum 安装 PostgreSql v11

说明:

TYPE:pg的连接方式,local:本地unix套接字,host:tcp/ip连接

DATABASE:指定数据库

USER:指定数据库用户

ADDRESS:ip地址,可以定义某台主机或某个网段,32代表检查整个ip地址,相当于固定的ip,24代表只检查前三位,最后一                         位是0~255之间的任何一个

METHOD:认证方式,常用的有ident,md5,password,trust,reject。

                      md5是常用的密码认证方式。

                      password是以明文密码传送给数据库,建议不要在生产环境中使用。

                      trust是只要知道数据库用户名就能登录,建议不要在生产环境中使用。

                      reject是拒绝认证。

8、防火墙开放 5432 端口(条件允许可关闭防火墙)

例如:
[[email protected] ~]# firewall-cmd --zone=public(作用域) --add-port=80/tcp(端口和访问类型) --permanent(永久生效)
操作:
[[email protected] ~]# firewall-cmd --zone=public --add-port=5432/tcp --permanent
success
[[email protected] ~]#  firewall-cmd --reload
success
           

其它防火墙操作可参考:

https://blog.csdn.net/llwy1428/article/details/99676257

9、 postgresql 安装目录授权

[[email protected] ~]# chown postgres:root -R /usr/pgsql-11/
           

10、启动服务

[[email protected] ~]# systemctl start postgresql-11
[[email protected] ~]# netstat -lntp
           
记一次 Centos7.4 使用 yum 安装 PostgreSql v11

11、切换用户,设置数据库密码

[[email protected] ~]# su - postgres
-bash-4.2$ psql -U postgres
postgres=# ALTER USER postgres with encrypted password '123456';
postgres=# \l
           
记一次 Centos7.4 使用 yum 安装 PostgreSql v11

注意:遇到启动错误及解决方式:

[[email protected] ~]# systemctl start postgresql-11
Job for postgresql-11.service failed because the control process exited with error code. See "systemctl status postgresql-11.service" and "journalctl -xe" for details.
           

查看错误详情:

[[email protected] ~]# journalctl -xe
           
记一次 Centos7.4 使用 yum 安装 PostgreSql v11

说明:data 目录下信息可能丢失

重新初始化,提示 data 目录已经存在

[[email protected] ~]# /usr/pgsql-11/bin/postgresql-11-setup initdb
Data directory is not empty!
           

删除现有 data 目录下的所有信息

[[email protected] ~]# rm -rf /var/lib/pgsql/11/data/*
           

重新执行数据库初始化:

[[email protected] ~]# /usr/pgsql-11/bin/postgresql-11-setup initdb
Initializing database ... OK
           

再次启动 postgresql 服务

[[email protected] ~]# systemctl start postgresql-11
[[email protected] ~]# netstat -lntp
           
记一次 Centos7.4 使用 yum 安装 PostgreSql v11
记一次 Centos7.4 使用 yum 安装 PostgreSql v11

12、第三方客户端工具测试连接:

记一次 Centos7.4 使用 yum 安装 PostgreSql v11
记一次 Centos7.4 使用 yum 安装 PostgreSql v11

至此  Centos7.4  使用  yum  安装 PostgreSql   v11  操作完毕!

安装、配置 Pgadmin 可参考:

https://hunter.blog.csdn.net/article/details/102486511

扩展:Centos7 编译安装 PostgreSql 11.4

https://blog.csdn.net/llwy1428/article/details/95444151