Java/SpringBoot

[SpringBoot #1-2] Project 생성(DB Driver 포함_PostgreSQL)

은정재 2022. 6. 17. 15:44

사전작업 : PostgreSQL 설치

PostgreSQL 설치법은 아래 링크를 참고한다.

https://egurishun.tistory.com/28?category=943477

 

[PostgreSQL #1] MacOS에 설치하기

1. PostgreSQL 설치 터미널에서 다음과 같이 명령어를 입력하여 PostgreSQL을 설치한다. brew install postgresql 2. PostgreSQL 시작 1) 서비스 시작 brew services start postgresql 2) 설치된 버전 확인 postg..

egurishun.tistory.com


1. Spring Starter Project 생성

1) Package Explorer > 'Create new Spring Starter Project' 선택

2) Java Version : 설치한 JDK Version에 맞춰 선택

3) Name, Group, Package : 사용자 지정 (com.egurishun)

4) 'Next' 선택

5) Spring Boot Version : '2.7.0' 선택

* Swagger-UI 사용 시 2.5.x대로 설정해줘야 호환 가능

6) Available : 검색을 통해 아래 3가지 선택

- Spring Web

- Spring Data JPA

- PostgreSQL Driver

* MySQL Driver, MariaDB Driver 등 사용자 환경에 맞게 구성

7) 'Finish' 버튼 선택

8) Project 생성 확인

9) Tomcat Server 실행 (생략 가능)

서버를 바로 실행할 시, 에러가 발생한다.

* 6) 과정에서 DB Driver를 설치하였기 때문에 프로젝트에는 접근할 DB에 관한 정보가 필요하다.

에러 발생 console 화면

서버를 정상적으로 구동하기 위해 application.properties 파일에 DB 관련 정보를 입력해줘야 한다.

 

2. DB 연동 설정 (application.properties)

상단링크를 통한 사전작업이 끝났다면, 해당정보를 토대로 application.properties를 수정한다.

1) 사전작업 요약

- database 생성 : test_db

- user 생성 : postgres

- pw 설정 : admin

2) application.properties 수정

spring.datasource.url=jdbc:postgresql://localhost:5432/test_db
spring.datasource.username=postgres
spring.datasource.password=admin
spring.datasource.driverClassName=org.postgresql.Driver

spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL10Dialect

3) Tomcat Server 실행

성공적으로 실행된 console 화면

 


Test Environment Info.
- OS : macOS Catalina 10.15.2
- JDK : 1.8.0_321
- STS : 4.13.0.RELEASE