跳到主要内容

PostgreSQL 数据类型

Halo 数据库在 PostgreSQL 兼容模式下支持完整的 PostgreSQL 数据类型。

数值类型​

类型存储大小范围
SMALLINT2 字节-32768 到 +32767
INTEGER / INT4 字节-2147483648 到 +2147483647
BIGINT8 字节-9223372036854775808 到 +9223372036854775807
REAL4 字节6 位十进制数字精度
DOUBLE PRECISION8 字节15 位十进制数字精度
NUMERIC(p,s) / DECIMAL(p,s)可变小数点前 131072 位,小数点后 16383 位
SERIAL4 字节自增整数(1 到 2147483647)
postgres=# CREATE TABLE int_test (
id SERIAL PRIMARY KEY,
small_num SMALLINT,
num INTEGER,
big_num BIGINT
);
CREATE TABLE
postgres=# INSERT INTO int_test (small_num, num, big_num) VALUES (-32768, 2147483647, 9223372036854775807);
INSERT 0 1
postgres=# \x on
扩展显示已打开
postgres=# SELECT * FROM int_test;
-[ RECORD 1 ]------------------
id | 1
small_num | -32768
num | 2147483647
big_num | 9223372036854775807

高精度小数:

postgres=# CREATE TABLE numeric_test (
id SERIAL PRIMARY KEY,
price NUMERIC(10,2),
ratio DECIMAL(30,20)
);
CREATE TABLE
postgres=# INSERT INTO numeric_test (price, ratio) VALUES (12345.67, 123456.78901234567890123456);
INSERT 0 1
postgres=# SELECT * FROM numeric_test;
id | price | ratio
----+----------+-----------------------------
1 | 12345.67 | 123456.78901234567890123456
(1 行记录)

字符类型​

类型说明最大长度
CHAR(n) / CHARACTER(n)定长字符串10485760
VARCHAR(n) / CHARACTER VARYING(n)变长字符串10485760
TEXT变长字符串1 GB
postgres=# CREATE TABLE char_test (
id SERIAL PRIMARY KEY,
fixed_char CHAR(10),
variable_char VARCHAR(100),
long_text TEXT
);
CREATE TABLE
postgres=# INSERT INTO char_test (fixed_char, variable_char, long_text)
postgres-# VALUES ('hello', 'world', 'This is a long text field');
INSERT 0 1
postgres=# SELECT * FROM char_test;
id | fixed_char | variable_char | long_text
----+------------+---------------+---------------------------
1 | hello | world | This is a long text field
(1 行记录)

日期/时间类型​

类型存储大小说明
DATE4 字节日期(无时间)
TIME8 字节时间(无日期)
TIMETZ12 字节带时区的时间
TIMESTAMP8 字节日期和时间
TIMESTAMPTZ8 字节带时区的日期和时间
INTERVAL16 字节时间间隔
postgres=# CREATE TABLE datetime_test (
id SERIAL PRIMARY KEY,
a_date DATE,
a_time TIME,
a_timestamp TIMESTAMP,
a_timestamptz TIMESTAMPTZ,
a_interval INTERVAL
);
CREATE TABLE
postgres=# INSERT INTO datetime_test (a_date, a_time, a_timestamp, a_timestamptz, a_interval)
postgres-# VALUES ('2024-01-01', '12:34:56', '2024-01-01 12:34:56', '2024-01-01 12:34:56+00',
postgres'# '3 years 4 months 1 day 2 hours 30 minutes 15 seconds');
INSERT 0 1
postgres=# \x on
扩展显示已打开
postgres=# SELECT a_date, a_time, a_timestamp, a_timestamptz, a_interval FROM datetime_test;
-[ RECORD 1 ]-+------------------------------
a_date | 2024-01-01
a_time | 12:34:56
a_timestamp | 2024-01-01 12:34:56
a_timestamptz | 2024-01-01 20:34:56+08
a_interval | 3 years 4 mons 1 day 02:30:15

日期加减与格式化:

postgres=# SELECT CURRENT_DATE + INTERVAL '10 days' AS plus_ten_days;
plus_ten_days
---------------------
2026-08-09 00:00:00
(1 行记录)

postgres=# SELECT to_char(a_timestamp, 'YYYY-MM-DD HH24:MI:SS') AS formatted_date FROM datetime_test;
formatted_date
---------------------
2024-01-01 12:34:56
(1 行记录)

布尔类型​

postgres=# CREATE TABLE halo_test (a BOOL, b BOOLEAN);
CREATE TABLE
postgres=# INSERT INTO halo_test VALUES ('y', 'n'), (TRUE, FALSE);
INSERT 0 2
postgres=# SELECT * FROM halo_test;
a | b
---+---
t | f
t | f
(2 行记录)

说明:PostgreSQL 的布尔值在输出时显示为 t(true)/ f(false),输入时接受 TRUE/FALSE、'y'/'n'、1/0 等多种写法。

二进制类型​

postgres=# CREATE TABLE bytea_test (
id SERIAL PRIMARY KEY,
data BYTEA
);
CREATE TABLE
postgres=# INSERT INTO bytea_test (data) VALUES ('\x48656C6C6F20576F726C64');
INSERT 0 1
postgres=# SELECT * FROM bytea_test;
id | data
----+--------------------------
1 | \x48656c6c6f20576f726c64
(1 行记录)

上例中 \x48656C6C6F20576F726C64 是 "Hello World" 的十六进制编码。

高级数据类型(JSON / XML)​

postgres=# CREATE TABLE halo_test (
json_data JSON,
xml_data XML
);
CREATE TABLE
postgres=# INSERT INTO halo_test VALUES (
postgres(# '{"name":"Halo","version":"16"}'::JSON,
postgres(# '<title>Halo Database</title>'::XML
postgres(# );
INSERT 0 1
postgres=# SELECT * FROM halo_test;
json_data | xml_data
--------------------------------+------------------------------
{"name":"Halo","version":"16"} | <title>Halo Database</title>
(1 行记录)