你的问题应该是在问 CentOS 怎样安装 PostgreSQL(保 = 安装/部署 的笔误)。下面给你几种常用方法。
适用于 CentOS 7 / 8(CentOS 8 已 EOL,建议用 Stream 或 Rocky/Alma)
# CentOS 7
yum install -y postgresql-server postgresql-contrib
# 初始化数据库
postgresql-setup initdb
# 启动并设置开机自启
systemctl start postgresql
systemctl enable postgresql
默认版本一般是 9.2(CentOS 7),不推荐生产使用。
# CentOS 7
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# CentOS 8 / Stream
dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
yum install -y postgresql15-server postgresql15
/usr/pgsql-15/bin/postgresql-15-setup initdb
systemctl start postgresql-15
systemctl enable postgresql-15
docker run -d \
--name pg \
-e POSTGRES_PASSWORD=yourpassword \
-p 5432:5432 \
postgres:15
# 切换 postgres 用户
su - postgres
# 进入命令行
psql
# 修改密码
ALTER USER postgres WITH PASSWORD 'newpassword';
远程连接 修改:
/var/lib/pgsql/15/data/postgresql.conf
/var/lib/pgsql/15/data/pg_hba.conf
并开放防火墙:
firewall-cmd --add-port=5432/tcp --permanent
firewall-cmd --reload
CentOS 停止维护 建议改用 Rocky Linux / AlmaLinux / CentOS Stream
如果你说的是 “保活 / 守护 PostgreSQL”(不是安装),我也可以告诉你用 systemd 或 supervisor 的方法。