PostgreSQL Data Types
beginnerbasics
2 min read
What are Data Types?
Every column in a PostgreSQL table has a data type that determines what kind of values it can store. Choosing the right type ensures data integrity and optimal performance.
Common Data Types
Numeric
INTEGER/INT— Whole numbers (-2 billion to 2 billion)BIGINT— Large whole numbersSERIAL— Auto-incrementing integerNUMERIC(p, s)/DECIMAL— Exact decimal numbersREAL/DOUBLE PRECISION— Floating-point numbers
Text
VARCHAR(n)— Variable-length string up to n charactersCHAR(n)— Fixed-length stringTEXT— Unlimited-length string
Boolean
BOOLEAN— true, false, or NULL
Date and Time
DATE— Calendar dateTIME— Time of dayTIMESTAMP— Date and timeTIMESTAMPTZ— Timestamp with time zone
Other
JSONB— Binary JSON dataUUID— Universally unique identifierARRAY— Array of any type
Key Points
- Choose the Smallest Type — Use
INTEGERinstead ofBIGINTunless you need the range. - TEXT vs VARCHAR — In PostgreSQL,
TEXTandVARCHARwithout a length perform identically. - NUMERIC for Money — Use
NUMERICfor financial calculations; floating-point types can introduce rounding errors. - TIMESTAMPTZ — Always prefer
TIMESTAMPTZoverTIMESTAMPfor time-zone-aware applications. - JSONB Over JSON —
JSONBis more efficient thanJSONfor storage and querying.
Guided Practice
Solve the challenge below. Use hints when stuck and check your answer for instant feedback.
Practice challengeGuided learning mode
PostgreSQL Data Types Challenge
Write a query that solve this task: create a products table showcasing different types.
Expected result
Table created with integer, string, numeric, boolean, timestamp, and array columns.
Hidden checks
- Returned rows and values
- Output columns and result shape
- Final database state after the query runs
Lesson guidance
What are Data Types?
Initializing database...Each run starts from fresh sample data.
More Examples
Cast between types
Convert a string to an integer using CAST.
Initializing database...Each run starts from fresh sample data.
Frequently Asked Questions
Should I use TEXT or VARCHAR in PostgreSQL?
In PostgreSQL, TEXT and VARCHAR without a length limit perform identically. Use VARCHAR(n) only if you need to enforce a maximum length at the database level.
What is the difference between NUMERIC and FLOAT?
NUMERIC stores exact decimal values, ideal for money. FLOAT uses binary floating-point, which is faster but can introduce small rounding errors.