云服务之阿里云--服务器搭建:Postgre 9.5 安装
2019-11-05 本文已影响0人
回首潇潇雨歇处
CentOS Yum 工具安装,简单方便,查看了一下官方源版本,显示目前最新版本是9.2.15,需要更新源,文档中有专门的rpm包列表,RPM LIST。
添加RPM
yum install https://download.postgresql.org/pub/repos/yum/9.5/redhat/rhel-7-x86_64/pgdg-centos95-9.5-2.noarch.rpm


安装PostgreSQL 9.5
yum install postgresql95-server postgresql95-contrib

初始化数据库
/usr/pgsql-9.5/bin/postgresql95-setup initdb

设置开机自启动
systemctl enable postgresql-9.5.service
启动服务
systemctl start postgresql-9.5.service

自此,PostgreSQL 9.5 安装完成,此过程中注意安装权限,我在安装过程中一直使用的是root用户进行的安装。接下来,进行一下简单的配置。PostgreSQL 安装完成后,会建立一下‘postgres’用户,用于执行PostgreSQL,数据库中也会建立一个'postgres'用户,默认密码为自动生成,需要在系统中改一下。
修改用户密码
su - postgres # 切换用户,执行后提示符会变为 '-bash-4.2$'
psql # 登录数据库,执行后提示符变为 'postgres=#'
select version() # 获取数据库的版本信息
# 通过\l命令可以查看目前有哪些数据库
ALTER USER postgres WITH PASSWORD 'abc123' #设置postgres用户密码
\q # 退出数据库

开启远程访问
vi /var/lib/pgsql/9.5/data/postgresql.conf
修改#listen_addresses = 'localhost' 为 listen_addresses=''
当然,此处‘’也可以改为任何你想开放的服务器IP

信任远程连接
vi /var/lib/pgsql/9.5/data/pg_hba.conf
修改如下内容,信任指定服务器连接
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 10.211.55.6/24(需要连接的服务器IP) MD5

打开防火墙
CentOS 防火墙中内置了PostgreSQL服务,配置文件位置在/usr/lib/firewalld/services/postgresql.xml,我们只需以服务方式将PostgreSQL服务开放即可。
firewall-cmd --add-service=postgresql --permanent #开放postgresql服务
firewall-cmd --reload #重载防火墙

如果出现上图中的错误,那是因为我们的防火墙没有打开,按以下操作打开:
systemctl status firewalld # 查看防火墙的状态,发现当前是dead状态,即防火墙未开启。显示running即已开启了
systemctl start firewalld # 开启防火墙,没有任何提示即开启成功
systemctl stop firewalld # 关闭防火墙

然后添加 postgresql 服务,提示success,表示设置成功

添加端口支持,提示success,表示设置成功

firewalld-cmd --reload # 重载防火墙
firewall-cmd --list-all # 查看防火墙列表,发现我们的 postgresql 已经开启了

重启PostgreSQL数据服务
systemctl restart postgresql-9.5.service
使用 Navicat 连接postgresql数据库



"postgres": {
"host": "127.0.0.1",
"port": 5432,
"user": "postgres",
"password": "hahahaha",
"database": "hehehehe"
}
连接成功,接下来我们就可以通过我们的 navicat 实现本地连接管理我们的云数据库了
引自: 昭熙小乐的博客