[Spring Boot] Spring Boot와 RDS 연동(PostgreSQL)

Yujeong·2024년 2월 18일
0
post-thumbnail

데이터베이스 정보 확인

AWS RDS 인스턴스에서 데이터베이스의 정보를 확인한다.

  • 엔드포인트
  • DB 식별자
  • 포트: 5432
  • 계정명
  • 비밀번호

build.gradle

PostgreSQL JDBC Driver를 참고하여 최신버전으로 의존성 주입해줬다.

dependencies {
	...
    implementation 'org.postgresql:postgresql:42.7.1'
    ...
}

application-local.yml

  • DB_URL=jdbc:postgresql://<엔트포인트>:5432/<DB명>
  • DB_USERNAME=<계정명>
  • DB_PASSWORD=<비밀번호>
spring:
  config:
    import: classpath:config/local-config.properties
  datasource:
    url: ${DB_URL}
    username: ${DB_USERNAME}
    password: ${DB_PASSWORD}
    driver-class-name: org.postgresql.Driver
  jpa:
    show-sql: true
    database: postgresql
    hibernate:
      ddl-auto: update
logging:
  level:
    root: info

연결 확인

@Slf4j
@SpringBootTest
@ActiveProfiles("local")
public class PostgresqlDbConfigTest {

    @Value("${DB_URL}")
    private String URL;

    @Value("${DB_USERNAME}")
    private String USERNAME;

    @Value("${DB_PASSWORD}")
    private String PASSWORD;


    @Test
    @DisplayName("postgresql db 연결 테스트")
    void DbConnectionTest() throws SQLException {
        Connection conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
        log.info("연결 정보 확인: {}", conn);
        assertThat(conn).isNotNull();
    }
}

참고
Spring Data JDBC - Reference Documentation
[Spring Boot] Jpa와 PostgreSQL 연동

profile
공부 기록

0개의 댓글