PostgreSQL RDBMS: Data types
PostgreSQL has a rich set of data types available to users.
Users can add new types to PostgreSQL using the CREATE TYPE
command.
CREATE TYPE book_genre AS ENUM (
'Adventure',
'Biography',
'Comedy',
'Crime',
'Drama',
'Fantasy',
'Historical Fiction',
'Horror',
'Mystery',
'Poetry',
'Romance',
'Science Fiction',
'Self-Help',
'Thriller',
'Young Adult'
);
Name |
Aliases |
Description |
---|---|---|
|
|
signed eight-byte integer |
|
|
auto incrementing eight-byte integer |
|
fixed-length bit string |
|
|
|
variable-length bit string |
|
|
logical Boolean (true/false) |
|
rectangular box on a plane |
|
|
binary data (“byte array”) |
|
|
|
fixed-length character string |
|
|
variable-length character string |
|
IPv4 or IPv6 network address |
|
|
circle on a plane |
|
|
|
double precision floating-point number (8 bytes) |
|
calendar date (year, month, day) |
|
|
IPv4 or IPv6 host address |
|
|
|
signed four-byte integer |
|
time span |
|
|
textual JSON data |
|
|
binary JSON data, decomposed |
|
|
infinite line on a plane |
|
|
line segment on a plane |
|
|
MAC (Media Access Control) address |
|
|
MAC (Media Access Control) address (EUI-64 format) |
|
|
currency amount |
|
|
|
exact numeric of selectable precision |
|
geometric path on a plane |
|
|
PostgreSQL Log Sequence Number |
|
|
user-level transaction ID snapshot |
|
|
geometric point on a plane |
|
|
closed geometric path on a plane |
|
|
|
single precision floating-point number (4 bytes) |
|
|
signed two-byte integer |
|
|
auto incrementing two-byte integer |
|
|
auto incrementing four-byte integer |
|
variable-length character string |
|
|
time of day (no time zone) |
|
|
|
time of day, including time zone |
|
date and time (no time zone) |
|
|
|
date and time, including time zone |
|
text search query |
|
|
text search document |
|
|
user-level transaction ID snapshot (deprecated; see pg_snapshot) |
|
|
universally unique identifier |
|
|
XML data |
Types casting
SQL is a strong type language. That is, every data item has an associated data type which determines its behavior and allowed usage. PostgreSQL has an extensible type system that is more general and flexible than other SQL implementations.
You can cast some types in place using ::
notation, like:
SELECT '42'::INT;
SELECT '24.5'::REAL;
SELECT 'today'::DATE;
SELECT 'tomorrow'::TIMESTAMP;
Numeric types
Numeric types consist of two-, four-, and eight-byte integers, four- and eight-byte floating-point numbers, and selectable-precision decimals.
Type types smallint
, integer
and bigint
store whole numbers, that
is, numbers without fractional component, of various ranges. Attempts to store
values outside of the allowed range will result an error.
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 auto incrementing integer |
1 to 32767 |
serial |
4 bytes |
auto incrementing integer |
1 to 2147483647 |
bigserial |
8 bytes |
large auto incrementing integer |
1 to 9223372036854775807 |
Character types
Name |
Description |
---|---|
|
variable-length with limit |
|
fixed-length, blank-padded |
|
variable unlimited length, blank-trimmed |
|
variable-unlimited length |
SQL defines two primary character types: character varying(n)
and
character(n)
, where n
is a positive integer. Both of these types can
store string up to n
characters length. An attempt to store a longer string
into a column of these types will result an error, unless all the excess
characters are all spaces, in which case the string will be truncated to
the maximum length. However, if one explicitly casts a value to varchar(n)
or char(n)
, then an over-length value will be truncated to n
characters
without raising an error. If the string to be stored is shorter than the
declared length, value of type char
will be space-padded; values of
varchar
will simply store the shorter string.
In addition postgres provides the text
type, which stores strings of any
length.
If specified, the length n
must be greater than 0 and cannot exceed
10,485,760. If character varying
is used without length specifier,
the type accepts strings of any length. If bpchar
lacks a length specifier,
it also accepts strings of any length, but trailing spaces are semantically
insignificant. If character
lacks a specifier, it is equivalent to
character(1)
.
Date/Time types
PostgreSQL supports the full set of SQL date and time types. Dates are counted according to the Gregorian calendar, even in years before that calendar was introduced.
time
, timestamp
, and interval
accept an optional precision value,
which specifies the number of fractional digits retained in the seconds field.
By default, there is no explicit bound on precision. The allowed of this value
is from 0 to 6.
The interval
type has an additional option, which is to restrict the of
stored fields by writing one of these phrases:
YEAR
MONTH
DAY
HOUR
MINUTE
SECOND
YEAR TO MONTH
DAY TO HOUR
DAY TO MINUTE
DAY TO SECOND
HOUR TO MINUTE
HOUR TO SECOND
MINUTE TO SECOND
Dates
Some possible inputs for the date
type.
Example |
Description |
---|---|
|
ISO 8601; January 8 in any mode (recommended format) |
|
unambiguous in any date style input mode |
|
January 8 in MDY mode; August 1 in DMY mode |
|
January 18 in MDY mode; rejected in other modes |
|
January 2, 2003 in MDY mode; February 1, 2003 in DMY mode; February 3, 2001 in YMD mode |
|
January 8 in any mode |
|
January 8 in YMD mode, else error |
|
January 8, except error in YMD mode |
|
ISO 8601; January 8, 1999 in any mode |
|
year and day of year |
|
Julian date |
|
year 99 BC |
Times
Some possible inputs for the time
type.
Example |
Description |
---|---|
|
ISO 8601 |
|
same as 04:05; AM does not affect value |
|
same as 16:05; input hour must be <= 12 |
|
ISO 8601, with time zone as UTC offset |
|
ISO 8601, with fractional-hour time zone as UTC offset |
|
UTC offset specified to seconds (not allowed in ISO 8601) |
|
time zone specified by abbreviation |
|
time zone specified by full |
Time zone input
Example |
Description |
---|---|
|
Abbreviation (for Pacific Standard Time) |
|
Full time zone name |
|
POSIX-style time zone specification |
|
UTC offset for PST |
|
UTC offset for PST (ISO 8601 extended format) |
|
UTC offset for PST (ISO 8601 basic format) |
|
UTC offset for PST (ISO 8601 basic format) |
|
Military abbreviation for UTC |
|
Short form of zulu (also in ISO 8601) |
Special date/time inputs
Input String |
Valid Types |
Description |
---|---|---|
epoch |
|
1970-01-01 00:00:00+00 (Unix system time zero) |
infinity |
|
later than all other time stamps |
-infinity |
|
earlier than all other time stamps |
now |
|
current transaction’s start time |
today |
|
midnight (00:00) today |
tomorrow |
|
midnight (00:00) tomorrow |
yesterday |
|
midnight (00:00) yesterday |
allballs |
|
00:00:00.00 UTC |
Intervals
interval
values can be written using the following verbose syntax:
[@] quantity unit [quantity unit...] [direction]
Intervals are useful when you need to move timestamp back in past or to the future.
Abbreviation |
Meaning |
---|---|
Y |
Years |
M |
Months |
W |
Weeks |
D |
Days |
H |
Hours |
M |
Minutes |
S |
Seconds |
SELECT 'yesterday'::TIMESTAMP + INTERVAL '7d ago';
SELECT 'yesterday'::TIMESTAMP - INTERVAL '7' DAY;
Boolean type
PostgreSQL provides the standard SQL type boolean
. The boolean
type can
have several states: true, false, and a third state, unknown, which
is represented by the SQL NULL
value.
The datetype input function for type boolean
accepts these strings
representations for the true state:
true
yes
on
1
and these representations for the false state:
false
no
off
0
The key words TRUE
or FALSE
are preferred (SQL-compliant) method for
writing Boolean constants in SQL queries. But, you can also use string
representations by following the generic string-literal constant syntax, for
example 'yes'::boolean
.
Enumerated types
Enumerated (enums) 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 might be the days of the week,
or a set of status values for a piece of data.
Enum types are created using the CREATE TYPE
command; and once created,
the enum type can be used in table in function definitions, much like other
types:
CREATE TYPE WORK_DAY AS ENUM ('mon', 'tue', 'wed', 'thu', 'fri');
CREATE TABLE work_log
(
amount INTEGER,
date DATE,
day WORK_DAY
);
Ordering
The ordering of the values in an enum type is the order in which the values were listed when the type was created. All standard comparison operators and related aggregate functions are supported for enums. However, remember that each enumerated data type is separated and cannot be compared with other enumerated types.