在 CentOS 上设置 PostgreSQL 开机自启动,主要取决于你使用的 CentOS 版本 以及 PostgreSQL 的安装方式(系统自带仓库 / 官方 RPM / 源码编译)。下面按常见情况说明。
一般服务名类似:
systemctl list-unit-files | grep postgres
常见名称:
postgresql.servicepostgresql-14.service(版本相关)sudo systemctl enable postgresql
# 或指定版本
sudo systemctl enable postgresql-14
sudo systemctl start postgresql
systemctl status postgresql
chkconfig postgresql on
service postgresql start
如果你是用 EnterpriseDB / PGDG 安装的:
# 初始化(仅第一次)
/usr/pgsql-14/bin/postgresql-14-setup initdb
# 开机自启
systemctl enable postgresql-14
systemctl start postgresql-14
sudo vi /etc/systemd/system/postgresql.service
示例:
[Unit]
Description=PostgreSQL database server
After=network.target
[Service]
Type=forking
User=postgres
ExecStart=/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start
ExecStop=/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data stop
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl daemon-reexec
systemctl enable postgresql
systemctl start postgresql
journalctl -u postgresql
PostgreSQL 必须用 postgres 用户运行:
chown -R postgres:postgres /var/lib/pgsql
检查:
listen_addresses = '*'
在 postgresql.conf 中配置。
如果你能告诉我:
我可以给你更精确的一条命令方案。