PostgreSQL postmaster.pid 잠금 파일 문제 해결하기
문제 상황
Homebrew에서 PostgreSQL 서비스가 에러 상태를 보여줍니다:
❯ brew services list
Name Status User File
postgresql@17 error 1 user ~/Library/LaunchAgents/[email protected]
원인 조사
로그 확인
PostgreSQL 로그에서 원인을 확인할 수 있었습니다:
❯ tail -50 /opt/homebrew/var/log/[email protected]
2026-01-15 10:57:27.984 PST [49027] FATAL: lock file "postmaster.pid" already exists
2026-01-15 10:57:27.984 PST [49027] HINT: Is another postmaster (PID 732) running in data directory "/opt/homebrew/var/postgresql@17"?
postmaster.pid 잠금 파일이 이미 존재해서 PostgreSQL이 시작에 실패하고 있었습니다. PID 732가 사용 중이라고 표시되어 있었습니다.
잠금 파일 확인
❯ cat /opt/homebrew/var/postgresql@17/postmaster.pid
732
/opt/homebrew/var/postgresql@17
1764184024
5432
/tmp
localhost
21922676 65536
stopping
잠금 파일 내용:
- PID 732 - 잠금을 소유한 프로세스 ID
- 포트 5432 - PostgreSQL 포트
- 상태: “stopping” - PostgreSQL이 종료 중에 문제가 발생
PID 732가 실제로 PostgreSQL인지 확인
❯ ps aux | grep -E "^.* 732 "
user 732 0.0 0.1 435406032 59968 ?? S Tue10AM 0:11.34 /System/Library/PrivateFrameworks/IMCore.framework/imagent.app/Contents/MacOS/imagent
PID 732는 PostgreSQL이 아닌 **macOS의 iMessage 데몬(imagent)**이었습니다!
근본 원인
- PostgreSQL이 PID 732로 실행 중이었음
- 시스템 종료 또는 PostgreSQL이 “stopping” 상태에서 충돌
postmaster.pid잠금 파일이 정리되지 않음- 재부팅 후 macOS가 PID 732를 다른 프로세스(
imagent)에 할당 - PostgreSQL은 잠금 파일에 PID 732가 아직 데이터 디렉토리를 사용 중이라고 기록되어 있어 시작을 거부
- PostgreSQL은 PID 732가 실제로 postgres 프로세스인지 확인하지 않음
해결 방법
오래된 잠금 파일을 삭제하고 PostgreSQL을 재시작합니다:
rm /opt/homebrew/var/postgresql@17/postmaster.pid
brew services restart postgresql@17
postmaster.pid 이해하기
postmaster.pid 파일의 역할:
- 다중 인스턴스 방지 - 하나의 데이터 디렉토리에 하나의 PostgreSQL 서버만 사용하도록 보장
- 접속 정보 저장 - 클라이언트를 위한 포트 번호와 소켓 디렉토리 정보 포함
- 정상 종료 지원 -
pg_ctl stop이 실행 중인 서버를 찾는 데 사용
잠금 파일 형식
<PID>
<데이터 디렉토리>
<Postmaster 시작 타임스탬프>
<포트>
<Unix 소켓 디렉토리>
<리스닝 주소>
<공유 메모리 키> <공유 메모리 크기>
<상태>
잠금 파일이 오래되는 경우
- 시스템 충돌 또는 정전
- postgres 프로세스에
kill -9실행 - 종료 중 PostgreSQL 충돌
- 정상 종료 없이 컨테이너/VM 종료
예방법
-
항상 PostgreSQL을 정상적으로 종료:
brew services stop postgresql@17 -
시스템 종료 전 PostgreSQL이 중지되었는지 확인
-
PostgreSQL 충돌 시 재시작 전에 오래된 잠금 파일을 확인하고 삭제
안전 참고사항
postmaster.pid를 삭제하기 전에 반드시 PostgreSQL 프로세스가 실행 중이지 않은지 확인하세요:
# postgres 프로세스 확인
pgrep -l postgres
# 더 자세하게
ps aux | grep postgres | grep -v grep
PostgreSQL 프로세스가 없는 경우에만 잠금 파일을 삭제하세요. 실행 중에 삭제하면 데이터 손상이 발생할 수 있습니다.