Chapter 8. Data Types

Table of Contents
Numeric Types
Monetary Type
Character Types
Date/Time Types
Boolean Type
Geometric Types

Describes the built-in data types available in Postgres.

Postgres has a rich set of native data types available to users. Users may add new types to Postgres using the define type command described elsewhere.

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 date and time types.

Table 8-1. Postgres Data Types




boolbooleanlogical boolean (true/false)
boxrectangular box in 2D plane
char(n)character(n)fixed-length character string
circlecircle in 2D plane
datedatecalendar date without time of day
float4/8float(p)floating-point number with precision p
float8real, double precisiondouble-precision floating-point number
int2smallintsigned two-byte integer
int4int, integersigned 4-byte integer
int4decimal(p,s)exact numeric for p <= 9, s = 0
int4numeric(p,s)exact numeric for p == 9, s = 0
lineinfinite line in 2D plane
lsegline segment in 2D plane
moneydecimal(9,2)US-style currency
pathopen and closed geometric path in 2D plane
pointgeometric point in 2D plane
polygonclosed geometric path in 2D plane
timetimetime of day
timespanintervalgeneral-use time span
timestamptimestamp with time zonedate/time
varchar(n)character varying(n)variable-length character string

Table 8-2. Postgres Function Constants



getpgusername()current_useruser name in current session
date('now')current_datedate of current transaction
time('now')current_timetime of current transaction
timestamp('now')current_timestampdate and time of current transaction

Postgres has features at the forefront of ORDBMS development. In addition to SQL3 conformance, substantial portions of SQL92 are also supported. Although we strive for SQL92 compliance, there are some cases in the standard which are ill considered and which should not live through subsequent standards. Postgres will not make great efforts to conform to these cases. However, these cases tend to be little-used and obsure, and a typical user is not likely to run into them.

Although most of the input and output functions corresponding to the base types (e.g., integers and floating point numbers) do some error-checking, some are not particularly rigorous about it. More importantly, few of the operators and functions (e.g., addition and multiplication) perform any error-checking at all. Consequently, many of the numeric operators can (for example) silently underflow or overflow.

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.

Destroying a DatabaseUpNumeric Types