SQL is considered a strongly typed language . This means that any piece of data represented by PostgreSQL has an associated data type, even if it is not plainly obvious. A data value's type both defines and constrains the kinds of operations which may be performed it.
Not only is every piece of data associated with a type, but types play a large part in the construction of tables. As stated in the section called Introduction to Relational Databases , tables are made up of one or more columns. These columns must, in addition to having a name, have a specific data type.
|Extending Data Types|
While PostgreSQL provides a wide variety of built-in data types, you also have the option to add new data types to PostgreSQL using the CREATE TYPE command. See Chapter 5 for more information on this extensibility.
Table 3-9 lists the data types officially supported by PostgreSQL, as well as any PostgreSQL recognized aliases (alternative names which are identical in connotation). There are many other internal (meaning they are no longer intended for normal use) or deprecated (outdated, and discouraged) data types available which are unlisted.
Additionally, while most of the data types implemented in PostgreSQL are directly derived from SQL standards, there are some actively maintained data types which are non-standard (such as the geometric and spacial types). Whether or not you are able to find equivalent types on other SQL-capable database management systems is therefore highly subjective.
Table 3-9. PostgreSQL Supported Data Types
Boolean and Binary Types
boolean , bool
A single true or false value.
An n-length bit string (exactly n binary bits).
bit varying(n) , varbit(n)
A variable n -length bit string (up to n binary bits)
character (n) , char(n)
A fixed n -length character string.
character varying(n) , varchar(n)
A variable length character string of up to n characters.
Variable length character string, of unlimited length.
smallint , int2
A signed two-byte integer.
integer , int , int4
A signed, fixed-precision four-byte number.
bigint , int8
A signed 8-byte integer, up to 18 digits in length.
real , float4
A single precision floating-point number.
double precision , float8 , float
An 8-byte floating-point number.
numeric(p,s) , decimal(p, s)
An exact numeric type with arbitrary precision p , and scale s .
A fixed precision, US-style currency.
An auto-incrementing four-byte integer.
Date and Time Types
The calendar date (day, month and year).
The time of day.
time with time zone
The time of day, including time zone information.
timestamp [with time zone]
Both the date and time.
An arbitrarily specified length of time.
A rectangular box in a two-dimensional plane.
infinite line in a 2D plane
finite line segment in a 2D plane
a circle with center and radius
Open and closed geometric paths in a two-dimensional plane.
geometric point in a 2D plane
closed geometric path in a 2D plane
An IP network specification
A network IP address, with optional subnet bits
A MAC address (e.g., an Ethernet card's hardware address)
An object (row) identifier
A transaction identifier
Remaining true to theme, the following sections on data types will describe in further detail each of the most widely used and practical types. This book will not go into detail on the non-standard and/or more esoteric types, such as the geometric, network and bitwise types.
These sections include information on valid usage, storage considerations, input and output formats and general syntactic conventions. Before we go much further on specific data types, however, a couple of topics worth discussing first are type coercion and the NULL keyword.