postgresql 中带和不带时区的时间戳之间的区别
本文将讨论 postgresql 中的时间戳类型并展示它们的区别。
postgresql 中的时间戳
在 postgresql 中,有两种类型的时间戳。
- 没有时区的时间戳
- 带时区的时间戳
第一个存储本地日期。例如,假设现在是 24 小时系统时钟中的 11.00。
因此,这将存储为 11.00。如果它保存在数据库中,比如远程数据库中,并且有人从 cst 时区拉出这一行,他仍会将其视为 11.00。
然而,这不是真正的时间。查看该时间所需的 cst 将转换为 cst 时区。
由于它不存储有关时区的任何信息,因此无法在不同时区进一步确定。
第二种方法解决了这个问题。因此,每当你在数据库中推送时间戳时,它都会从主机系统中提取时区并将时区与时间戳一起保存。
假设我们在 gmt 6 时区。这是时间戳的演示。
selectnow()as"system time",now()::timestampas"postgres time",now()attimezone'gmt'as"time without zone",now()attimezone'cst'as"time without zone",now()::timestampattimezone'gmt'as"timestamp gmt",now()::timestampattimezone'cst'as"timestamp cst";
这里,第一列包含系统时间,第二列包含将时间转换为不带时区的时间戳后的时间戳。
输出:
system time | postgres time | time without zone | time without zone | timestamp gmt | timestamp cst
------------------------------- ---------------------------- ---------------------------- ---------------------------- ------------------------------- -------------------------------
2022-03-15 10:19:05.432758 06 | 2022-03-15 10:19:05.432758 | 2022-03-15 04:19:05.432758 | 2022-03-14 22:19:05.432758 | 2022-03-15 16:19:05.432758 06 | 2022-03-15 22:19:05.432758 06
(1 row)
postgresql 中带和不带时区的时间戳之间的区别
让我们创建一个表,看看它如何存储时间戳,以及如何在 postgresql 中使用无时区
和有时区
。
首先,将你的 psql 控制台连接到 postgres,然后运行以下 sql 命令来创建如下表:
createtabletimes(idintprimarykeynotnull,time_without_zonetimestampwithouttimezonedefaultnow(),time_with_zonetimestampwithtimezonedefaultnow());
现在,用一些条目填充表。
insertintotimes(id)values(1);insertintotimes(id)values(2);insertintotimes(id)values(3);insertintotimes(id)values(4);insertintotimes(id)values(5);insertintotimes(id)values(6);insertintotimes(id)values(7);
输出:
postgres=# select * from times;
id | time_without_zone | time_with_zone
---- ---------------------------- -------------------------------
1 | 2022-03-15 10:29:03.52078 | 2022-03-15 10:29:03.52078 06
2 | 2022-03-15 10:29:03.52564 | 2022-03-15 10:29:03.52564 06
3 | 2022-03-15 10:29:03.526723 | 2022-03-15 10:29:03.526723 06
4 | 2022-03-15 10:29:03.527775 | 2022-03-15 10:29:03.527775 06
5 | 2022-03-15 10:29:03.528865 | 2022-03-15 10:29:03.528865 06
6 | 2022-03-15 10:29:03.529941 | 2022-03-15 10:29:03.529941 06
7 | 2022-03-15 10:29:05.045774 | 2022-03-15 10:29:05.045774 06
(7 rows)
postgres=#
如你所见,time_with_zone
列存储带有 gmt 06 时区的时间。时间可以转换为任何其他时区,因为 psql 将知道列中时间的基数。
这是一个示例输出,它显示了如果你尝试在没有时区类型的列上插入带有时区的时间戳会发生什么。
insertintotimes(id,time_without_zone,time_with_zone)values(9,'2022-03-15 10:29:05.045774 06','2022-03-15 10:29:05.045774 06');
输出:
postgres=# select * from times where id=9;
id | time_without_zone | time_with_zone
---- ---------------------------- -------------------------------
9 | 2022-03-15 10:29:05.045774 | 2022-03-15 10:29:05.045774 06
(1 row)
如你所见,psql 只是在 time_without_zone
列中删除了 06。
如果要查看 postgres 中所有可用的时区,可以运行以下 sql 命令:
postgres=#selectnamefrompg_timezone_names;name----------------------------------
africa/abidjanafrica/accraafrica/addis_ababaafrica/algiersafrica/asmaraafrica/asmeraafrica/bamakoafrica/banguiafrica/banjulafrica/bissau-- more --
此处,系统在 gmt 6 时区运行。如果要将 postgres 时区更改为其他时区,可以运行以下命令:
postgres=#settimezone='utc';set
如果你看到上表的数据,你会看到带有时区的列已成功转换为 utc。
postgres=#select*fromtimes;id|time_without_zone|time_with_zone---- ---------------------------- -------------------------------
1|2022-03-1510:29:03.52078|2022-03-1504:29:03.52078002|2022-03-1510:29:03.52564|2022-03-1504:29:03.52564003|2022-03-1510:29:03.526723|2022-03-1504:29:03.526723004|2022-03-1510:29:03.527775|2022-03-1504:29:03.527775005|2022-03-1510:29:03.528865|2022-03-1504:29:03.528865006|2022-03-1510:29:03.529941|2022-03-1504:29:03.529941007|2022-03-1510:29:05.045774|2022-03-1504:29:05.045774009|2022-03-1510:29:05.045774|2022-03-1504:29:05.04577400(8rows)
你可以看到 time_without_zone
保持不变,但 time_with_zone
从 gmt 06 转换为 utc。要了解有关时间戳格式和表示的更多信息,请访问这里。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
在一个 postgresql 查询中使用多个 with 语句
发布时间:2023/03/20 浏览次数:337 分类:postgresql
-
在本教程中,我们将学习如何使用多个 with 语句在 postgresql 中使用两个临时表执行查询。
发布时间:2023/03/20 浏览次数:185 分类:postgresql
-
本文介绍如何在 ubuntu 上找到 postgresql 数据库的配置文件。
发布时间:2023/03/20 浏览次数:409 分类:数据库
-
本文解释了如何直接从终端/命令行或 psql shell 运行 sql 文件。为此,你需要指定主机名、端口、用户名和数据库名称。
发布时间:2023/03/20 浏览次数:89 分类:postgresql
-
本文展示了如何列出 postgresql 上的活动连接。
发布时间:2023/03/20 浏览次数:966 分类:postgresql
-
在 pl/sql 中,你可能需要在 postgres 中使用循环。我们可以使用 for 和 while 语句来创建循环。
发布时间:2023/03/20 浏览次数:141 分类:postgresql
-
本文介绍如何在 postgresql 中仅使用单个查询来重命名列以及更改其类型。
发布时间:2023/03/20 浏览次数:233 分类:postgresql
-
本文介绍如何在 postgresql 中使用 select 方法连接列。
发布时间:2023/03/20 浏览次数:281 分类:postgresql
-
本文展示了如何使用 case 语句并给出了 postgresql 中的示例。