PostgreSQL中with和without time zone兩者有什么區別


with和without time zone兩者有什么區別

1.區別

1)名字上看一個是帶時區的,另一個是不帶時區的,查出來的時間是一樣的,只是一個帶時區標志,一個不帶而已,時區的基准是格林威治時間UTC。
2)這對於數據的顯示上來說,區別就是時間數據的末尾帶不帶時區標志,即+/-時區,比如中國(prc),時區是東八區,帶時區標志的話就是+08。

數據庫內部還是都存儲的是UTC格式時間,根據當前的時區進行轉換顯示。

 

2.查看配置文件中時區的配置:

[postgres@localhost ~]$ more /database/pgdata/postgresql.conf |grep timezone
log_timezone = 'US/Pacific'
timezone = 'US/Pacific'
#timezone_abbreviations = 'Default' # Select the set of available time zone
# share/timezonesets/.

 

3.客戶端時區的更改說明

在服務端有一些時區的配置信息,安裝的時候就給裝上了,客戶端時區的更改並不能隨意設置。
這些配置信息是放在$PGHOME/share/timezone里面。


4.實驗:

美國西7區與中國東八區相差15個小時,格林威治時間UTC和東八區相差8個小時

test=# set timezone="US/Pacific";
SET
test=# show timezone; 
TimeZone 
------------
US/Pacific
(1 row)

test=# select now()::timestamp with time zone, now()::timestamp without time zone;
now | now 
-------------------------------+----------------------------
2019-08-20 11:41:47.578319-07 | 2019-08-20 11:41:47.578319
(1 row)

test=# set timezone=prc; 
SET
test=# show timezone; 
TimeZone 
----------
PRC
(1 row)

test=# select now()::timestamp with time zone, now()::timestamp without time zone;
now | now 
-------------------------------+----------------------------
2019-08-21 02:42:01.898303+08 | 2019-08-21 02:42:01.898303
(1 row)

test=# set timezone=UTC; 
SET
test=# show timezone; 
TimeZone 
----------
UTC
(1 row)

test=# select now()::timestamp with time zone, now()::timestamp without time zone;
now | now 
-------------------------------+----------------------------
2019-08-20 18:42:09.018571+00 | 2019-08-20 18:42:09.018571
(1 row)

 

北京、上海都是PRC東八區,時間是一樣的

test=# set timezone="Asia/Shanghai"; 
SET
test=# show timezone; 
TimeZone 
---------------
Asia/Shanghai
(1 row)

test=# select now()::timestamp with time zone, now()::timestamp without time zone;
now | now 
-------------------------------+----------------------------
2019-08-21 02:51:26.704907+08 | 2019-08-21 02:51:26.704907
(1 row)

test=# set timezone="Asia/Beijing"; 
SET
test=# select now()::timestamp with time zone, now()::timestamp without time zone;
now | now 
-------------------------------+----------------------------
2019-08-21 02:51:41.534930+08 | 2019-08-21 02:51:41.534930
(1 row)

test=# set timezone=PRC; 
SET
test=# select now()::timestamp with time zone, now()::timestamp without time zone;
now | now 
-------------------------------+----------------------------
2019-08-21 02:51:54.704583+08 | 2019-08-21 02:51:54.704583
(1 row)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM