Redshift 中, 您不能使用集群参数组设置 timezone 配置参数。使用 SET 命令只能为当前会话设置时区。要为某个特定数据库用户运行的所有会话设置时区,请使用 ALTER USER 命令。ALTER USER … SET TIMEZONE 将更改后续会话的时区,而不是更改当前会话的时区。
日期时间类型 (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) 您可以使用 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
