场景

PostGresSQL简介与Windows上的安装教程

上面讲了在Windows上的安装教程,在CentOS上怎样安装。

注:

关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

1、去Postgresql的官网,根据自己对应的版本生成安装脚本

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

Postgresql在CentOS上的安装(脚本在线安装)_leaflet

 

这里使用14版本,Centos,脚本内容如下

sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo yum install -y postgresql14-server
sudo /usr/pgsql-14/bin/postgresql-14-setup initdb
sudo systemctl enable postgresql-14
sudo systemctl start postgresql-14

来到服务器上某目录下新建脚本文件

touch postgresqlInstall.sh

编辑脚本文件将上面的内容复制进去

vi postgresqlInstall.sh

赋予脚本执行权限

chmod 755 postgresqlInstall.sh

然后执行脚本文件

./postgresqlInstall.sh

Postgresql在CentOS上的安装(脚本在线安装)_数据库_02

2、安装脚本执行成功之后创建用户和数据库并授权,安装成功之后默认会生成postgres用户以及一个postgres数据库,不带密码,为了方便后续使用新建用户和数据库

切换postgres用户登录(PostgresSQL安装后会自动创建postgres用户,无密码)

su postgres

登录postgresql数据库

psql

创建用户和数据库授权

create user badao with password '123456';

create database test_db owner badao ;

grant all privileges on database test_db to badao ;

退出psql

输入 \q 再按回车键

3、配置允许远程连接

修改/var/lib/pgsql/14/data/postgresql.conf,这里14对应自己的数据库版本,取消 listen_addresses 的注释,将参数值改为“*”

Postgresql在CentOS上的安装(脚本在线安装)_数据库_03

添加本地连接地址修改/var/lib/pgsql/14/data/pg_hba.conf

Postgresql在CentOS上的安装(脚本在线安装)_postgresql_04

 

在IPv4下添加一行

host  all  all  0.0.0.0/0  trust

保存退出,重启服务

systemctl restart postgresql-14

4、服务器上开启默认5432端口并重新加载防火墙

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

firewall-cmd --reload

5、查看服务状态

systemctl status postgresql-14.service

Postgresql在CentOS上的安装(脚本在线安装)_postgresql_05

 

6、使用Navicat远程连接

Postgresql在CentOS上的安装(脚本在线安装)_leaflet_06