2017-03-23 6 views
1

AWS ECS에서 psycopg2를 통한 데이터베이스 연결에 문제가 있습니다. App 컨테이너와 DB 컨테이너가 있습니다. 컨테이너가 연결됩니다.AWS ECS의 PostgreSQL : psycopg2.OperationalError 잘못된 포트 번호 5432

응용 프로그램에는 응용 프로그램 서버를 시작하기 전에 DB가 가동 중인지 확인하는 진입 점 스크립트가 있습니다.

$ until psql -h "$DB_HOST" -U "$DB_USER" -c '' && >&2 echo "Postgres is up"; do 
    >&2 echo "Postgres is unavailable - sleeping" 
    sleep 1 
done 

> Is the server running on host "db" (172.17.0.3) and accepting 
> TCP/IP connections on port 5432? 
> Postgres is unavailable - sleeping 
> Postgres is up 

이 부분은 잘 작동하지만 즉시 애플리케이션 서버가 시작될 때 나는 다음과 같은 오류를 얻을 DB에 연결을 시도 :

psycopg2.OperationalError: invalid port number: "tcp://172.17.0.3:5432" 

나는 경우가 될 수 무엇인지 전혀 모른다. Docker로 로컬에서 실행하는 경우이 방법이 유용합니다.

힌트를 보내 주시면 감사하겠습니다. 감사!

답변

1

그래서 조금 더 배경을 제공하십시오. 응용 프로그램은 장고에 기록되고 여기에 데이터베이스 설정 섹션은 다음과 같습니다

항목 스크립트에서 psql 명령은 기본적에게 5432 포트를 사용하여, 잘 연결되었다
DATABASES = { 
    'default': { 
     # Requests will be wrapped in a transaction automatically 
     # https://docs.djangoproject.com/en/1.10/topics/db/transactions/#tying-transactions-to-http-requests 
     'ATOMIC_REQUESTS': True, 
     'ENGINE': 'django.contrib.gis.db.backends.postgis', 
     'NAME': os.getenv('DB_NAME', 'postgres'), 
     'USER': os.getenv('DB_USER', 'postgres'), 
     'PASSWORD': os.getenv('DB_PASSWORD', 'secret'), 
     'HOST': os.getenv('DB_HOST', 'localhost'), 
     'PORT': os.getenv('DB_PORT', 5432), 
     'OPTIONS': { 
      'client_encoding': 'UTF8' 
     } 
    } 
} 

. 장고 연결을 열려고 할 때 그렇게 할 이유가 표시되지 않는, 명시 적으로 DB_PORT ENV를 설정하지 않은으로

는 지금, 그것은이 os.getenv('DB_PORT', 5432) 호출에서 5432의 기본값을 사용했다.

AWS ECS 작업 정의에서 DB_PORT ENV를 명시 적으로 설정하고 놀랍게도 작동했습니다. 어떤 이유로 든 어쩌면 int 대신 str으로 전달되었을 수 있습니다.

작업 구성에서 ENV var 정의를 추가/제거하여 2 번 확인했습니다.

0
#!/bin/bash 
set -e 
cmd="[email protected]" 
if [ -z "$POSTGRES_USER" ]; then 
    export POSTGRES_USER=postgres 
fi 

export DATABASE_URL=postgres://$POSTGRES_USER:[email protected]:5432/$POSTGRES_USER 


function postgres_ready(){ 
python << END 
import sys 
import psycopg2 
try: 
    conn = psycopg2.connect(dbname="$POSTGRES_USER", user="$POSTGRES_USER", password="$POSTGRES_PASSWORD", host="postgres") 
except psycopg2.OperationalError: 
    sys.exit(-1) 
sys.exit(0) 
END 
} 

until postgres_ready; do 
    >&2 echo "Postgres is unavailable - sleeping" 
    sleep 1 
done 

>&2 echo "Postgres is up - continuing..." 
exec $cmd