-
django error: connection to server at "localhost" (::1), port 5432 failed: Connection refused공부/오류 2023. 3. 1. 22:07
내용
sqlite3을 사용하다 postgresql을 사용하려고 하는데, 해당 문제를 겪게 되었다.
에러 내용
더보기django.db.utils.OperationalError:
connection to server at "localhost" (::1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?문제
내가 생각할 때 나한테 postgres가 설치가 안돼 있다고 생각을 하게 되었다. 왜냐하면 다른 분들이 쓴 블로그 글이나 문제들을 보면 local 환경에 postgres를 설치하여 사용하고 있었고, postgres와 관련이 된 파일을 제거하면 된다고 한다.
그러나 나의 경우에는 postgres를 설치하지 않고 도커로 진행을 하려고 하였다. 도커에서 진행하면서 겪었던 문제들이 여러가지 있을 수 있지만, 연결성의 문제가 있었다고 보고 있다.
아래 작성한 방법은 내가 도커를 이용하여 local환경에서 postgres를 사용한 방법이다.
해결 방법
1. docker에 postgres 컨테이너를 생성해야 한다.
더보기docker run -d \
-e POSTGRES_USER=username \
-e POSTGRES_PASSWORD=password \
-e POSTGRES_DB=dbname \
-p 5432:5432 \
postgres:latestdocker run => 도커 컨테이너를 생성
-d => 도커를 데몬으로 돌린다 ( 백그라운드에서 실행될수있도록 한다.)
-e => 환경변수를 등록한다.
-p => 포트를 설정한다.
postres:latest 이미지를 선정한다.
해당 명령어를 입력하면 postgres가 도커 데몬에서 실행이 되는것을 확인 할 수 있다.
2. settings 파일을 변경해야한다.
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'dbname', 'USER': 'username', 'PASSWORD': 'password', 'HOST': 'localhost', 'PORT': '5432', } }settings를 설정해줘야 local에서 runserver를 했을 경우 문제없이 작동 할 수 있다.
3. migrate 실행
python manage.py makemigrations
python manage.py migrate
를 차례로 입력하면 기존에 봤던 에러가 아닌 초록색 OK 를 볼 수 있을 것이다.
그리고 나는 서버를 gunicorn app.wsgi.prod:application으로 돌려주고 테스트를 해보니 정상적으로 처리가 되었다.!!!!
- 어떤 문제를 해결하기위해 검색하고 블로그에 작성한 글입니다. 부족한점이 많지만 틀린점이나 부족한점이 있다면 말씀해주시면 감사하겠습니다
Ref.
https://junior-datalist.tistory.com/177
[Postgres 접속 에러] : could not connect to server: Connection refused Is the server running on host "localhost" (::1) and a
상황 : nestjs 서버 접속시 db 연결이 되지 않는 문제 발생 에러메시지 : could not connect to server: Connection refused Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? 원인: pg 서버가
junior-datalist.tistory.com
'공부 > 오류' 카테고리의 다른 글