暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

postgresql数据库数据类型(一)

skylines 2025-11-26
27
这期主要分享一下postgresql数据类型的一些体验,主要先围绕数值类型、字符类型和时间日期类型,这三种最常用的数据类型进行展开。
1、数值类型
数值类型由 2 字节、4 字节或 8 字节的整数以及 4 字节或 8 字节的浮点数和可选精度的十进制数组成。
名字
存储长度
描述
范围
smallint
2 字节
小范围整数
-32768 到 +32767
integer
4 字节
常用的整数
-2147483648 到 +2147483647
bigint
8 字节
大范围整数
-9223372036854775808 到 +9223372036854775807
decimal
可变长
用户指定的精度,精确
小数点前 131072 位;小数点后 16383 位
numeric
可变长
用户指定的精度,精确
小数点前 131072 位;小数点后 16383 位
real
4 字节
可变精度,不精确
6 位十进制数字精度
double precision
8 字节
可变精度,不精确
15 位十进制数字精度
smallserial
2 字节
自增的小范围整数
1 到 32767
serial
4 字节
自增整数
1 到 2147483647
money
8 字节
货币金额
-92233720368547758.08 到 +92233720368547758.07
    --创建测试表
    create table tabnumber_test(col1 smallint,col2 integer,col3 bigint,col4 decimal,col5 numeric,col6 real,col7 double precision,col8 smallserial,col9 serial,col10 bigserial,col11 money);


    --查看表结构
    mytest=# \d tabnumber_test;
                                      Table "public.tabnumber_test"
     Column |       Type       | Collation | Nullable |                    Default
    --------+------------------+-----------+----------+-----------------------------------------------
     col1   | smallint         |           |          |
     col2   | integer          |           |          |
     col3   | bigint           |           |          |
     col4   | numeric          |           |          |
     col5   | numeric          |           |          |
     col6   | real             |           |          |
     col7   | double precision |           |          |
     col8   | smallint         |           | not null | nextval('tabnumber_test_col8_seq'::regclass)
     col9   | integer          |           | not null | nextval('tabnumber_test_col9_seq'::regclass)
     col10  | bigint           |           | not null | nextval('tabnumber_test_col10_seq'::regclass)
     col11  | money            |           |          |


    mytest=#


    --插入测试数据
    insert into tabnumber_test(col1,col2,col3,col4,col5,col6,col7,col11) values(32767,32768,2147483648,32768.56,32768.56,35.78,39.12,100.30);
    insert into tabnumber_test(col1,col2,col3,col4,col5,col6,col7,col11) values(32766,32766,2147483646,32768.56,32768.31,35.78,40.12,110.31);


    --第一个数据超出长度暴多
    mytest=insert into tabnumber_test(col1,col2,col3,col4,col5,col6,col7,col11) values(32768,32768,2147483648,32768.56,32768.56,35.78,39.12,100.30);
    ERROR:  smallint out of range


    mytest=insert into tabnumber_test(col1,col2,col3,col4,col5,col6,col7,col11) values(32767,32768,2147483648,32768.56,32768.56,35.78,39.12,100.30);
    INSERT 0 1


    mytest=select * from tabnumber_test;
     col1  | col2  |    col3    |   col4   |   col5   | col6  | col7  | col8 | col9 | col10 |  col11
    -------+-------+------------+----------+----------+-------+-------+------+------+-------+---------
     32767 | 32768 | 2147483648 | 32768.56 | 32768.56 | 35.78 | 39.12 |    1 |    1 |     1 | $100.30
    (1 row)


    mytest=select * from tabnumber_test;
     col1  | col2  |    col3    |   col4   |   col5   | col6  | col7  | col8 | col9 | col10 |  col11
    -------+-------+------------+----------+----------+-------+-------+------+------+-------+---------
     32767 | 32768 | 2147483648 | 32768.56 | 32768.56 | 35.78 | 39.12 |    1 |    1 |     1 | $100.30
     32766 | 32766 | 2147483646 | 32768.56 | 32768.31 | 35.78 | 40.12 |    2 |    2 |     2 | $110.31
    (2 rows)


    --查看数据
    mytest=select * from tabnumber_test;
    -[ RECORD 1 ]-----
    col1  | 32767
    col2  | 32768
    col3  | 2147483648
    col4  | 32768.56
    col5  | 32768.56
    col6  | 35.78
    col7  | 39.12
    col8  | 1
    col9  | 1
    col10 | 1
    col11 | $100.30

    从上面可以看到,smallint范围比较小,serial类型其实就是自增序列。money类型是专门用来记录货币类型,数值部分与numeric一样,前者就比后者多了一个货币符号‘$’。

    2、字符类型

    序号
    名字 & 描述
    长度(字符)
    1
    character varying(n), varchar(n)
    10485760
    变长,有长度限制
    2
    character(n), char(n)
    10485760
    定长,不足补空白
    3
    bpchr,bpchar(n)
    无限制
    无限制,去掉多余空白
    4
    text
    无限制
    变长,无长度限制
      --创建测试表
      create table tabchar_test(col1 varchar(20),col2 char(20),col3 bpchar,col4 bpchar(20),col5 text);


      --插入测试数据
      insert into tabchar_test values('haha','caca','dada   ','papa ','mama');
      insert into tabchar_test values('gaga','nana','sasa','rara ','lala');
      --查看表结构
      mytest=# \d tabchar_test;
                         Table "public.tabchar_test"
       Column |         Type          | Collation | Nullable | Default
      --------+-----------------------+-----------+----------+---------
       col1   | character varying(20|           |          |
       col2   | character(20)         |           |          |
       col3   | bpchar                |           |          |
       col4   | character(20)         |           |          |
       col5   | text                  |           |          |


      mytest=#


      --查看表数据
      mytest=select * from tabchar_test;
       col1 |         col2         |  col3   |         col4         | col5
      ------+----------------------+---------+----------------------+------
       haha | caca                 | dada    | papa                 | mama
      (1 row)


      --查看字段字符串长度
      mytest=#
      mytest=select length(col1),length(col2),length(col3),length(col4),length(col5) from tabchar_test;
       length | length | length | length | length
      --------+--------+--------+--------+--------
            4 |      4 |      4 |      4 |      4
      (1 row)


      mytest=insert into tabchar_test values('gaga','nana','sasa','rara ','lala');
      INSERT 0 1
      mytest=#
      mytest=select * from tabchar_test;
       col1 |         col2         |  col3   |         col4         | col5
      ------+----------------------+---------+----------------------+------
       haha | caca                 | dada    | papa                 | mama
       gaga | nana                 | sasa    | rara                 | lala
      (2 rows)


      mytest=select length(col1),length(col2),length(col3),length(col4),length(col5) from tabchar_test;
       length | length | length | length | length
      --------+--------+--------+--------+--------
            4 |      4 |      4 |      4 |      4
            4 |      4 |      4 |      4 |      4
      (2 rows)

      postgresql数据库字符类型,字符的长度,实际都还是计算非空字符的长度,bpchar(n)实际与char(n)是一样的。

      3、日期时间类型
      名字
      存储空间
      描述
      最低值
      最高值
      分辨率
      timestamp [ (p) ] [ without time zone ]
      8 字节
      日期和时间(无时区)
      4713 BC
      294276 AD
      1 毫秒 14 位
      timestamp [ (p) ] with time zone
      8 字节
      日期和时间,有时区
      4713 BC
      294276 AD
      1 毫秒 14 位
      date
      4 字节
      只用于日期
      4713 BC
      5874897 AD
      1 天
      time [ (p) ] [ without time zone ]
      8 字节
      只用于一日内时间
      00:00:00
      24:00:00
      1 毫秒 14 位
      time [ (p) ] with time zone
      12 字节
      只用于一日内时间,带时区
      00:00:00+1459
      24:00:00-1459
      1 毫秒 14 位
      interval [ fields ] [ (p) ]
      12 字节
      时间间隔
      -178000000 年
      178000000 年
      1 毫秒 14 位
        --创建测试表
        create table tabtime_test(col1 timestamp,col2 timestamp with time zone,col3 date,col4 time,col5 time with time zone,col6 interval);


        --插入测试数据
        insert into tabtime_test values(now(),now(),now(),now(),now(),INTERVAL '1 month');
        insert into tabtime_test values(now(),now(),now(),now(),now(),now()+INTERVAL '1 month' - CURRENT_DATE);
        insert into tabtime_test values(now(),now(),now(),now(),now(),CURRENT_DATE+INTERVAL '1 month' - now());


        --查看表结构
        mytest=# \d tabtime_test;
                              Table "public.tabtime_test"
         Column |            Type             | Collation | Nullable | Default
        --------+-----------------------------+-----------+----------+---------
         col1   | timestamp without time zone |           |          |
         col2   | timestamp with time zone    |           |          |
         col3   | date                        |           |          |
         col4   | time without time zone      |           |          |
         col5   | time with time zone         |           |          |
         col6   | interval                    |           |          |


        mytest=#




        --查看表数据
        mytest=select * from tabtime_test;
                    col1            |             col2              |    col3    |      col4       |        col5        | col6
        ----------------------------+-------------------------------+------------+-----------------+--------------------+-------
         2025-11-25 16:29:36.503391 | 2025-11-25 16:29:36.503391+08 | 2025-11-25 | 16:29:36.503391 | 16:29:36.503391+08 | 1 mon
        (1 row)


        mytest=select * from tabtime_test;
                    col1            |             col2              |    col3    |      col4       |        col5        |          col6
        ----------------------------+-------------------------------+------------+-----------------+--------------------+-------------------------
         2025-11-25 16:29:36.503391 | 2025-11-25 16:29:36.503391+08 | 2025-11-25 | 16:29:36.503391 | 16:29:36.503391+08 | 1 mon
         2025-11-25 16:38:05.764498 | 2025-11-25 16:38:05.764498+08 | 2025-11-25 | 16:38:05.764498 | 16:38:05.764498+08 | 30 days 16:38:05.764498
        (2 rows)


        mytest=select * from tabtime_test;
                    col1            |             col2              |    col3    |      col4       |        col5        |          col6
        ----------------------------+-------------------------------+------------+-----------------+--------------------+-------------------------
         2025-11-25 16:29:36.503391 | 2025-11-25 16:29:36.503391+08 | 2025-11-25 | 16:29:36.503391 | 16:29:36.503391+08 | 1 mon
         2025-11-25 16:38:05.764498 | 2025-11-25 16:38:05.764498+08 | 2025-11-25 | 16:38:05.764498 | 16:38:05.764498+08 | 30 days 16:38:05.764498
         2025-11-25 16:42:28.060743 | 2025-11-25 16:42:28.060743+08 | 2025-11-25 | 16:42:28.060743 | 16:42:28.060743+08 | 29 days 07:17:31.939257


        --查看倒计时(附加)
        mytest=select to_char('2026-1-1 00:00:00'::timestamp - now(),'DD hh24:mi:ss')::INTERVAL;
             to_char
        ------------------
         36 days 06:25:28
        (1 row)
        以上就是对postgresql数据库数值、字符和日期时间三种数据类型的体验,进行了简单的测试,稍微体验一下在实际中的应用情况。

        文章转载自skylines,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

        评论