AWS - Redshift - 时区 Timezone

  1. Redshift 中, 您不能使用集群参数组设置 timezone 配置参数。使用 SET 命令只能为当前会话设置时区。要为某个特定数据库用户运行的所有会话设置时区,请使用 ALTER USER 命令。ALTER USER … SET TIMEZONE 将更改后续会话的时区,而不是更改当前会话的时区。

  2. 日期时间类型
    (1) 当您创建表时, 若使用日期时间类型为不包含 TZ (time zone), 那么您插入数据时, 无论是否有时区, 结果都为您插入的结果,并且忽略时区。
    例如:

-- t_ntz 中 col1 列时间类型不包含时区
testdb=# create table t_ntz(id int , col1 timestamp);
CREATE TABLE


-- 插入数据
testdb=# insert into t_ntz values (1,'2020-10-01 12:00:00');
INSERT 0 1
testdb=# insert into t_ntz values (1,'2020-10-01 12:00:00+08:00');
INSERT 0 1

-- 查询
testdb=# select * from t_ntz;
 id |        col1
----+---------------------
  1 | 2020-10-01 12:00:00
  1 | 2020-10-01 12:00:00
(2 rows)

(2) 当您创建表时, 若使用日期时间类型包含 TZ(time zone), 那么您插入数据时, 无论您的时区设置为什么, 都会自动将时间转为 UTC 存储。
例如:

-- t_tz 中 col1 列时间类型包含时区
testdb=# create table t_tz(id int, col1 timestamp with time zone);
CREATE TABLE
testdb=# insert into t_tz values (11,'2020-10-01 12:00:00');
INSERT 0 1
testdb=# insert into t_tz values (11,'2020-10-01 12:00:00+08:00');
INSERT 0 1
testdb=# select * from t_tz;
 id |          col1
----+------------------------
 11 | 2020-10-01 12:00:00+00
 11 | 2020-10-01 04:00:00+00
(2 rows)
  1. 您可以使用 ALTER USER 命令将数据库用户设置为某时区,这样您查出一张带有时区的时间列时, 显示结果中的时间列会自动转化为您用户时区来展示。
    例如:
-- 使用 master user 修改用户 user1 的时区设置。
alter user user1 set timezone to 'Asia/Taipei';

-- 使用user1 登录数据库。
-- 分别向 t_ntz 和 t_tz 中插入数据
testdb=> insert into t_ntz values (21, '2020-10-01 12:00:00');
INSERT 0 1
testdb=> insert into t_tz values (21, '2020-10-01 12:00:00');
INSERT 0 1

-- 查看两个表中的数据
testdb=> select * from t_ntz;
 id |        col1
----+---------------------
  1 | 2020-10-01 12:00:00
  1 | 2020-10-01 12:00:00
 21 | 2020-10-01 12:00:00
(3 rows)

testdb=> select * from t_tz;
 id |          col1
----+------------------------
 11 | 2020-10-01 20:00:00+08
 11 | 2020-10-01 12:00:00+08
 21 | 2020-10-01 12:00:00+08
(3 rows)

参考文档:
[1] https://docs.amazonaws.cn/redshift/latest/dg/r_timezone_config.html
[2] https://docs.amazonaws.cn/redshift/latest/dg/r_Datetime_types.html
[3] https://docs.amazonaws.cn/redshift/latest/dg/r_AT_TIME_ZONE.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值