PG有着最丰富的数据类型。我们看一下都有那种。摘自官网。
https://www.postgresql.org/docs/10/static/datatype.html
1. Numeric
Name | Storage Size | Description | Range |
smallint | 2 bytes | small-range integer | -32768 to +32767 |
integer | 4 bytes | typical choice for integer | -2147483648 to +2147483647 |
bigint | 8 bytes | large-range integer | -9223372036854775808 to +9223372036854775807 |
decimal | variable | user-specified precision, exact | up to 131072 digits before the decimal point; up to 16383 digits after the decimal point |
numeric | variable | user-specified precision, exact | up to 131072 digits before the decimal point; up to 16383 digits after the decimal point |
real | 4 bytes | variable-precision, inexact | 6 decimal digits precision |
double precision | 8 bytes | variable-precision, inexact | 15 decimal digits precision |
smallserial | 2 bytes | small autoincrementing integer | 1 to 32767 |
serial | 4 bytes | autoincrementing integer | 1 to 2147483647 |
bigserial | 8 bytes | large autoincrementing integer | 1 to 9223372036854775807 |
2. Monetary
Name | Storage Size | Description | Range |
money | 8 bytes | currency amount | -92233720368547758.08 to +92233720368547758.07 |
3. Character
Name | Description |
character varying(n), varchar(n) | variable-length with limit |
character(n), char(n) | fixed-length, blank padded |
text | variable unlimited length |
4. Binary Data
Name | Storage Size | Description |
bytea | 1 or 4 bytes plus the actual binary string | variable-length binary string |
5. Date/Time
Name | Storage Size | Description | Low Value | High Value | Resolution |
timestamp [ (p) ] [ without time zone ] | 8 bytes | both date and time (no time zone) | 4713 BC | 294276 AD | 1 microsecond |
timestamp [ (p) ] with time zone | 8 bytes | both date and time, with time zone | 4713 BC | 294276 AD | 1 microsecond |
date | 4 bytes | date (no time of day) | 4713 BC | 5874897 AD | 1 day |
time [ (p) ] [ without time zone ] | 8 bytes | time of day (no date) | 00:00:00 | 24:00:00 | 1 microsecond |
time [ (p) ] with time zone | 12 bytes | time of day (no date), with time zone | 00:00:00+1459 | 24:00:00-1459 | 1 microsecond |
interval [ fields ] [ (p) ] | 16 bytes | time interval | -178000000 years | 178000000 years | 1 microsecond |
6. Boolean
Name | Storage Size | Description |
boolean | 1 byte | state of true or false |
7. Enumerated
Enumerated (enum) types are data types that comprise a static, ordered set of values. They are equivalent to the enum types supported in a number of programming languages. An example of an enum type might be the days of the week, or a set of status values for a piece of data.
Example: create type mood as enum('sad', 'ok', 'happy');
8. Geometric
Name | Storage Size | Description | Representation |
point | 16 bytes | Point on a plane | (x,y) |
line | 32 bytes | Infinite line | {A,B,C} |
lseg | 32 bytes | Finite line segment | ((x1,y1),(x2,y2)) |
box | 32 bytes | Rectangular box | ((x1,y1),(x2,y2)) |
path | 16+16n bytes | Closed path (similar to polygon) | ((x1,y1),...) |
path | 16+16n bytes | Open path | [(x1,y1),...] |
polygon | 40+16n bytes | Polygon (similar to closed path) | ((x1,y1),...) |
circle | 24 bytes | Circle | <(x,y),r> (center point and radius) |
9. Network Address
Name | Storage Size | Description |
cidr | 7 or 19 bytes | IPv4 and IPv6 networks |
inet | 7 or 19 bytes | IPv4 and IPv6 hosts and networks |
macaddr | 6 bytes | MAC addresses |
macaddr8 | 8 bytes | MAC addresses (EUI-64 format) |
10. Bit String
Bit String是1和0的字符串,用来存储或可视化bit掩码。有两种SQL位类型:bit(n) 和bit (n),其中n是正整数。
bit类型数据必须精确匹配长度n;试图存储较短或更长的位串是错误的。可变长度的bit数据的可变到最大长度n;较长的字符串将被拒绝。没有长度的写入位相当于位(1),而没有长度规格的位变化意味着无限长。
Example: INSERT INTO test VALUES (B'10'::bit(3), B'101');
11. Text Search
PostgreSQL提供了两种数据类型,它们被设计为支持全文搜索,这是通过自然语言文档集合搜索以找到与查询最匹配的那些活动。tsvector类型表示以文本搜索优化的形式的文档;tsquery类型同样表示文本查询。
tsvector存储不同的词的排序列表,这些词被归一化以合并同一单词的不同变体。排序和重复消除是在输入过程中自动完成的,
mytest=# select ' '' '' a b c d a d c b e'::tsvector;
tsvector
-------------------------
' ' 'a' 'b' 'c' 'd' 'e'
(1 row)
tsquery存储要搜索的次,使用布尔运算符&, |, !和组合它们以及短语搜索操作符<>。
mytest=# select 'a & b &! c'::tsquery;
tsquery
------------------
'a' & 'b' & !'c'
(1 row)
12. UUID
存储Universally Unique Identifiers (UUID)。该标识符是由选择的算法生成的128位相同的标识符不太可能由已知的宇宙中的任何其他人使用相同的算法生成。
对于分布式系统,这些标识符提供了比序列生成器更好的唯一性保证,这些序列生成器在单个数据库中是唯一的。
UUID被写为一个小写的十六进制数字序列,在几个由连字符分开的组中,具体地是一组8位数字,后面是三组4位数字,后面是一组12位数字,总共代表128位的32位数字。
例如:a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11
13. XML
存储XML数据。
它在文本字段中存储XML数据的优点在于,它检查输入值的正确性,并且有支持函数对其执行类型安全操作。
使用此数据类型需要安装的时候配置 --with-libxml。
可以存储由XML标准定义的格式良好的“文档”,以及由XML定义的“内容”片段。XML标准中的内容。粗略地说,这意味着内容片段可以有不止一个顶层元素或字符节点。XMLValk为文档的表达式可用于评估特定的XML值是否为完整文档或仅为内容片段。
14. JSON
存储JSON (JavaScript Object Notation)。
JSON包括: json and jsonb.
15. Arrays
16. Composite
17. Range
18. Object Identifier
19. pg_lsn
存储 LSN (Log Sequence Number)数据, 64-bit 的数字。存储的是WAL的位置点。
20. Pseudo-Types
Name | Description |
any | Indicates that a function accepts any input data type. |
anyelement | Indicates that a function accepts any data type. |
anyarray | Indicates that a function accepts any array data type. |
anynonarray | Indicates that a function accepts any non-array data type. |
anyenum | Indicates that a function accepts any enum data type. |
anyrange | Indicates that a function accepts any range data type. |
cstring | Indicates that a function accepts or returns a null-terminated C string. |
internal | Indicates that a function accepts or returns a server-internal data type. |
language_handler | A procedural language call handler is declared to return language_handler. |
fdw_handler | A foreign-data wrapper handler is declared to return fdw_handler. |
index_am_handler | An index access method handler is declared to return index_am_handler. |
tsm_handler | A tablesample method handler is declared to return tsm_handler. |
record | Identifies a function taking or returning an unspecified row type. |
trigger | A trigger function is declared to return trigger. |
event_trigger | An event trigger function is declared to return event_trigger. |
pg_ddl_command | Identifies a representation of DDL commands that is available to event triggers. |
void | Indicates that a function returns no value. |
unknown | Identifies a not-yet-resolved type, e.g. of an undecorated string literal. |
opaque | An obsolete type name that formerly served many of the above purposes. |