You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将CentOS 7默认PostgreSQL服务切换至14版本

配置PostgreSQL 14为CentOS 7开机默认启动服务

步骤1:禁用旧版本PostgreSQL的开机自启

先停止旧版本服务并禁用其开机启动,避免与新版本冲突:

# 停止旧版本服务
sudo systemctl stop postgresql.service
# 禁用开机自启
sudo systemctl disable postgresql.service
# 确认状态,返回"disabled"即为成功
sudo systemctl is-enabled postgresql.service

步骤2:为PostgreSQL 14创建systemd服务文件

CentOS 7通过systemd管理开机服务,需要为PostgreSQL 14创建对应的服务配置文件,替代手动pg_ctl启动的方式:

  1. 创建服务文件:
sudo vi /etc/systemd/system/postgresql-14.service
  1. 将以下内容写入文件(注意替换日志路径为你希望的固定位置,建议放在数据目录下):
[Unit]
Description=PostgreSQL 14 database server
Documentation=man:postgres(1)
After=network.target

[Service]
Type=forking
User=postgres
Group=postgres
# 对应你手动启动的命令参数
ExecStart=/usr/pgsql-14/bin/pg_ctl -D /data/psql/data14 -l /data/psql/data14/postgresql.log start
ExecStop=/usr/pgsql-14/bin/pg_ctl -D /data/psql/data14 stop
ExecReload=/usr/pgsql-14/bin/pg_ctl -D /data/psql/data14 reload
# 服务异常退出时自动重启
Restart=on-failure

[Install]
WantedBy=multi-user.target

注意:日志文件不要用临时的logfile,指定固定路径能避免权限丢失或日志找不到的问题。

  1. 保存退出后,重新加载systemd配置:
sudo systemctl daemon-reload

步骤3:启用PostgreSQL 14的开机自启并验证

# 启动PostgreSQL 14服务
sudo systemctl start postgresql-14.service
# 配置开机自启
sudo systemctl enable postgresql-14.service
# 确认服务运行状态,显示"active(running)"即为正常
sudo systemctl status postgresql-14.service
# 确认开机自启状态,返回"enabled"即为成功
sudo systemctl is-enabled postgresql-14.service

可选:验证开机自启效果

重启服务器后,执行以下命令确认PostgreSQL 14是否自动启动:

sudo systemctl status postgresql-14.service

内容的提问来源于stack exchange,提问作者Jose Cabrera Zuniga

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 22:52:09