[IntelliJ Ultimate] Data Source Timezone 세팅법과 필요성 (feat. postgres)

식빵·2023년 5월 11일
0

intellij

목록 보기
4/15
post-thumbnail

바쁘신 분들은 Data Source Timezone 세팅법 만 보시기 바랍니다.
나머지 목차들은 보충적인 내용들입니다.

참고로 여기서 DataSource 로 연결하는 DBMS 는 Postgres 입니다.
다른 DBMS 를 사용중이면 이 글과 무관하게 Timezone 을 세팅하지 않아도 정상동작할
수도 있다는 점 유의하시기 바랍니다.


IntelliJ DataSource Timezone


Default Timezone

intellij 에서는 Postgres Datasourcetimezone 세팅을 안하면 timezone=UTC 을 사용합니다. 그래서 select now() 같은 쿼리를 돌리면 UTC 기준으로 시간을 표기합니다.

  • select now() 쿼리의 결과는 04 시 57분
  • 저의 컴퓨터 시계로는 13시 57분 (=오후 1시 57분)
  • 정확히 9 시간 차이가 납니다.
    • 참고: UTC 와 우리나라(Asia/Seoul)의 표준시 의 차이가 9 시간입니다.

이렇게 시간대가 잘못되어서 조회되는 것을 원치 않으면 Intellij
Data Source Timezone 설정을 우리나라(Asia/Seoul) 표준시로 세팅해야 합니다 .

지금부터 그 방법을 알아보겠습니다 😎



Data Source Timezone 세팅법

  • IntelliJ 실행시키고 Ctrl + Shift + A 입력
  • 입력란에 DataBase 입력하고 Enter
  • 이후에 DataBase Tool Window 가 보이게 됩니다.

  • DataBase Tool Window 에서 timezone 변경하고자 하는
    datasource 에 마우스 대고 오른쪽 클릭
  • Properties 버튼 클릭

  • Options 탭 클릭
  • time zone 선택란에서 Asia/Seoul 선택
  • Apply 버튼 클릭

참고(1) : 지금 설정을 바꾼 Data Source 가 이미 query Console 을 열고 사용하던 중이면 곧바로 적용되지 않습니다. 귀찮지만 연결을 한번 Deactivate 하고, 다시 Connect(= Query console 에서 아무 쿼리나 실행) 하면 됩니다.

참고(2) : 이 방법은 한번 DataSource 를 새로 생성할 때마다 수동으로 해줘야 합니다.
만약에 자동으로 DataSource 의 timezone 을 세팅하는 법을 알고 싶다면 자동으로 timezone 적용시키기을 확인해주세요.




참고


DBeaver 는 세팅 안해도 왜 잘될까요?

그런데 Dbeaver 로 똑같은 DataSource 에 접속하여 select now() 쿼리를 실행하면 정상적으로 한국의 timezone 을 적용한 시간을 보여줍니다. 왜 이럴까요?

그 이유는 바로 DBeaver 가 DB 연결 시에 DB timezone 을 자동으로
DBeaver 를 실행시킨 컴퓨터의 timezone 과 똑같이 설정하기 때문입니다..

Intellij 에서는 이런 동작 방식이 사용자에게 더 큰 혼란을 줄 수 있다고 판단하여 기본 timezone=UTC 로 하고, 사용자들이 직접 고르도록 하는 겁니다.




그럼 IntelliJ 는 왜 이렇게 동작시킬까요?


사실 이건 IntelliJ 를 만드신 분들의 의도입니다.

Postgres: Database console sets different timezone in session that PGAdmin 을 읽으면 그 이유가 잘 나옵니다. 그리고 이유뿐만 아니라 Debeaver 와 IntelliJ 의 동작방식이 다른 이유 등도 잘 설명해줍니다. 해당 글 일부를 발췌해서 아래에 복사했으니, 보고 싶으신 분은 참고하시길 바랍니다.


Some conclusions for those who bump in here:

TLDR: Until the driver made it possible to find out about the server time zone, the current solution is the least problematic. And you need to set the time zone in the Options tab.

  1. PostgreSQL in the JDBC driver has no way to find out the default server time zone, which is stored there in the config file. You can find a corresponding ticket with a huge discussion. The ticket, by the way, was closed, although they did not implement it.
    github.com/pgjdbc/pgjdbc/issues/576
  1. So, when DataGrip connects to the database using jdbc, you need to set some kind of time zone. We expose UTC. If this is not done, then the time zone will be set automatically, and it will be taken from the process. That is, roughly speaking, the time zone of the machine from which DataGrip is launched will be set. But it's even worse than UTC because the user doesn't expect it. This is how it used to work for us, and almost every day users came to us who did not understand what was happening.
    We thought for a long time and decided that UTC is the option that causes the least number of problems.

    의도적으로 UTC 가 적용되도록 한 거네요!

  1. What to do for those for whom UTC is not suitable? Set the time zone explicitly, for this we made the UI: intellij-support.jetbrains.com/hc/user_images/D0Mif8Q0HMgqWufc0v2FpQ.png
  1. How does some other clients work (say DBeaver) then if it also uses JDBC? They take the time zone of the local machine, and does not know anything about the time zone of the server, because it's impossible (see point one). If everything works as expected, it means that the client and the server are in the same time zone. That is to say, it’s just a coincidence.



자동으로 timezone 적용시키기

IntelliJ 는 결국 Jdbc Driver 를 통해서 DB 연결을 합니다.
그러니 해당 Jdbc Driver 를 통한 연결을 시도할 때 timezone 옵션을 같이 전송해주면 됩니다.

이렇게 한번만 하면 같은 DBMStimezone 옵션을 추가한 똑같은 Jdbc Driver 가 사용되므로 일정하게 같은 timezone 이 적용됩니다.

방법은 아래와 같습니다.

1. Driver Sources and Drivers 세팅 창에서 Drivers 탭 선택
2. Jdbc Driver 선택
3. Advanced 탭 클릭
4. VM options-Duser.timezone=Asia/Seoul 입력
5. Apply 클릭

이후에 이 Jdbc Driver 를 사용해서 연결을 시도하는 Data Source 생성 시,
자동으로 timezone=Asia/Seoul 이 적용됩니다.

profile
백엔드를 계속 배우고 있는 개발자입니다 😊

0개의 댓글