温馨提示×

centos怎样保postgresql安

小樊
36
2026-10-12 05:32:20
栏目: 云计算

你的问题应该是在问 CentOS 怎样安装 PostgreSQL(保 = 安装/部署 的笔误)。下面给你几种常用方法。


方法一:使用 CentOS 官方仓库(最简单,但版本较旧)

适用于 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),不推荐生产使用。


方法二:使用 PostgreSQL 官方 YUM 仓库(推荐 ✅)

1. 添加官方仓库(以 PostgreSQL 15 为例)

# 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

2. 安装 PostgreSQL

yum install -y postgresql15-server postgresql15

3. 初始化数据库

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

4. 启动服务

systemctl start postgresql-15
systemctl enable postgresql-15

方法三:使用 Docker(最干净、最灵活)

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 的方法。

0 踩