安装PostgreSQL
- 添加PostgreSQL的官方Yum仓库
# 添加PostgreSQL的官方Yum仓库:
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
- 安装PostgreSQL 14
# 安装PostgreSQL 14:
sudo yum install -y postgresql14-server
- 初始化数据库并启用自动启动
# 初始化 PostgreSQL
sudo /usr/pgsql-14/bin/postgresql-14-setup initdb;
# 设置开机启动
sudo systemctl enable postgresql-14;
# 启动 PostgreSQL
sudo systemctl start postgresql-14;
PostgreSQL配置
- 修改PostgreSQL配置文件
sudo vi /var/lib/pgsql/14/data/postgresql.conf
将listen_addresses改为*'以监听所有接口,如果是内网访问可以指定具体的IP地址。
- 修改pg_hba.conf文件以允许远程连接
sudo vi /var/lib/pgsql/14/data/pg_hba.conf
添加以下行以允许远程连接(替换0.0.0.0/0为你的具体IP范围):
host all all 0.0.0.0/0 md5
防火墙设置
默认端口:5432
sudo firewall-cmd --permanent --add-port=5432/tcp;
sudo firewall-cmd --reload;
创建用户
账号:root, 密码:123456
# 使用postgres用户
su postgres
# 进入pgsql
psql
# 创建超级管理用户,并赋予远程登录权限
create user root with superuser login password '123456';
# 查看所有用户
\du