Skip to main content

Data Types

Updated Oct 11, 2019 ·

Most Common Data Types

PostgreSQL offers several common data types, many of which align with SQL standards and are found in other database systems:

  • Text: allows character strings of any length.
  • Varchar/Char: sets a maximum length or a fixed length for character strings.
  • Boolean: supports true/false values and can also include a "NULL" for unknown states.
  • Numeric: supports numbers with arbitrary precision.
  • Integer: handles whole numbers within a specified range.
  • Bigint: accommodates larger numbers beyond the integer range.

Here is a table format:

NameTypeAliasesDescription
integerNumericint, int4Stores whole numbers
bigintNumericint8Stores large whole numbers
smallintNumericint2Stores small whole numbers
decimalNumericnumericStores exact numeric values with arbitrary precision
numericNumericdecimalStores exact numeric values with arbitrary precision
realNumericfloat4Stores floating-point numbers with single precision
double precisionNumericfloat8Stores floating-point numbers with double precision
char(n)CharactercharacterStores fixed-length strings
varchar(n)Charactercharacter varyingStores variable-length strings with a limit
textCharacter-Stores variable-length strings with no specific length limit
dateDate and Time-Stores dates
timeDate and Time-Stores time of day
timestampDate and Time-Stores date and time
timestamptzDate and Time-Stores date and time with time zone
booleanBooleanboolStores truth values (TRUE, FALSE)
byteaBinary-Stores binary data (e.g., images, files)
jsonJSON-Stores JSON data
jsonbJSON-Stores JSON data in a binary format for faster queries
integer[]Array-Array of integers
text[]Array-Array of text strings
pointGeometric-Stores a point in a 2D plane
lineGeometric-Stores a line defined by the general linear equation
circleGeometric-Stores a circle with a center and radius
cidrNetwork-Stores IPv4 or IPv6 network addresses
inetNetwork-Stores individual IP addresses
uuidUUID-Stores universally unique identifiers
moneyMoney-Stores currency amounts

Text Data Types

Text data types in PostgreSQL are used to store and manage various types of text information. The three main text data types are TEXT, VARCHAR, and CHAR. Each type serves different purposes depending on the nature of the text data you want to store.

TEXT

The TEXT data type is the most flexible option for storing text in PostgreSQL.

  • Suitable for text data of unknown or varying length
  • Can store from 0 characters up to potentially millions
  • Ideal for storing large or unrestricted text, like descriptions, feedback, or content

Example:

CREATE TABLE customers (
customer_id SERIAL PRIMARY KEY,
notes TEXT -- Comments about the customer
);

VARCHAR

The VARCHAR data type is similar to TEXT but allows for specifying a maximum length.

  • Stores variable-length text up to a defined maximum
  • Restriction can be imposed on column values: VARCHAR(N), where "N" is the maximum length
  • An error is generated if the input exceeds the defined length
  • Useful for fields where length constraints are necessary, such as usernames or titles

Example:

CREATE TABLE customers (
customer_id SERIAL PRIMARY KEY,
first_name VARCHAR(50), -- First name limited to 50 characters
last_name VARCHAR(50), -- Last name limited to 50 characters
notes TEXT -- Comments about the customer
);

CHAR

The CHAR data type is used for fixed-length text.

  • Stores text of a specific, fixed length
  • If input is shorter than defined length, spaces are added to reach the required length
  • Suitable for standard-length data like codes, identifiers, or predefined formats

Example:

CREATE TABLE customers (
customer_id SERIAL PRIMARY KEY,
first_name VARCHAR(50), -- First name limited to 50 characters
last_name VARCHAR(50), -- Last name limited to 50 characters
middle_initial CHAR, -- Middle initial; single character
zip_code CHAR(5), -- Fixed 5-character string
notes TEXT -- Comments about the customer
);

Numeric Data Types

When structuring a database, selecting the correct numeric data type ensures data is stored efficiently and accurately. PostgreSQL provides several numeric types, each suited for specific scenarios.

Discrete Values

Discrete values are whole numbers without fractional components, ideal for counting items or tracking quantities where only integers are needed.

Data TypeDescriptionExample Use CaseRange
SMALLINTSuitable for small-range valuesStoring a person's age-32,768 to 32,767
INTEGERGeneral-purpose whole numbersCounting sales or items-2,147,483,648 to 2,147,483,647
BIGINTLarge-range valuesTracking credit card transactions-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
SERIALAuto-incrementing integer, used for IDsAuto-incrementing ID1 to 2,147,483,647
BIGSERIALAuto-incrementing big integer for large IDsLarge auto-incrementing ID1 to 9,223,372,036,854,775,807

Continuous Values

Continuous values include decimals, representing data that requires precision, such as financial or scientific data.

Data TypeDescriptionExample Use CasePrecision
DECIMAL(8, 2)Stores exact numeric data with defined precision and scaleStoring salary dataUp to 6 digits before and 2 digits after the decimal point
NUMERICSame as DECIMAL, used for exact numeric valuesFinancial calculationsSame as DECIMAL
REALSingle precision, suitable for approximate valuesApproximate values6 decimal digits
DOUBLE PRECISIONHigher precision, used for scientific calculationsScientific data15 decimal digits

Example: Numeric Data Types

Here’s an example of how you might define an employee table using both discrete and continuous values:

CREATE TABLE employees (
employee_id SERIAL PRIMARY KEY,
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
num_sales INTEGER,
salary DECIMAL(8, 2) -- 8 digits precision, 2 digits after the decimal point
);

We can also use DEFAULT values if no value is set for the column. Here's an example of an small businesses administration (SBA) loans:

CREATE TABLE campaign (
id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
budget NUMERIC(7, 2), -- The campaign's budget
num_days SMALLINT DEFAULT 30, -- The duration of campaign in days
goal_amount INTEGER DEFAULT 100, -- The number of new applications desired
num_applications INTEGER DEFAULT 0 -- The number of received applications
);

Bool and Temporal

Boolean

The BOOLEAN data type in PostgreSQL is ideal for binary decisions or flags within your database. It is used to represent:

  • TRUE or FALSE values
  • NULL, indicating an unknown value

BOOLEAN and BOOL are interchangeable in PostgreSQL. If no default value is specified for a BOOLEAN column, it defaults to false. However, you can explicitly set the default value based on your use case.

CREATE TABLE products (
product_id SERIAL PRIMARY KEY,
product_name VARCHAR(100),
in_stock BOOLEAN DEFAULT TRUE
);

Temporal

Temporal data types are used to store dates, times, or both, making them essential for tracking events or time-sensitive data.

Data TypeDescriptionFormatExample Use Case
TIMESTAMPStores both date and time2021-06-23 04:51:23Start date and time for a promotion
DATEStores only the date2021-06-23Employee birthdates
TIMEStores only the time04:51:23Scheduling daily automated reports

Temporal data types allow for precise control over date and time storage in your database. For example, the TIMESTAMP type can record the exact moment a marketing campaign begins, while the DATE type is sufficient for storing birthdates where the time of day is irrelevant.

Example:

CREATE TABLE promotions (
promotion_id SERIAL PRIMARY KEY,
promotion_name VARCHAR(100),
start_time TIMESTAMP
);

CREATE TABLE employees (
employee_id SERIAL PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
birthdate DATE
);

This setup allows you to track when promotions start and record employee birthdates accurately, using the appropriate temporal data type for each scenario.