Like all database systems, PostgreSQL
lets you store data using a variety of different data types. These data types
allow the database engine to optimize its use of memory and storage, and to
perform operations on the stored data more efficiently and with fewer errors.
Data type selection plays an important role in how
efficiently your RDBMS functions, and so it’s important to be fully aware of
the options available to you, and to select the most appropriate data type for
your storage needs. That’s where this document comes in. It outlines the most
important data types supported by PostgreSQL, describing when and how each
should be used, and provides you with a ready supply of choices the next time
you sit down to optimize your existing databases or create new ones.
Table A
Data Type |
Description |
Bytes Used |
Recommended Use |
BOOLEAN |
Logical |
1 |
Storing attributes that Examples: Enable/disable, yes/no fields |
SMALLINT |
Integer |
2 |
Storing relatively small Examples: Age, quantity |
INTEGER |
Integer |
4 |
Storing medium integer Example: Distance |
BIGINT |
Extremely |
8 |
Storing large integer Example: Scientific/mathematical values |
FLOAT |
Floating-point |
4 |
Storing decimal values Examples: Measurement, temperature |
NUMERIC |
Floating-point |
Variable |
Storing decimal values Examples: Currency amounts, scientific values |
SERIAL |
Auto-incrementing |
4 |
Automatically numbering Example: Table primary keys |
CHAR |
Fixed-length |
4 + specified |
Storing string values Examples: Airline, country or post codes |
VARCHAR |
Variable-length |
Variable; 4 |
Storing string values of Examples: Names, passwords, short text labels |
TEXT |
Variable-length |
Variable |
Storing large blocks of Examples: News stories, product descriptions |
BYTEA |
Binary |
Variable; 4 |
Storing binary data Examples: Images, attachments, binary documents |
DATE |
Date |
4 |
Storing dates Examples: Birthdays, product expiry dates |
TIME |
Time |
8
|
Storing times Example: Alarms |
TIMESTAMP |
Combined |
8
|
Recording time instants Examples: Event triggers, “last log-in” |
INTERVAL |
Interval |
12
|
Storing durations Examples: Interval between two timestamps, task |
OID |
PostgreSQL |
4 |
Identifying table records Example: Table primary keys |
For a complete list and detailed descriptions, see the PostgreSQL manual.