Data Types
- Describes the built-in data types available in
-
-
-
Postgres has a rich set of native data
types available to users.
- In the context of data types, the following sections will discuss
-
SQL standards compliance, porting issues, and usage.
-
- Some
Postgres types correspond directly to
-
SQL92-compatible types. In other
- cases, data types defined by
SQL92 syntax are mapped directly
- into native
Postgres types.
-
- Many of the built-in types have obvious external formats. However, several
- types are either unique to
Postgres,
- such as open and closed paths, or have
- several possibilities for formats, such as the date and time types.
+ shows all general-purpose data types
+ available to users. Most of the alternative names listed in the
+ Aliases
column are the names used internally by
+
Postgres for historical reasons. In
+ addition, some internally used or deprecated types are available,
+ but they are not documented here. Many of the built-in types have
+ obvious external formats. However, several types are either unique
+ to
Postgres, such as open and closed
+ paths, or have several possibilities for formats, such as the date
+ and time types.
-
tocentry="1">
+
id="datatype-table">
Data Types
|
+ Type Name
+ Aliases
Description
+
|
- bool
- boolean
- logical boolean (true/false)
+ bigint
+ int8
+ signed eight-byte integer
+
|
- box
+ bit
- rectangular box in 2D plane
+ fixed-length bit string
+
|
- char(n)
- character(n)
- fixed-length character string
+ bit varying(n)
+ varbit(n)
+ variable-length bit string
+
|
- cidr
-
- IP network address
+ boolean
+ <type>bool</entry>
+ logical boolean (true/false)
+
|
- circle
+ box
- circle in 2D plane
+ rectangular box in 2D plane
+
|
- date
- date
- calendar date without time of day
+ character(n)
+ char(n)
+ fixed-length character string
+
|
- decimal
- decimal(p,s)
- exact numeric with selectable precision
+ character varying(n)
+ varchar(n)
+ variable-length character string
+
|
- float4
- float(p), p < 7
- floating-point number with precision p
+ cidr
+
+ IP network address
+
|
- float8
- float(p), 7 <= p < 16
- floating-point number with precision p
+ circle
+
+ circle in 2D plane
+
|
- inet
+ date
- IP network or host address
+ calendar date (year, month, day)
+
|
- int2
- smallint
- signed two-byte integer
+ double precision
+ float8
+ double precision floating-point number
+
|
- int4
- int, integer
- signed 4-byte integer
+ inet
+
+ IP host address
+
|
- int8
-
- signed 8-byte integer
+ integer
+ <type>int, int4</entry>
+ signed four-byte integer
+
|
- interval
- interval
+ interval
+
general-use time span
+
|
- line
+ line
infinite line in 2D plane
+
|
- lseg
+ lseg
line segment in 2D plane
+
|
- money
- decimal(9,2)
- US-style currency
+ macaddr
+
+ MAC address
+
|
- numeric
- numeric(p,s)
- exact numeric with selectable precision
+ money
+
+ US-style currency
+
|
- path
-
- open and closed geometric path in 2D plane
+ numeric(p, s)
+ <type>decimal(p, s)</entry>
+ exact numeric with selectable precision
+
|
- point
+ oid
- geometric point in 2D plane
+ object identifier
+
|
- polygon
+ path
- closed geometric path in 2D plane
+ open and closed geometric path in 2D plane
+
|
- serial
+ point
- unique id for indexing and cross-reference
+ geometric point in 2D plane
+
|
- text
+ polygon
- variable-length character string
+ closed geometric path in 2D plane
+
|
- time
- time [ without time zone ]
- time of day
+ real
+ float4
+ single precision floating-point number
+
|
- timetz
- time with time zone
- time of day, including time zone
+ smallint
+ int2
+ signed two-byte integer
+
|
- timestamp
- timestamp [ with time zone ]
- date/time
+ serial
+
+ autoincrementing four-byte integer
+
|
- varchar(n)
- character varying(n)
+ text
+
variable-length character string
-
-
-
-
-
-
- The cidr and inet types are designed to handle any IP type
- but only ipv4 is handled in the current implementation.
- Everything here that talks about ipv4 will apply to ipv6 in a
- future release.
-
-
-
-
-
Postgres Function Constants
- Constants
-
-
- |
- date('now')
- current_date
- date of current transaction
+ time [ without time zone ]
+
+ time of day
+
|
- time('now')
- current_time
- time of current transaction
+ time with time zone
+
+ time of day, including time zone
+
|
- timestamp('now')
- current_timestamp
- date and time of current transaction
+ timestamp [ with time zone ]
+
+ date and time
-
Postgres has features at the forefront of
-
ORDBMS development. In addition to
-
SQL99 conformance, substantial portions
- of
SQL92 are also supported.
- Although we strive for
SQL92 compliance,
- there are some aspects of the standard
- that are ill considered and which should not live through subsequent standards.
-
Postgres will not make great efforts to
- conform to these features; however, these tend to apply in little-used
- or obsure cases, and a typical user is not likely to run into them.
-
+
+
Compatibility
+ The following types (or spellings thereof) are specified by SQL:
+ bit, bit varying, boolean,
+ char, character, character
+ varying, varchar, date,
+ double precision, integer,
+ interval, numeric, decimal,
+ real, smallint, time,
+ timestamp (both with or without time zone).
+
+
Most of the input and output functions corresponding to the
Some of the input and output functions are not invertible. That is,
the result of an output function may lose precision when compared to
the original input.
-
-
- Floating point numbers are allowed to retain
- most of the intrinsic precision of the type (typically 15 digits for doubles,
- 6 digits for 4-byte floats).
- Other types with underlying floating point fields (e.g. geometric
- types) carry similar precision.
-
-
- numeric-types">
+ datatype-numeric">
Numeric Types
Range
+
|
- decimal
- variable
- User-specified precision
- no limit
-
- |
- float4
- 4 bytes
- Variable-precision
- 6 decimal places
-
- |
- float8
- 8 bytes
- Variable-precision
- 15 decimal places
-
- |
- int2
+ smallint
2 bytes
Fixed-precision
-32768 to +32767
|
- int4
+ integer
4 bytes
Usual choice for fixed-precision
-2147483648 to +2147483647
|
- int8
+ bigint
8 bytes
Very large range fixed-precision
- ~18 decimal places
+ about 18 decimal places
+
+
+ |
+ decimal
+ variable
+ User-specified precision
+ no limit
|
numeric
User-specified precision
no limit
+
+ |
+ real
+ 4 bytes
+ Variable-precision
+ 6 decimal places
+
+ |
+ double precision
+ 8 bytes
+ Variable-precision
+ 15 decimal places
+
+
|
serial
4 bytes
- The int8 type may not be available on all platforms since
+ The bigint type may not be available on all platforms since
it relies on compiler support for eight-byte integers.
-
+ id="datatype-serial">
The Serial Type
- monetary-types">
+ datatype-money">
Monetary Type
- character-types">
+ datatype-character">
Character Types
-
SQL92 defines two primary character types:
- char and varchar.
+
SQL defines two primary character types:
+ character and character varying.
Postgres supports these types, in
addition to the more general text type,
- which unlike varchar
+ which unlike character varying
does not require an explicit declared upper
limit on the size of the field.
|
- "char"
- 1 byte
- Single character
+ character(n), char(n)
+ (4+n) bytes
+ Fixed-length blank padded
|
- char(n)
+ character varying(n), varchar(n)
(4+n) bytes
- Fixed-length blank padded
+ Variable-length with limit
|
text
Most flexible
Variable unlimited length
- |
- varchar(n)
- (4+n) bytes
- Variable-length with limit
-
+
+
+ Although the type text is not SQL-compliant, many
+ other RDBMS packages have it as well.
+
+
- There is one other fixed-length character type in
Postgres.
- The name type exists only for
- storage of internal catalog names and
- is not intended for use by the general user.
- Its length is currently defined as 32 bytes (31 characters plus terminator)
- but should be reference using NAMEDATALEN.
- The length is set at compile time (and is therefore adjustable for
- special uses); the default maximum length may change in a future release.
+ There are two other fixed-length character types in
+
Postgres. The
name type
+ exists only for storage of internal catalog
+ names and is not intended for use by the general user. Its length
+ is currently defined as 32 bytes (31 characters plus terminator)
+ but should be referenced using the macro
+ NAMEDATALEN. The length is set at compile time
+ (and is therefore adjustable for special uses); the default
+ maximum length may change in a future release. The type
+ "char" (note the quotes) is different from
+ char(1) in that it only uses one byte of storage. It
+ is internally used in the system catalogs as a poor-man's
+ enumeration type.
+ |
+ "char"
+ 1 byte
+ Single character internal type
+
|
name
32 bytes
- etime-types">
+ atype-datetime">
Date/Time Types
-
+ id="datatype-datetime-input">
Date/Time Input
- See endterm="datetime-appendix-title" linkend="datetime-appendix-title">
+ See linkend="datetime-appendix">
for the exact parsing rules of date/time input and for the recognized time zones.
- Refer to endterm="timezone-title" linkend="timezone"> for
+ Refer to linkend="datatype-timezone-table"> for
more examples of time zones.
-
timezone">
-
id="timezone-title">Postgres Time Zone Input
+
datatype-timezone-table">
+
Postgres Time Zone Input
Time Zone Inputs
-
+ id="datatype-datetime-output">
Date/Time Output
-
+ id="datatype-timezones">
Time Zones
-
+ id="datatype-datetime-internals">
Internals
- boolean-type">
+ datatype-boolean">
Boolean Type
-
Postgres supports
bool as
- the
SQL99 boolean type.
- bool can have one of only two states: 'true' or 'false'.
- A third state, 'unknown', is not
- implemented and is not suggested in
SQL99;
- effective substitute. bool can be used in any boolean expression,
- and boolean expressions
- always evaluate to a result compatible with this type.
+ boolean can have one of only two states: 'true' or
+ 'false'. A third state, 'unknown', is represented by the SQL NULL
+ state. boolean can be used in any boolean expression,
+ and boolean expressions always evaluate to a result compatible
+ with this type.
+
- bool uses 1 byte of storage.
+ boolean uses 1 byte of storage.
- geometric-types">
+ datatype-geometric">
Geometric Types
-
+ datatype-net-types">
Network Address Data Types
types, because these types offer input error checking and several
specialized operators and functions.
-
+
datatype-net-types-table">
Network Address Data Types
- inet-type">
+ datatype-inet">
inet
- cidr-type">
+ datatype-cidr">
cidr>
-
+ datatype-inet-vs-cidr">
inet vs cidr
- macaddr-type">
+ datatype-macaddr">
macaddr>>
+
+
Bit String Types
+
+ Bit strings are strings of 1's and 0's. They can be used to store
+ or visualize bit masks. There are two SQL bit types:
+ BIT(x) and BIT
+ VARYING(x); the
+ x specifies the maximum length.
+ BIT type data is automatically padded with 0's on the
+ right to the maximum length, BIT VARYING is of
+ variable length. BIT without length is requivalent
+ to BIT(1), BIT VARYING means
+ unlimited length. Input data that is longer than the allowed
+ length will be truncated. Refer to
+ linkend="sql-syntax-bit-strings"> for information about the syntax
+ of bit string constants. Bit-logical operators and string
+ manipulation functions are available; see
+ linkend="functions">.
+
+
+
+ Some examples:
+CREATE TABLE test (a BIT(3), b BIT VARYING(5));
+INSERT INTO test VALUES (B'101', B'00');
+SELECT SUBSTRING(b FROM 1 FOR 2) FROM test;
+
+
+
+
+
+
+
Functions and Operators
@ -5.0
5.0
+
+ |
+ &
+ Binary AND
+ 91 & 15
+ 11
+
+
+ |
+ |
+ Binary OR
+ 32 | 3
+ 35
+
+
+ |
+ #
+ Binary XOR
+ 17 # 5
+ 20
+
+
+ |
+ ~
+ Binary NOT
+ ~1
+ -2
+
+
+ |
+ <<
+ Binary shift left
+ 1 << 4
+ 16
+
+
+ |
+ >>
+ Binary shift right
+ 8 >> 2
+ 2
+
+
-
+ The binary
operators are also available for the bit
+ string types BIT and BIT VARYING.
+
+
+
Bit String Binary Operators
+
+
+
+ |
+ Example
+ Result
+
+
+
+
+ |
+ B'10001' & B'01101'
+ 00001
+
+ |
+ B'10001' | B'01101'
+ 11101
+
+ |
+ B'10001' # B'01101'
+ 11110
+
+ |
+ ~ B'10001'
+ 01110
+
+ |
+ B'10001' << 3
+ 01000
+
+ |
+ B'10001' >> 2
+ 00100
+
+
+
+
+
+ Bit string arguments to &, |,
+ and # must be of equal length. When bit
+ shifting, the original length of the string is preserved, as shown
+ here.
+
Mathematical Functions
wary of potential effects of the automatic padding when using the
CHARACTER type. Generally the functions described
here also work on data of non-string types by converting that data
- to a string representation first.
+ to a string representation first. Some functions also exist
+ natively for bit string types.
-
+ id="sql-syntax-bit-strings">
Bit String Constants