为何需删除postmaster.pid?PostgreSQL启动异常求助
解决Homebrew安装的PostgreSQL@15残留postmaster.pid问题
确保服务正确自启与关闭
用Homebrew服务命令配置PostgreSQL随系统启动,关机时系统会自动触发正常停止流程,避免pid文件残留:brew services enable postgresql@15 brew services start postgresql@15添加系统关机钩子强制清理
若系统关机时PostgreSQL未及时退出,可创建LaunchDaemon脚本确保关机时正常停止服务:- 创建文件
/Library/LaunchDaemons/com.postgresql.shutdown.plist,写入以下内容(替换你的用户名为当前系统用户名):<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>com.postgresql.shutdown</string> <key>ProgramArguments</key> <array> <string>/opt/homebrew/bin/pg_ctl</string> <string>-D</string> <string>/opt/homebrew/var/postgres@15</string> <string>stop</string> <string>-m</string> <string>fast</string> </array> <key>ExitTimeOut</key> <integer>10</integer> <key>UserName</key> <string>你的用户名</string> </dict> </plist> - 加载该脚本:
sudo launchctl load /Library/LaunchDaemons/com.postgresql.shutdown.plist
- 创建文件
检查日志定位根本问题
查看PostgreSQL日志文件,排查是否有权限或进程异常导致pid无法正常清理:cat /opt/homebrew/var/postgres@15/log/postgresql.log若发现权限错误,修复数据目录权限:
sudo chown -R $(whoami) /opt/homebrew/var/postgres@15自动清理无效pid文件(临时方案)
若上述方法无效,可编写脚本自动检测并删除无效pid:- 创建脚本
clean_postgres_pid.sh:#!/bin/bash PID_FILE="/opt/homebrew/var/postgres@15/postmaster.pid" if [ -f "$PID_FILE" ]; then PID=$(cat "$PID_FILE") if ! ps -p "$PID" > /dev/null 2>&1; then rm "$PID_FILE" fi fi - 赋予执行权限:
chmod +x clean_postgres_pid.sh - 将脚本添加到用户登录项(系统偏好设置→用户与群组→登录项),开机时自动执行清理。
- 创建脚本
内容的提问来源于stack exchange,提问作者thiebo
相关产品推荐
相关产品推荐

