PostgreSQL數(shù)據(jù)庫timestamp數(shù)據(jù)類型精度進(jìn)位問題解析
PostgreSQL數(shù)據(jù)庫timestamp數(shù)據(jù)類型精度進(jìn)位問題
1. PostgreSQL簡介
PostgreSQL 起源于 1986 年加州大學(xué)伯克利分校開發(fā)的 POSTGRES 項目。它經(jīng)過多年的發(fā)展和演進(jìn),成為了一個功能強(qiáng)大、開源的關(guān)系型數(shù)據(jù)庫管理系統(tǒng)。
PostgreSQL 本身是開源的,通常不存在嚴(yán)格意義上的商業(yè)版和社區(qū)版之分。
官網(wǎng)地址:https://www.postgresql.org/
在官網(wǎng)上,您可以獲取更詳細(xì)和準(zhǔn)確的信息,包括最新的特性、下載安裝指南、文檔教程等。
2. PostgreSQL特點
1.強(qiáng)大的功能:支持豐富的特性,如復(fù)雜查詢、事務(wù)處理、存儲過程、視圖等。
2.開源和社區(qū)驅(qū)動:擁有活躍的開源社區(qū),不斷進(jìn)行改進(jìn)和更新。
3.可擴(kuò)展性:易于擴(kuò)展功能和數(shù)據(jù)類型。
4.標(biāo)準(zhǔn)符合性:高度符合 SQL 標(biāo)準(zhǔn)。
5.安全性:提供多種安全機(jī)制,包括用戶認(rèn)證、權(quán)限管理等。
3. PostgreSQL優(yōu)缺點
優(yōu)點:
1.穩(wěn)定性高:經(jīng)過長期的實踐和優(yōu)化,具有出色的穩(wěn)定性和可靠性。
2.數(shù)據(jù)完整性:確保數(shù)據(jù)的一致性和完整性。
3.跨平臺支持:可在多種操作系統(tǒng)上運(yùn)行。
4.豐富的文檔和支持:有詳細(xì)的官方文檔和活躍的社區(qū)支持。
缺點:
1.配置相對復(fù)雜:對于新手來說,初始配置可能具有一定的難度。
2.資源消耗:在處理大型數(shù)據(jù)集時,可能需要較多的系統(tǒng)資源。
4. PostgreSQL數(shù)據(jù)類型
包括數(shù)值類型(如整數(shù)、浮點數(shù))、字符串類型(固定長度和可變長度)、日期和時間類型、布爾類型、數(shù)組類型、枚舉類型、幾何類型等。
| 數(shù)據(jù)類型 | 說明 | 精度 | 特點 | 案例 |
|---|---|---|---|---|
smallint | 小范圍整數(shù) | 2 字節(jié) | 占用存儲空間小,適用于數(shù)據(jù)范圍較小的整數(shù) | smallint: 100 |
integer(或 int) | 常規(guī)整數(shù) | 4 字節(jié) | 常見的整數(shù)類型,能滿足大多數(shù)常規(guī)整數(shù)需求 | integer: 2000 |
bigint | 大范圍整數(shù) | 8 字節(jié) | 用于存儲非常大的整數(shù)數(shù)值 | bigint: 9000000000 |
decimal(或 numeric) | 用戶指定精度和小數(shù)位數(shù)的精確數(shù)值 | 取決于指定的精度和小數(shù)位數(shù) | 精度和小數(shù)位數(shù)可自定義,適用于對精度要求高的數(shù)值計算 | decimal(5, 2): 123.45 |
real | 單精度浮點數(shù) | 4 字節(jié) | 大約 6 位十進(jìn)制精度 | 存儲精度要求不高的浮點數(shù) |
double precision | 雙精度浮點數(shù) | 8 字節(jié) | 大約 15 位十進(jìn)制精度 | 適用于高精度的浮點數(shù)計算 |
char(n) | 固定長度的字符字符串,不足長度用空格填充 | n 個字符 | 長度固定,存儲和讀取效率較高 | char(10): 'hello ' |
varchar(n) | 可變長度的字符字符串,最大長度為 n | 最大 n 個字符 | 節(jié)省存儲空間,適用于長度不固定的字符串 | varchar(20): 'hello world' |
text | 可變長度的字符字符串,無長度限制 | 無限制 | 可存儲大量文本數(shù)據(jù) | text: 'This is a long text.' |
date | 日期 | 格式為 YYYY-MM-DD | 只存儲日期信息 | date: '2024-08-08' |
time | 時間,不包含日期 | 格式為 HH:MM:SS | 只存儲時間信息 | time: '12:30:00' |
timestamp | 日期和時間 | 格式為 YYYY-MM-DD HH:MM:SS ,支持到微秒級別(6 位小數(shù)) | 同時包含日期和時間信息 | timestamp: '2024-08-08 12:30:00.123456' |
boolean | 布爾值 | 真或假 | 用于表示邏輯值 | boolean: true |
array | 數(shù)組 | 取決于元素類型和數(shù)組長度 | 可以存儲相同數(shù)據(jù)類型的元素集合 | integer[]: '{1, 2, 3}' |
json | JSON 數(shù)據(jù) | 無固定精度 | 方便存儲和處理 JSON 格式的數(shù)據(jù) | json: '{"key": "value"}' |
uuid | 通用唯一標(biāo)識符 | 128 位 | 用于生成唯一標(biāo)識符 | uuid: 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11' |
5. PostgreSQL注意事項
PostgreSQL數(shù)據(jù)庫timestamp數(shù)據(jù)類型精度存在進(jìn)位問題,先說結(jié)論,在測試驗證。
PostgreSQL只能存儲最多6位的小數(shù)部分。因此,當(dāng)嘗試存儲超過6位的分?jǐn)?shù)秒時,會發(fā)生以下情況之一:
如果額外的數(shù)字導(dǎo)致整個分?jǐn)?shù)秒部分超過 999999 微秒,則整個時間值會進(jìn)位到下一秒,剩余的部分會被截斷或者四舍五入到 6 位小數(shù)。
在這種情況下,2024-08-07 16:19:23.999999999 會被處理為 2024-08-07 16:19:24.000000。
如果你使用的是某種客戶端或者編程語言接口來處理這個時間值,在插入之前它可能會自動進(jìn)行四舍五入或截斷。
比如,
截取前三位數(shù)據(jù):小于 2024-08-07 16:19:23.999999500
進(jìn)位數(shù)據(jù):[2024-08-07 16:19:23.999999500 - 2024-08-07 16:19:23.999999999]
通過指定精度數(shù)據(jù)庫中仍然會保存6位,但是指定精度3時后三位始終為0,精度6則保存6位。
SELECT * FROM crm.t_wx_user_1 WHERE 1=1; SELECT user_id, to_char(last_login_time, 'YYYY-MM-DD HH24:MI:SS.US'), to_char(create_time, 'YYYY-MM-DD HH24:MI:SS.US') FROM crm.t_wx_user_1 WHERE 1=1; -- last_login_time timestamp,-- 》timestamp(3) ALTER TABLE crm.t_wx_user_1 ALTER COLUMN last_login_time TYPE TIMESTAMP(3) WITHOUT TIME ZONE; ALTER TABLE crm.t_wx_user_1 ALTER COLUMN create_time TYPE TIMESTAMP(6) WITHOUT TIME ZONE;
注意問題:
PostgreSQL 的 timestamp 類型默認(rèn)只支持到微秒級別(6位小數(shù)),為什么select出來的數(shù)據(jù)中展示三位2024-08-07 16:19:23.999?
PostgreSQL 的 timestamp 類型確實支持到微秒級別(6位小數(shù))。然而,默認(rèn)情況下,當(dāng)您從數(shù)據(jù)庫中查詢 timestamp 值時,PostgreSQL 會將結(jié)果顯示為毫秒級別(3位小數(shù))以提高可讀性。這是默認(rèn)的行為,但是您可以更改輸出的精度。案例如下。
--PostgreSQL數(shù)據(jù)庫timestamp數(shù)據(jù)類型精度進(jìn)位問題
--# 1.PostgreSQL數(shù)據(jù)庫數(shù)據(jù)類型
-- 數(shù)據(jù)庫版本,PostgreSQL 15.3 on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
SELECT version();
-- 創(chuàng)建數(shù)據(jù)庫
-- crm.t_wx_user_1 definition
-- Drop table
-- DROP TABLE crm.t_wx_user_1;
CREATE TABLE crm.t_wx_user_1 (
user_id int8 NOT NULL,
union_id varchar(50) NULL,
open_id varchar(50) NULL,
last_login_time timestamp NULL,
privacy_agreement_version int8 NOT NULL DEFAULT 0,
create_time timestamp NOT NULL,
update_time timestamp NOT NULL DEFAULT '1970-01-01 00:00:00'::timestamp without time zone,
delete_flag int2 NOT NULL DEFAULT 0,
delete_time timestamp NOT NULL DEFAULT '1970-01-01 00:00:00'::timestamp without time zone,
first_login_time timestamp NULL,
created_by int8 NOT NULL DEFAULT '-1'::integer,
updated_by int8 NOT NULL DEFAULT '-1'::integer,
inst_id int8 NULL,
CONSTRAINT t_wx_user_pkey_1 PRIMARY KEY (user_id)
);
CREATE INDEX idx_t_wx_user_open_id_1 ON crm.t_wx_user_1 USING btree (open_id);
CREATE INDEX idx_t_wx_user_union_id_1 ON crm.t_wx_user_1 USING btree (union_id);
CREATE UNIQUE INDEX uniq_wx_userid_unionid_openid_1 ON crm.t_wx_user_1 USING btree (user_id, union_id, open_id);
-- 測試驗證數(shù)據(jù)庫
select * from crm.t_wx_user_1 order by user_id ;
-- postgres數(shù)據(jù)庫 timestamp類型,新增操作,支持類型 2024-08-07 16:19:23.142,正常插入數(shù)據(jù)庫
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230000, '1', '1', '2024-08-07 16:19:23.142', 0, '2024-07-12 16:52:39.674', '2024-08-07 16:19:23.142', 0, '1970-01-01 00:00:00.000', '2024-07-12 16:52:39.674', -1, -1, 1);
-- postgres數(shù)據(jù)庫 timestamp類型,新增操作,6位數(shù)據(jù)類型 2024-08-07 16:19:23.123456,截斷前三位插入數(shù)據(jù)庫
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230001, '1', '1', '2024-08-07 16:19:23.123456', 0, '2024-07-12 16:52:39.123456', '2024-08-07 16:19:23.123456', 0, '1970-01-01 00:00:00.123456', '2024-07-12 16:52:39.123456', -1, -1, 1);
-- postgres數(shù)據(jù)庫 timestamp類型,新增操作,3個9驗證 2024-08-07 16:19:23.999,正常插入數(shù)據(jù)庫
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230002, '1', '1', '2024-08-07 16:19:23.999', 0, '2024-07-12 16:52:39.999', '2024-08-07 16:19:23.999', 0, '1970-01-01 00:00:00.999', '2024-07-12 16:52:39.999', -1, -1, 1);
-- postgres數(shù)據(jù)庫 timestamp類型,新增操作,4個9驗證 2024-08-07 16:19:23.9999,截斷前三位插入數(shù)據(jù)庫
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230003, '1', '1', '2024-08-07 16:19:23.9999', 0, '2024-07-12 16:52:39.9999', '2024-08-07 16:19:23.9999', 0, '1970-01-01 00:00:00.9999', '2024-07-12 16:52:39.9999', -1, -1, 1);
-- postgres數(shù)據(jù)庫 timestamp類型,新增操作,6個9驗證 2024-08-07 16:19:23.999999,截斷前三位插入數(shù)據(jù)庫
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230004, '1', '1', '2024-08-07 16:19:23.999999', 0, '2024-07-12 16:52:39.999999', '2024-08-07 16:19:23.999999', 0, '1970-01-01 00:00:00.999999', '2024-07-12 16:52:39.999999', -1, -1, 1);
-- postgres數(shù)據(jù)庫 timestamp類型,新增操作,9個9驗證 2024-08-07 16:19:23.999999999,會進(jìn)位,存到數(shù)據(jù)庫為:2024-08-07 16:19:24.000
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230005, '1', '1', '2024-08-07 16:19:23.999999999', 0, '2024-07-12 16:52:39.999999999', '2024-08-07 16:19:23.999999999', 0, '1970-01-01 00:00:00.999999999', '2024-07-12 16:52:39.999999999', -1, -1, 1);
-- postgres數(shù)據(jù)庫 timestamp類型,查詢操作,9個9驗證 2024-08-07 16:19:23.999999999,會進(jìn)位,把2024-08-07 16:19:24.000的也查詢出來。
select * from crm.t_wx_user_1 where last_login_time <= '2024-08-07 16:19:23.999999999';
-- postgres數(shù)據(jù)庫 timestamp類型,新增操作,9個值驗證 2024-08-07 16:19:23.123456789,截斷前三位插入數(shù)據(jù)庫
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230006, '1', '1', '2024-08-07 16:19:23.123456789', 0, '2024-07-12 16:52:39.123456789', '2024-08-07 16:19:23.123456789', 0, '1970-01-01 00:00:00.123456789', '2024-07-12 16:52:39.123456789', -1, -1, 1);
-- postgres數(shù)據(jù)庫 timestamp類型,查詢操作,9個9驗證 2024-08-07 16:19:23.999999999,會進(jìn)位,把2024-08-07 16:19:24.000的也查詢出來。
select * from crm.t_wx_user_1 where last_login_time <= '2024-08-07 16:19:23.999999999';
-- postgres數(shù)據(jù)庫 timestamp類型,新增操作,9個9驗證 2024-08-07 16:19:23.999999998,會進(jìn)位,存到數(shù)據(jù)庫為:2024-08-07 16:19:24.000
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230007, '1', '1', '2024-08-07 16:19:23.999999998', 0, '2024-07-12 16:52:39.999999998', '2024-08-07 16:19:23.999999998', 0, '1970-01-01 00:00:00.999999998', '2024-07-12 16:52:39.999999998', -1, -1, 1);
-- postgres數(shù)據(jù)庫 timestamp類型,新增操作,9個9驗證 2024-08-07 16:19:23.999999001,截斷前三位插入數(shù)據(jù)庫
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230008, '1', '1', '2024-08-07 16:19:23.999999001', 0, '2024-07-12 16:52:39.999999001', '2024-08-07 16:19:23.999999001', 0, '1970-01-01 00:00:00.999999001', '2024-07-12 16:52:39.999999001', -1, -1, 1);
-- postgres數(shù)據(jù)庫 timestamp類型,新增操作,9個9驗證 2024-08-07 16:19:23.999999991,進(jìn)位
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230009, '1', '1', '2024-08-07 16:19:23.999999991', 0, '2024-07-12 16:52:39.999999991', '2024-08-07 16:19:23.999999991', 0, '1970-01-01 00:00:00.999999991', '2024-07-12 16:52:39.999999991', -1, -1, 1);
-- postgres數(shù)據(jù)庫 timestamp類型,新增操作,9個9驗證 2024-08-07 16:19:23.999999990,進(jìn)位
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230010, '1', '1', '2024-08-07 16:19:23.999999990', 0, '2024-07-12 16:52:39.999999990', '2024-08-07 16:19:23.999999990', 0, '1970-01-01 00:00:00.999999990', '2024-07-12 16:52:39.999999990', -1, -1, 1);
-- postgres數(shù)據(jù)庫 timestamp類型,新增操作,9個9驗證 2024-08-07 16:19:23.999999900,進(jìn)位
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230011, '1', '1', '2024-08-07 16:19:23.999999900', 0, '2024-07-12 16:52:39.999999900', '2024-08-07 16:19:23.999999900', 0, '1970-01-01 00:00:00.999999900', '2024-07-12 16:52:39.999999900', -1, -1, 1);
-- postgres數(shù)據(jù)庫 timestamp類型,新增操作,9個9驗證 2024-08-07 16:19:23.999999800,進(jìn)位
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230012, '1', '1', '2024-08-07 16:19:23.999999800', 0, '2024-07-12 16:52:39.999999800', '2024-08-07 16:19:23.999999800', 0, '1970-01-01 00:00:00.999999800', '2024-07-12 16:52:39.999999800', -1, -1, 1);
-- postgres數(shù)據(jù)庫 timestamp類型,新增操作,9個9驗證 2024-08-07 16:19:23.999999000,截取前三位
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230013, '1', '1', '2024-08-07 16:19:23.999999000', 0, '2024-07-12 16:52:39.999999000', '2024-08-07 16:19:23.999999000', 0, '1970-01-01 00:00:00.999999000', '2024-07-12 16:52:39.999999000', -1, -1, 1);
-- postgres數(shù)據(jù)庫 timestamp類型,新增操作,9個9驗證 2024-08-07 16:19:23.999999500,進(jìn)位
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230014, '1', '1', '2024-08-07 16:19:23.999999500', 0, '2024-07-12 16:52:39.999999500', '2024-08-07 16:19:23.999999500', 0, '1970-01-01 00:00:00.999999500', '2024-07-12 16:52:39.999999000', -1, -1, 1);
-- postgres數(shù)據(jù)庫 timestamp類型,新增操作,9個9驗證 2024-08-07 16:19:23.999999100,進(jìn)位
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230015, '1', '1', '2024-08-07 16:19:23.999999100', 0, '2024-07-12 16:52:39.999999100', '2024-08-07 16:19:23.999999100', 0, '1970-01-01 00:00:00.999999100', '2024-07-12 16:52:39.999999100', -1, -1, 1);
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230016, '1', '1', '2024-08-07 16:19:23.999999200', 0, '2024-07-12 16:52:39.999999200', '2024-08-07 16:19:23.999999100', 0, '1970-01-01 00:00:00.999999100', '2024-07-12 16:52:39.999999100', -1, -1, 1);
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230017, '1', '1', '2024-08-07 16:19:23.999999300', 0, '2024-07-12 16:52:39.999999300', '2024-08-07 16:19:23.999999100', 0, '1970-01-01 00:00:00.999999100', '2024-07-12 16:52:39.999999100', -1, -1, 1);
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230018, '1', '1', '2024-08-07 16:19:23.999999400', 0, '2024-07-12 16:52:39.999999400', '2024-08-07 16:19:23.999999100', 0, '1970-01-01 00:00:00.999999100', '2024-07-12 16:52:39.999999100', -1, -1, 1);
-- 再次驗證 500 進(jìn)位
INSERT INTO crm.t_wx_user_1
(user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id)
VALUES(1230019, '1', '1', '2024-08-07 16:19:23.999999500', 0, '2024-07-12 16:52:39.999999500', '2024-08-07 16:19:23.999999100', 0, '1970-01-01 00:00:00.999999100', '2024-07-12 16:52:39.999999100', -1, -1, 1);
-- postgres數(shù)據(jù)庫 timestamp類型,查詢操作,9個9驗證 2024-08-07 16:19:23.999999999,會進(jìn)位,把2024-08-07 16:19:24.000的也查詢出來。
select * from crm.t_wx_user_1 ;
where last_login_time <= '2024-08-07 16:19:23.999999999';
--截取前三位數(shù)據(jù):小于 2024-08-07 16:19:23.999999500
--進(jìn)位數(shù)據(jù):[2024-08-07 16:19:23.999999500 - 2024-08-07 16:19:23.999999999]
結(jié)論
PostgreSQL只能存儲最多6位的小數(shù)部分。因此,當(dāng)嘗試存儲超過6位的分?jǐn)?shù)秒時,會發(fā)生以下情況之一:
如果額外的數(shù)字導(dǎo)致整個分?jǐn)?shù)秒部分超過 999999 微秒,則整個時間值會進(jìn)位到下一秒,剩余的部分會被截斷或者四舍五入到 6 位小數(shù)。
在這種情況下,2024-08-07 16:19:23.999999999 會被處理為 2024-08-07 16:19:24.000000。
如果你使用的是某種客戶端或者編程語言接口來處理這個時間值,在插入之前它可能會自動進(jìn)行四舍五入或截斷。
比如:
截取前三位數(shù)據(jù):小于 2024-08-07 16:19:23.999999500
進(jìn)位數(shù)據(jù):[2024-08-07 16:19:23.999999500 - 2024-08-07 16:19:23.999999999]
-- 創(chuàng)建表
CREATE TABLE crm.wx_users (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL
);
-- 插入數(shù)據(jù)
INSERT INTO crm.wx_users (name, created_at)
VALUES ('John Doe', '2024-08-07 16:19:23.999999');
INSERT INTO crm.wx_users (name, created_at)
VALUES ('Jerry', '2024-08-07 16:19:23.999996');
-- 查詢數(shù)據(jù)
SELECT * FROM crm.wx_users;
PostgreSQL 的 timestamp 類型默認(rèn)只支持到微秒級別(6位小數(shù)),為什么select出來的數(shù)據(jù)中展示三位2024-08-07 16:19:23.999
PostgreSQL 的 timestamp 類型確實支持到微秒級別(6位小數(shù))。然而,默認(rèn)情況下,當(dāng)您從數(shù)據(jù)庫中查詢 timestamp 值時,PostgreSQL 會將結(jié)果顯示為毫秒級別(3位小數(shù))以提高可讀性。這是默認(rèn)的行為,但是您可以更改輸出的精度。
-- 使用 to_char 函數(shù)來顯示完整的微秒
SELECT id, name, to_char(created_at, 'YYYY-MM-DD HH24:MI:SS.US') AS created_at FROM crm.wx_users;到此這篇關(guān)于PostgreSQL數(shù)據(jù)庫timestamp數(shù)據(jù)類型精度進(jìn)位問題的文章就介紹到這了,更多相關(guān)PostgreSQL timestamp數(shù)據(jù)類型內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決PostgreSQL服務(wù)啟動后占用100% CPU卡死的問題
前文書說到,今天耗費(fèi)了九牛二虎之力,終于馴服了NTFS權(quán)限安裝好了PostgreSQL,卻不曾想,服務(wù)啟動后,新的狀況又出現(xiàn)了。2009-08-08
postgreSql分組統(tǒng)計數(shù)據(jù)的實現(xiàn)代碼
這篇文章給大家介紹postgreSql的監(jiān)控記錄表里多條不同時間的數(shù)據(jù),只取最新的數(shù)據(jù),并分組統(tǒng)計,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2020-12-12
基于PostgreSQL/openGauss?的分布式數(shù)據(jù)庫解決方案
ShardingSphere-Proxy?作為透明數(shù)據(jù)庫代理,用戶無需關(guān)心?Proxy?如何協(xié)調(diào)背后的數(shù)據(jù)庫。今天通過本文給大家介紹基于PostgreSQL/openGauss?的分布式數(shù)據(jù)庫解決方案,感興趣的朋友跟隨小編一起看看吧2021-12-12
postgres 使用存儲過程批量插入數(shù)據(jù)的操作
這篇文章主要介紹了postgres 使用存儲過程批量插入數(shù)據(jù)的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
PostgreSQL中調(diào)用存儲過程并返回數(shù)據(jù)集實例
這篇文章主要介紹了PostgreSQL中調(diào)用存儲過程并返回數(shù)據(jù)集實例,本文給出一創(chuàng)建數(shù)據(jù)表、插入測試數(shù)據(jù)、創(chuàng)建存儲過程、調(diào)用創(chuàng)建存儲過程和運(yùn)行效果完整例子,需要的朋友可以參考下2015-01-01
navicat連接postgresql、人大金倉等數(shù)據(jù)庫報錯解決辦法
在使用Navicat操作數(shù)據(jù)庫時,遇到數(shù)據(jù)報錯是一個常見的問題,這類問題可能涉及多個方面,下面這篇文章主要給大家介紹了關(guān)于navicat連接postgresql、人大金倉等數(shù)據(jù)庫報錯的解決辦法,需要的朋友可以參考下2024-08-08
PostgreSQL實現(xiàn)數(shù)據(jù)表跨庫同步的四種方案
文章詳細(xì)介紹了四種用于實現(xiàn)PostgreSQL數(shù)據(jù)庫表數(shù)據(jù)變化實時或定時同步到另一個獨(dú)立PG庫的方法,包括觸發(fā)器+外部表、邏輯復(fù)制、Debezium+Kafka和pg_dump定時任務(wù),針對不同場景和需求,推薦了最適合的方案,并詳細(xì)闡述了各自的優(yōu)點、適用場景、配置方法和使用注意事項2026-05-05
PostgreSQL基礎(chǔ)知識之SQL操作符實踐指南
這篇文章主要給大家介紹了關(guān)于PostgreSQL基礎(chǔ)知識之SQL操作符實踐的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用PostgreSQL具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05

