云服务器搭建PostgreSQL12(已验证)

本文操作可适用环境
系统:CentOS 7.x
软件:PostgreSQL 9.x-12.x

本文使用环境:
系统:7.6 Release
软件:12.x
本篇博客以笔记记录为主,同时也分享给大家,方便大家快速安装pgsql

PostgreSQL安装步骤

1、导入yum源

sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

2、安装PostgreSQL服务(12)

sudo yum install -y postgresql12 postgresql12-server

安装PostgreSQL服务(11)
yum install postgresql12 postgresql12-server
安装PostgreSQL服务(9.5)
yum install postgresql95 postgresql95-server
在这里插入图片描述

3、初始化数据库

sudo /usr/pgsql-12/bin/postgresql-12-setup initdb 

在这里插入图片描述
4、启动PostgreSQL服务

#启动PostgreSQL服务
sudo systemctl start postgresql-12

#设置PostgreSQL服务为开机启动
sudo systemctl enable postgresql-12

9.x版本的服务名是postgresql-9.x

修改postgres账号密码

postgres数据库中会初始化一名超级用户postgres
为了安全和管理,我们这里来修改一下密码
1、进入PostgreSQL命令行
通过su命令切换linux用户为postgres会自动进入命令行

su postgres

2、启动SQL Shell

psql

3、修改密码

ALTER USER postgres WITH PASSWORD 'NewPassword';

配置远程访问

1、开放端口(可跳过)

sudo firewall-cmd --add-port=5432/tcp --permanent
sudo firewall-cmd --reload

2、修改IP绑定

#修改配置文件
vi /var/lib/pgsql/12/data/postgresql.conf

#将监听地址修改为*
#默认listen_addresses配置是注释掉的,所以可以直接在配置文件开头加入该行
listen_addresses='*'

3、允许所有IP访问

#修改配置文件
vi /var/lib/pgsql/12/data/pg_hba.conf

#在文件尾部加入
host  all  all 0.0.0.0/0 md5

4、重启PostgreSQL服务

#重启PostgreSQL服务
sudo systemctl restart postgresql-12

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

在这里插入图片描述

记着点个赞哦~o

猜你喜欢

转载自blog.csdn.net/weixin_44146379/article/details/110492683