녕지의 개발 블로그
article thumbnail
Published 2022. 12. 29. 17:02
[PostgreSQL] 시간 타입 database/PostgreSQL

PostgreSQL에서 시간 다루기

postgresql에서 시간 데이터 타입은 timestamp, date, time이 있다.

 

현재 시간 반환 방법

select now();
select current_timestamp now;

now() / current_timestamp

now() / current_timestamp 

이 두 개를 사용하면 위와 같은 결과값이 나온다

 

current_time

current_time

현재 시간만 가져온다

current_date

cureent_date

현재 날짜만 가져온다

 

timestamp

날짜와 시간값을 저장하는 8바이트 값이다.

 

select now()::timestamp as now;
-- YYYY-MM-DD HH:MI:SS 형태로 값 반환

date

날짜 정보만 저장하는 4바이트 값이다

select now()::date as now;
-- YYYY-MM-DD 형태로 값 반환

time

시간 정보만 저장하는 8바이트 값이다

select now()::time as now;

interval

시간 값에 시간을 더하거나 뺄 때 사용하는 기능이다.

select now() -interval '1day';
-- 현재 날짜에서 하루를 뺀 날짜가 반환된다

1day 대신 1 + year, month, day, hour, minute, second 등이 들어갈 수 있다.

'database > PostgreSQL' 카테고리의 다른 글

RETURNING  (0) 2022.12.12
profile

녕지의 개발 블로그

@녕지

주니어 개발자가 쓰는 IT에서 살아남기 . . .