今天使用dbeaver添加时间字段发现可以选择
timestamp without time zone 和timestamp 两个
这个两个有什么区别吗
实际没有区别
测试如下:
sql
test1=# create table testtbl(info timestamp,info1 timestamp without time zone);
CREATE TABLE
test1=# \d testtbl
Table "public.testtbl"
Column | Type | Collation | Nullable | Default
--------+-----------------------------+-----------+----------+---------
info | timestamp without time zone | | |
info1 | timestamp without time zone | | |
实际是一个
但是PostgreSQL的类型中只有timestamp,并没有timestamp without time zone.
使用\d testtbl命令显示为timestamp without time zone是因为\d元命令后台调用了format_type函数,其会将timestamp类型表示为timestamp without time zone 而已。
sql
test1=# select oid from pg_type where typname='timestamp';
oid
------
1114
(1 row)
test1=# select oid from pg_type where typname='timestamp without time zone';
oid
-----
(0 rows)
为什么使用timestamp without time zone作为字段类型也可以呢
是因为PostgreSQL在语法分析阶段就将其转换成了timestamp了。
具体可以查看
src/backend/parser/gram.y

