- 一、数据库安装
- 二、配置防火墙
- 三、初始化用户名和密码
- 四、修改配置文件,配置远程访问
- 五、重启服务
- 六、连接测试
官网地址:https://www.postgresql.org/download/linux/redhat/
1.1 安装存储库RPM文件
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
1.2 安装PostgreSQL
yum install -y postgresql12-contrib postgresql12-server
1.3 验证是否安装成功
rpm -aq | grep postgres
1.4 初始化数据库
/usr/pgsql-12/bin/postgresql-12-setup initdb
1.5 设置开机自启动,启动PostgreSQL服务
systemctl enable postgresql-12 systemctl start postgresql-12二、配置防火墙
# 查询5432端口是否开放 firewall-cmd --query-port=5432/tcp # 开放5432端口(5432为PostgreSQL端口) firewall-cmd --zone=public --add-port=5432/tcp --permanent # 重启 firewall-cmd --reload # 查看防火墙是否放行5432端口 firewall-cmd --zone=public --list-ports三、初始化用户名和密码
# 切换用户,执行后提示符会变为'-bash-4.2$' su - postgres # 登录数据库,执行后提示符变为 'postgres=#' psql -U postgres # 设置密码,设置postgres用户密码为postgres ALTER USER postgres WITH PASSWORD 'postgres'; ALTER USER postgres WITH PASSWORD '密码'; # 退出数据库 q # 备注其他:列出所有库l 列出所有用户du 列出库下所有表d # 登出 exit四、修改配置文件,配置远程访问
4.1 修改监听postgresql.conf
vim /var/lib/pgsql/12/data/postgresql.conf # 修改如下内容: #listen_addresses = 'localhost' 为 listen_addresses='*'
4.2 修改pg_hba.conf
vim /var/lib/pgsql/12/data/pg_hba.conf # 修改如下内容,信任指定服务器连接 # 如果想允许所有IPv4地址,则加入一行host all all 0.0.0.0/0 md5。IPv6方法类似。 # IPv4 local connections: host all all 127.0.0.1/32 md5 host all all 0.0.0.0/0 md5五、重启服务
切换到root用户,重启postgresql服务
systemctl restart postgresql-12六、连接测试



