AI 부트캠프/챕터5(12.30~01.31)

TIL 84

musukie 2025. 1. 16. 22:21

트러블 슈팅

언제까지 포트 5432와 씨름해야 하는지 모르겠다. 매번 다른 방식으로 해결해야 했다. 아래 내용은 이번에 포트 5432와 다툰 내용이다. 결론은 Postgresql 서버를 시작해야 했다.

문제 상황

drf         | django.db.utils.OperationalError: connection to server at "localhost" (::1), port 5432 failed: Connection refused
drf         |   Is the server running on that host and accepting TCP/IP connections?
drf         | connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
drf         |   Is the server running on that host and accepting TCP/IP connections?
  • Django가 PostgreSQL 데이터베이스에 연결하려고 시도했지만 연결이 거부되었다.

원인

PostgreSQL 서버가 포트 5432에서 이미 실행 중인 것으로 확인됐다.

해결

PostgreSQL 서버가 정상적으로 실행되도록 하거나, 충돌하는 다른 프로세스를 종료해야 한다.

  1. sudo lsof -i :5432 명령어로 PostgreSQL 관련 프로세스를 찾고 실행 중인 PostgreSQL 프로세스가 있다면, 해당 프로세스를 종료해야 한다. (PostgreSQL 15가 실행 중인 것을 확인했다.)
  2. sudo kill -9 <PID> 명령어로 PostgreSQL 15 프로세스를 종료해야 한다.
  3. pg_ctl -D /path/to/data_directory restart 명령어로 PostgreSQL 데이터베이스 서버를 재시작해야 한다.
  4. 3번에서 Permission denied 에러가 발생한다면 방법을 찾아봐야 하는데,
    1. sudo chown -R postgres:staff /opt/homebrew/var/postgresql@14 명령어로 postgres 계정에서 실행하여 sudo chmod 700 /opt/homebrew/var/postgresql@14 명령어로 권한을 수정해주라고 한다. 그러나 위의 방법에서 password가 맞지 않다는 오류가 자꾸 발생하여 터미널 창을 껐다가 켰다.
    2. 다시 pg_ctl -D /opt/homebrew/var/postgresql@14 start 명령어를 치자 could not bind Unix address "/tmp/.s.PGSQL.5432": Address already in use 에러가 발생했다. 이는 이전에 생성된 소켓 파일이 남아있을 수 있어 /tmp 디렉토리에 PostgreSQL 소켓 파일이 남아 있는지 확인하고, 삭제해야 한다고 한다. ls -l /tmp/.s.PGSQL.5432 명령어로 파일이 존재함을 확인했고, sudo rm -f /tmp/.s.PGSQL.5432 명령어로 파일을 삭제했다. 그러고 나서 다시 ``pg_ctl -D /opt/homebrew/var/postgresql@14 start` 명령어를 치자 서버가 시작했음을 확인할 수 있었다.

아래는 처음에 받은 에러 메시지와 해결 후 서버가 시작됐음을 확인한 내용이다.

# 에러 메시지
(base) sparta@spartaui-MacBookAir ~ % pg_ctl -D /opt/homebrew/var/postgresql@14 start

waiting for server to start....2025-01-16 08:55:03.805 KST [13569] LOG:  starting PostgreSQL 14.15 (Homebrew) on aarch64-apple-darwin23.6.0, compiled by Apple clang version 16.0.0 (clang-1600.0.26.4), 64-bit
2025-01-16 08:55:03.806 KST [13569] LOG:  listening on IPv6 address "::1", port 5432
2025-01-16 08:55:03.806 KST [13569] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2025-01-16 08:55:03.806 KST [13569] LOG:  could not bind Unix address "/tmp/.s.PGSQL.5432": Address already in use
2025-01-16 08:55:03.806 KST [13569] HINT:  Is another postmaster already running on port 5432?
2025-01-16 08:55:03.806 KST [13569] WARNING:  could not create Unix-domain socket in directory "/tmp"
2025-01-16 08:55:03.807 KST [13569] FATAL:  could not create any Unix-domain sockets
2025-01-16 08:55:03.807 KST [13569] LOG:  database system is shut down
 stopped waiting
pg_ctl: could not start server
Examine the log output.


# 서버 시작 확인
(base) sparta@spartaui-MacBookAir ~ % pg_ctl -D /opt/homebrew/var/postgresql@14 start

waiting for server to start....2025-01-16 08:56:57.751 KST [13912] LOG:  starting PostgreSQL 14.15 (Homebrew) on aarch64-apple-darwin23.6.0, compiled by Apple clang version 16.0.0 (clang-1600.0.26.4), 64-bit
2025-01-16 08:56:57.752 KST [13912] LOG:  listening on IPv6 address "::1", port 5432
2025-01-16 08:56:57.752 KST [13912] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2025-01-16 08:56:57.752 KST [13912] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2025-01-16 08:56:57.756 KST [13913] LOG:  database system was shut down at 2025-01-15 12:21:33 KST
2025-01-16 08:56:57.758 KST [13912] LOG:  database system is ready to accept connections
 done
server started

 

'AI 부트캠프 > 챕터5(12.30~01.31)' 카테고리의 다른 글

TIL 86  (0) 2025.01.21
TIL 85  (0) 2025.01.17
TIL 83  (0) 2025.01.14
TIL 82  (0) 2025.01.08
TIL 81  (1) 2025.01.07