Python
Alembic alembic.ini 파일 sqlalchemy.url 동적으로 수정하기
john.k
2023. 8. 11. 12:36
반응형
Alembic의 alembic.ini 파일에 이런 식으로 DB 주소를 적는데요.
sqlalchemy.url = postgresql://user:password@localhost:5432/database_name
저는 환경변수에 저장된 DB 주소를 불러오고 싶었는데, ini 파일이라서 환경변수를 사용할 수가 없었습니다.
그래서 방법을 찾아 본 결과 alembic init을 한 디렉토리에 있는 env.py 파일에서 다음과 같은 코드를 입력하는 방법이 있었습니다.
config.set_main_option("sqlalchemy.url", os.getenv("DATABASE_URL", "postgresql://user:password@localhost:5432/database_name"))
설정 단계에서 환경 변수를 로드해서 주소를 수정하는 방법 같네요.
이렇게 해서 동적으로 alembic의 db 주소를 환경 변수에서 불러올 수 있었습니다.
출처 :
https://stackoverflow.com/questions/37890284/ini-file-load-environment-variable
.ini file load environment variable
I am using Alembic for migrations implementation in a Flask project. There is a alembic.ini file where the database configs must be specified: sqlalchemy.url = driver://user:password@host/dbname Is
stackoverflow.com
반응형