스프링 - Flyway BeanCreationException 해결 방법
작성일자 : 2025년 03월 09일
오류
스프링 프로젝트에서 Flyway를 적용하다가 다음과 같은 오류가 발생했습니다.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Failed to initialize dependency 'flywayInitializer' of LoadTimeWeaverAware bean 'entityManagerFactory': Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Unable to obtain connection from database: Connection to localhost:5433 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
해결 방법
위 오류는 Flyway가 데이터베이스에 연결하지 못해서 발생한 것입니다.
프로퍼티 파일에 spring.datasource
이외에도 spring.flyway
에 데이터베이스 연결 설정을 추가해주어야 합니다.
application.yml
에 다음과 같이 설정을 추가해줍니다.
spring:
datasource:
url: ${DATABASE_URL}
username: ${DATABASE_USERNAME}
password: ${DATABASE_PASSWORD}
driver-class-name: org.postgresql.Driver
flyway:
enabled: true
locations: classpath:db/migration
url: jdbc:postgresql://localhost:5433/mydb
user: ${DATABASE_USERNAME}
password: ${DATABASE_PASSWORD}
baseline-on-migrate: true # flyway_schema_history 테이블 생성
validate-on-migrate: true