Ira SQL ProIra SQL Pro

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 numbers
  • SERIAL — Auto-incrementing integer
  • NUMERIC(p, s) / DECIMAL — Exact decimal numbers
  • REAL / DOUBLE PRECISION — Floating-point numbers

Text

  • VARCHAR(n) — Variable-length string up to n characters
  • CHAR(n) — Fixed-length string
  • TEXT — Unlimited-length string

Boolean

  • BOOLEAN — true, false, or NULL

Date and Time

  • DATE — Calendar date
  • TIME — Time of day
  • TIMESTAMP — Date and time
  • TIMESTAMPTZ — Timestamp with time zone

Other

  • JSONB — Binary JSON data
  • UUID — Universally unique identifier
  • ARRAY — Array of any type

Key Points

  1. Choose the Smallest Type — Use INTEGER instead of BIGINT unless you need the range.
  2. TEXT vs VARCHAR — In PostgreSQL, TEXT and VARCHAR without a length perform identically.
  3. NUMERIC for Money — Use NUMERIC for financial calculations; floating-point types can introduce rounding errors.
  4. TIMESTAMPTZ — Always prefer TIMESTAMPTZ over TIMESTAMP for time-zone-aware applications.
  5. JSONB Over JSONJSONB is more efficient than JSON for 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.

Related Topics