실습에서 Spring 은 따로 Dockerize 하지 않고 로컬로 구동,
PostgresQL 은 Docker 로 구동할 예정
아래 명령어를 통해 바로 구동 가능
docker run --name postgresql-container \
-e POSTGRES_USER=asac \
-e POSTGRES_PASSWORD=1234 \
-e POSTGRES_DB=example \
-p 54322:5432 -d postgres:15
⚠️ 이전에 해당 명령어로 이미 컨테이너를 띄운 적이 있는 경우
docker stop postgresql-container명령어로
컨테이너가 종료된 상태여도 재구동이 가능
- 재구동을 위해
docker start postgresql-container수행 시
docker run과 동일docker rm -f postgresql-container명령어로 기존 컨테이너 삭제 후
이전 명령어 입력으로도 가능

-a)docker ps

컨테이너를 구동시킬 때는 docker start container
→ 컨테이너를 종료시킬 때는 docker stop container
아래 명령어를 통해 컨테이너 내 접속하여, 직접 데이터베이스 접속 및 쿼리 수행 가능
docker exec -it postgresql-container bash
PostgresQL DBMS 접속 : MySQL 과 달리 접속 시 데이터베이스를 선택
psql -U asac -d example
데이터베이스 확인
\l
List of databases
Name | Owner | Encoding | Collate | Ctype | ICU Locale | Locale Provider | Access privileges
-----------+-------+----------+------------+------------+------------+-----------------+-------------------
example | asac | UTF8 | en_US.utf8 | en_US.utf8 | | libc |
postgres | asac | UTF8 | en_US.utf8 | en_US.utf8 | | libc |
template0 | asac | UTF8 | en_US.utf8 | en_US.utf8 | | libc | =c/asac +
| | | | | | | asac=CTc/asac
template1 | asac | UTF8 | en_US.utf8 | en_US.utf8 | | libc | =c/asac +
| | | | | | | asac=CTc/asac
(4 rows)
테이블 확인
\dt
Did not find any relations.

# PostgresQL Docker 컨테이너 중지
docker stop postgresql-container
# PostgresQL Docker 컨테이너 시작
docker start postgresql-container
# PostgresQL Docker 컨테이너 재시작
docker restart postgresql-container
dependencies {
// runtimeOnly 'com.mysql:mysql-connector-j'
runtimeOnly 'org.postgresql:postgresql'
// runtimeOnly 'com.h2database:h2'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
...
}
gradle.build(A) Driver : PostgresQL Driver 설정
= 데이터베이스 접속을 위한 URL + ID/PW 및 스레드풀 관리
(B) JPA : Spring Data JPA 설정
= Java/Spring 내에서 데이터베이스 사용을 위한 JPA 관련 모듈
application.properties# ORM
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
# PostgresQL - CREATE USER user PASSWORD '!@#' SUPERUSER;
spring.datasource.url=jdbc:postgresql://localhost:54322/example?useSSL=false
spring.datasource.username=asac
spring.datasource.password=1234
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
application.ymlspring:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:54322/example?useSSL=false
username: asac
password: 1234
jpa:
generate-ddl: true
hibernate:
ddl-auto: create
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
show-sql: true

Entity 클래스 정의를 기반으로 데이터베이스 내
테이블을 생성하는 Auto DDL 이나 실제 DML 쿼리 수행 시
") 필요") 적용spring.jpa.properties.hibernate.globally_quoted_identifiers=true
application.properties 사용 설정 (application.yml 에서는 나누어 작성)컨테이너 내 PostgresQL 에서 SQL 쿼리를 수행할 때도 걸리적거리므로 아래와 같이 작성해줄 것
DROP TABLE employees, user ;
DROP TABLE employees, "user";
앞의 설정으로 충분히 스프링 어플리케이션이 정상 구동되긴했지만,
좀 더 직접적으로 테이블 생성까지 완료해볼 것
@Getter
@Entity
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@ToString
public class User {
/**
* JPA 통한 Database 사용 시 @GeneratedValue 전략에 대해 조금 상세히 알 필요가 있다.
* - AUTO : ID 생성 책임이 JPA 에게 있다 (JPA 는 hibernate_sequence 라는 sequence 테이블을 만들어 활용, nextval 호출)
* - IDENTITY : ID 생성 책임을 Database 에게 위임한다. (PostgresQL 은 Primary Key 에 대해 SERIAL 로 정의 및 DB 자체적으로 Sequence 생성)
* > MySQL 라면 AUTO_INCREMENT 사용할것이고,
* > PostgresQL 이라면 SERIAL + Sequence 사용 (sequence name 형식은 {tablename}_{columnname}_seq), currval 호출)
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Setter
private Integer id;
private String name;
private Integer age;
private String job;
private String specialty;
private LocalDateTime createdAt;
}
com.example.demo.repository.entity 라는

Did not find any relations
Control + Z\d+ user
