PostgreSQL字段类型说明

 

BIGSERIAL
SERIAL8 
存储自动递增的惟一整数,最多 8 字节。

BIT 
固定长度的位串。

BIT VARYING(n)
VARBIT(n) 
可变长度的位串,长度为 n 位。

BOOLEAN 
存储逻辑布尔值(true/false/unknown),可以是 TRUE、t、true、y、yes 和 1,或者 FALSE、f、false、n、no 和 0。

BYTEA 
用于存储大型二进制对象(比如图形)的原始二进制数据。使用的存储空间是 4 字节加上二进制串的长度。

CHAR(n)
CHARACTER(n) 
包含固定长度的字符串,用空格填充到长度 n。

DATE 
用 4 字节的存储空间存储日历日期(年、月、日)。

DATETIME 
存储日历日期和天内的时间。

DECIMAL(p,s)
NUMERIC(p,s) 
存储精确的数值,精度(p)和刻度(s)为 0 或更高。

FLOAT4
REAL 
存储浮点数,精度为 8 或更低和 6 个小数位。

FLOAT8
DOUBLE PRECISION 
存储浮点数,精度为 16 或更低和 15 个小数位。

SMALLINT 
存储有符号或无符号 2 字节整数。

INTEGER 
存储有符号或无符号 4 字节整数。

INT8
BIGINT 
存储有符号或无符号 8 字节整数。

SERIAL
SERIAL4 
存储自动递增的惟一整数值,最多 4 字节存储空间。

TEXT 
存储长度可变的大型字符串数据,最多 1 GB。PostgreSQL 自动压缩 TEXT 字符串。

TIME (WITHOUT TIME ZONE |
WITH TIME ZONE) 
存储天内的时间。如果不存储数据库服务器的时区,就使用 8 字节的存储空间;如果存储时区,就使用 12 字节。

TIMESTAMP (WITHOUT TIME ZONE |
WITH TIME ZONE) 
存储日期和时间。可以存储或不存储数据库服务器的时区,使用 8 字节存储空间。

VARCHAR(n)
CHARACTER VARYING(n)
CHARACTER VARYING 
存储可变长度的字符串,最大长度为 n。不存储末尾的空格。
 

 

  类似Oracle ,PostgreSQL也有强大的类型转换函数, 下面仅举两个类型转换例子。

 
 
 

      
--1 例子
postgres=# select 1/4;
 ?column? 
----------
        0
(1 row)

        在PG里如果想做除法并想保留小数,用上面的方法却行不通,因为"/" 运算结果为取整,并
且会截掉小数部分。
 

--2 类型转换
postgres=# select round(1::numeric/4::numeric,2);
 round 
-------
  0.25
(1 row)

  备注:类型转换后,就能保留小数部分了。


--3 也可以通过 cast 函数进行转换
postgres=# select round( cast ( 1 as numeric )/ cast( 4 as numeric),2);
 round 
-------
  0.25
(1 row)


--4 关于 cast 函数的用法
postgres=# SELECT substr(CAST (1234 AS text), 3,1);
 substr 
--------
 3
(1 row)


--5 附: PostgreSQL 类型转换函数

 

FunctionReturn TypeDescriptionExample


to_char
(timestamp, text

)


text
convert time stamp to string
to_char(current_timestamp, 'HH12:MI:SS')

to_char
(
interval, text
)

text
convert interval to string
to_char(interval '15h 2m 12s', 'HH24:MI:SS')

to_char
(
int, text
)

text
convert integer to string
to_char(125, '999')


to_char
(
double

precision
, text
)


text
convert real/double precision to string
to_char(125.8::real, '999D9')

to_char
(
numeric, text
)

text
convert numeric to string
to_char(-125.8, '999D99S')

to_date
(text, text
)

date
convert string to date
to_date('05 Dec 2000', 'DD Mon YYYY')

to_number
(
text, text
)

numeric
convert string to numeric
to_number('12,454.8-', '99G999D9S')

to_timestamp
(
text, text
)

timestamp with time zone
convert string to time stamp
to_timestamp('05 Dec 2000', 'DD Mon YYYY')

to_timestamp
(
double precision
)

timestamp with time zone
convert Unix epoch to time stamp
to_timestamp(1284352323)
 
 
1 varchar java.lang.String 12  
2 bpchar java.lang.String 1  
3 cidr java.lang.Object 1,111  
4 inet java.lang.Object 1,111  
5 macaddr java.lang.Object 1,111  
6 text java.lang.String 12  
7 int8 java.lang.Long -5  
8 int8 java.lang.Long -5  
9 box java.lang.Object 1,111  
10 circle java.lang.Object 1,111  
11 float8 java.lang.Double 8  
12 int4 java.lang.Integer 4  
13 interval java.lang.Object 1,111  
14 line java.lang.Object 1,111  
15 lseg java.lang.Object 1,111  
16 money java.lang.Double 8  
17 numeric java.math.BigDecimal 2  
18 path java.lang.Object 1,111  
19 point java.lang.Object 1,111  
20 polygon java.lang.Object 1,111  
21 float4 java.lang.Float 7  
22 int2 java.lang.Integer 5  
23 int4 java.lang.Integer 4  
24 time java.sql.Time 92  
25 timestamp java.sql.Timestamp 93  
26 bit java.lang.Boolean -7  
27 varbit java.lang.Object 1,111  
28 bool java.lang.Boolean -7  
29 bytea [B -2

原文地址:https://www.cnblogs.com/logsharing/p/8260129.html