PostgreSQL has a rich set of native data types available to users. Users can also add new types to PostgreSQL using the CREATE TYPE command.
Table 2-1 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 PostgreSQL for historical reasons. In addition, some internally used or deprecated types are available, but they are not documented here.
Table 2-1. Data Types
Type Name | Aliases | Description |
---|---|---|
bigint | int8 | signed eight-byte integer |
bigserial | serial8 | autoincrementing eight-byte integer |
bit | fixed-length bit string | |
bit varying(n) | variable-length bit string | |
boolean | bool | logical Boolean (true/false) |
box | rectangular box in 2D plane | |
bytea | binary data | |
character(n) | char(n) | fixed-length character string |
character varying(n) | varchar(n) | variable-length character string |
cidr | IP network address | |
circle | circle in 2D plane | |
date | calendar date (year, month, day) | |
double precision | float8 | double precision floating-point number |
float(p) | real or double precision | Approximate numeric (floating point) with at least the specified precision |
float | float8 | Approximate numeric (floating point) with default precision |
inet | IP host address | |
integer | int, int4 | signed four-byte integer |
interval | general-use time span | |
line | infinite line in 2D plane | |
lseg | line segment in 2D plane | |
macaddr | MAC address | |
numeric(p, s) | decimal(p, s), dec(p, s) | exact numeric with selectable precision and scale |
numeric(p) | decimal(p), dec(p) | exact numeric with selectable precision (scale = 0) |
numeric | decimal, dec | exact numeric with default precision |
oid | object identifier | |
path | open and closed geometric path in 2D plane | |
point | geometric point in 2D plane | |
polygon | closed geometric path in 2D plane | |
real | float4 | single precision floating-point number |
serial | serial4 | autoincrementing four-byte integer |
smallint | int2 | signed two-byte integer |
text | variable-length character string | |
time [ (p) ] [ without time zone ] | time of day | |
time [ (p) ] with time zone | timetz | time of day, including time zone |
timestamp [ (p) ] without time zone | timestamp | date and time |
timestamp [ (p) ] [ with time zone ] | timestamptz | date and time, including time zone |
In the table above, in the description of numeric, scale refers to the number of digits of the fractional component.
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). |
Each data type has an external representation determined by its input and output functions. Many of the built-in types have obvious external formats. However, several types are either unique to PostgreSQL, such as open and closed paths, or have several possibilities for formats, such as the date and time types. Most of the input and output functions corresponding to the base types (for example, integers and floating-point numbers) do some error-checking. 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.
Some of the operators and functions (for example, addition and multiplication) do not perform run-time error-checking in the interests of improving execution speed.
Numeric types consist of two-, four-, and eight-byte integers, four- and eight-byte floating point numbers and fixed-precision decimals.
Table 2-2. Numeric Types
Type Name | Storage | Description | Range |
---|---|---|---|
smallint | 2 bytes | Fixed-precision | -32768 to +32767 |
integer | 4 bytes | Usual choice for fixed-precision | -2147483648 to +2147483647 |
bigint | 8 bytes | Very large range fixed-precision | -9223372036854775808 to 9223372036854775807 |
decimal | variable | User-specified precision | no limit |
double precision | 8 bytes | variable-precision, inexact | 15 decimal digits precision |
float(p) | real or double precision | Approximate numeric (floating point) with at least the specified precision | depends on machine implementation |
float | float8 | Approximate numeric (floating point) with default precision | depends on machine implementation |
numeric | variable | User-specified precision | no limit |
real | 4 bytes | Variable-precision | 6 decimal places |
serial | 4 bytes | Identifier or cross-reference | 0 to +2147483647 |
bigserial | 8 bytes | autoincrementing integer | 1 to 9223372036854775807 |
The syntax of constants for the numeric types is described in the Section called Constants in Chapter 1. The numeric types have a full set of corresponding arithmetic operators and functions. Refer to Chapter 3 for more information.
The types smallint, integer, bigint store whole numbers, that is, numbers without fractional components, of various ranges. Attempts to store values outside of the allowed range will result in an error.
The type integer is the usual choice, as it offers the best balance between range, storage size, and performance. The smallint type is generally only used if disk space is at a premium. The bigint type should only be used if the integer range is not sufficient, because the latter is definitely faster.
The bigint type may not function correctly on all platforms, since it relies on compiler support for eight-byte integers. On a machine without such support, bigint acts the same as integer (but still takes up eight bytes of storage). However, we are not aware of any reasonable platform where this is actually the case.
SQL only specifies the integer types integer (or int) and smallint. The type bigint, and the type names int2, int4, and int8 are extensions.
If you have a column of type smallint or bigint with an index, you may encounter problems getting the system to use that index. For instance, a clause of the form:
|
The type numeric can store numbers of practically unlimited size and precision, while being able to store all numbers and carry out all calculations exactly. It is especially recommended for storing monetary amounts and other quantities where exactness is required. However, the numeric type is very slow compared to the floating-point types described in the next section.
In discussing numerics we use the following terms: The scale of a numeric is the count of decimal digits in the fractional part, to the right of the decimal point. The precision of a numeric is the total count of significant digits in the whole number, that is, the number of digits to both sides of the decimal point. So the number 23.5141 has a precision of 6 and a scale of 4. Integers can be considered to have a scale of zero.
Both the precision and the scale of the numeric type can be configured. To declare a column of type numeric use the syntax
NUMERIC(precision, scale) |
NUMERIC(precision) |
NUMERIC |
If the precision or scale of a value is greater than the declared precision or scale of a column, the system will attempt to round the value. If the value cannot be rounded so as to satisfy the declared limits, an error is raised.
The types decimal and numeric are equivalent. Both types are part of the SQL standard.
The data types real and double precision are inexact, variable-precision numeric types. In practice, these types are usually implementations of IEEE 754 binary floating point (single and double precision, respectively), to the extent that the underlying processor, operating system, and compiler support it.
Inexact means that some values cannot be converted exactly to the internal format and are stored as approximations, so that storing and printing back out a value may show slight discrepancies. Managing these errors and how they propagate through calculations is the subject of an entire branch of mathematics and computer science and will not be discussed further here, except for the following points:
If you require exact storage and calculations (such as for monetary amounts), use the numeric type instead.
If you want to do complicated calculations with these types for anything important, especially if you rely on certain behavior in boundary cases (infinity, underflow), you should evaluate the implementation carefully.
Comparing two floating-point values for equality may or may not work as expected.
Normally, the real type has a range of at least -1E+37 to +1E+37 with a precision of at least 6 decimal digits. The double precision type normally has a range of around -1E+308 to +1E+308 with a precision of at least 15 digits. Values that are too large or too small will cause an error. Rounding may take place if the precision of an input number is too high. Numbers too close to zero that are not representable as distinct from zero will cause an underflow error.
The serial data types are not truly types, but are a notational convenience for setting up unique identifier columns in tables. In the current implementation, specifying:
CREATE TABLE tablename ( colname SERIAL ); |
CREATE SEQUENCE tablename_colname_seq; CREATE TABLE tablename ( colname integer DEFAULT nextval('tablename_colname_seq') UNIQUE NOT NULL ); |
The type names serial and serial4 are equivalent: both create integer columns. The type names bigserial and serial8 work just the same way, except that they create a bigint column. bigserial should be used if you anticipate use of more than 231 identifiers over the lifetime of the table.
The implicit sequence created for the serial type will not be not be automatically removed when the table is dropped. |
Implicit sequences supporting the serial are not automatically dropped when a table containing a serial type is dropped. So, the following commands executed in order will likely fail:
CREATE TABLE tablename (colname SERIAL); DROP TABLE tablename; CREATE TABLE tablename (colname SERIAL); |