PostgreSQL修改数据库timezone

628 阅读2分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第21天

timezone 是客户端设定的会话级别的时区参数。如果客户端没有指定,则以数据库设置的timezone参数或db,user级别参数为准。

例子:
查看当前timezone

bill@bill=>show timezone;
 TimeZone 
----------
 PRC
(1 row)
bill@bill=>select now();
              now              
-------------------------------
 2020-03-04 16:21:27.970797+08
(1 row)

修改timezone:
加粗样式
–方法一

bill@bill=>set timezone='+9';
SET
bill@bill=>show timezone;
 TimeZone 
----------
 <+09>-09
(1 row)

bill@bill=>select now();
              now              
-------------------------------
 2020-03-04 17:22:26.457585+09
(1 row)

–方法二

bill@bill=>alter role all set timezone='Asia/Kolkata';  
ALTER ROLE
bill@bill=>show timezone;
   TimeZone   
--------------
 Asia/Kolkata
(1 row)

bill@bill=>select now();
               now                
----------------------------------
 2020-03-04 13:53:28.741183+05:30
(1 row)

需要注意的是:我们设置的timezone必须得是数据库当前支持的,可以通过系统表查询当前数据库支持哪些别名时区设置。

bill@bill=>select * from pg_timezone_names;  
               name               | abbrev | utc_offset | is_dst 
----------------------------------+--------+------------+--------
 Africa/Asmera                    | EAT    | 03:00:00   | f
 Africa/Timbuktu                  | GMT    | 00:00:00   | f
 Africa/Algiers                   | CET    | 01:00:00   | f
 Africa/Ndjamena                  | WAT    | 01:00:00   | f
 Africa/Abidjan                   | GMT    | 00:00:00   | f
 Africa/Cairo                     | EET    | 02:00:00   | f
 Africa/Accra                     | GMT    | 00:00:00   | f
 Africa/Bissau                    | GMT    | 00:00:00   | f
 Africa/Nairobi                   | EAT    | 03:00:00   | f
 Africa/Monrovia                  | GMT    | 00:00:00   | f
 Africa/Tripoli                   | EET    | 02:00:00   | f
 Africa/Casablanca                | +01    | 01:00:00   | f
 Africa/El_Aaiun                  | +01    | 01:00:00   | f
 Africa/Maputo                    | CAT    | 02:00:00   | f
 Africa/Windhoek                  | CAT    | 02:00:00   | f
 Africa/Lagos                     | WAT    | 01:00:00   | f
...skipping one line
(594 rows)

时区支持文件所在位置:$PGHOME/share/timezonesets

pg12@oracle-> ll
total 104K
-rw-r--r-- 1 pg12 pg12 6.9K Sep 17 23:04 Africa.txt
-rw-r--r-- 1 pg12 pg12  11K Sep 17 23:04 America.txt
-rw-r--r-- 1 pg12 pg12 1.2K Sep 17 23:04 Antarctica.txt
-rw-r--r-- 1 pg12 pg12 8.2K Sep 17 23:04 Asia.txt
-rw-r--r-- 1 pg12 pg12 3.5K Sep 17 23:04 Atlantic.txt
-rw-r--r-- 1 pg12 pg12 1.2K Sep 17 23:04 Australia
-rw-r--r-- 1 pg12 pg12 3.3K Sep 17 23:04 Australia.txt
-rw-r--r-- 1 pg12 pg12  27K Sep 17 23:04 Default
-rw-r--r-- 1 pg12 pg12 1.3K Sep 17 23:04 Etc.txt
-rw-r--r-- 1 pg12 pg12 8.6K Sep 17 23:04 Europe.txt
-rw-r--r-- 1 pg12 pg12  593 Sep 17 23:04 India
-rw-r--r-- 1 pg12 pg12 1.3K Sep 17 23:04 Indian.txt
-rw-r--r-- 1 pg12 pg12 3.7K Sep 17 23:04 Pacific.txt