CentOS 8.5 安装使用 PostgreSQL

1. 添加官方 Yum 源

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

如果出现错误:

Errors during downloading metadata for repository 'ius':
- Status code: 404 for https://repo.ius.io/7/x86_64/repodata/repomd.xml

解决:

rm -f /etc/yum.repos.d/ius*.repo

2. 禁用系统模块

dnf -qy module disable postgresql

3. 安装 PostgreSQL 16

dnf install -y postgresql16-server postgresql16

4. 初始化

/usr/pgsql-16/bin/postgresql-16-setup initdb

5. 启动并自启

systemctl enable postgresql-16
systemctl start postgresql-16
systemctl status postgresql-16

6. 创建数据库

su - postgres
psql
CREATE USER solidtime WITH PASSWORD '你的强密码';
CREATE DATABASE solidtime OWNER solidtime ENCODING 'UTF8';
GRANT ALL PRIVILEGES ON DATABASE solidtime TO solidtime;
\q
exit

7. postgresql.conf

vi /var/lib/pgsql/16/data/postgresql.conf

修改:

listen_addresses = '*'
port = 5432

8. pg_hba.conf

vi /var/lib/pgsql/16/data/pg_hba.conf

添加(测试):

host    all     all     0.0.0.0/0       md5

或(推荐):

host    all     all     1.2.3.4/32       md5

9. 重启

systemctl restart postgresql-16
ss -lntp | grep 5432

10. 开放端口

image

Rocky安装

流程和上面一致

如果有防火墙需要放行端口

firewall-cmd --add-port=5432/tcp --permanent
firewall-cmd --reload
firewall-cmd --list-ports
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐