Skip to main content
PostgreSQL
CHAPTER 05 Intermediate

PostgreSQL Data Types Explained

Updated: May 16, 2026
6 min read

# CHAPTER 5

PostgreSQL Data Types Explained

1. Introduction

If you try to pour a gallon of water into a shot glass, it spills. If you build a massive swimming pool just to hold a shot of water, it is an incredible waste of space. Database columns work exactly the same way. When creating a table, you must tell PostgreSQL exactly what kind of data (and how much of it) a column is allowed to hold. In this chapter, we will master Data Types, the security guards that guarantee data integrity and optimize server storage.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Differentiate between Numeric types (INTEGER, DECIMAL).
  • Differentiate between String types (VARCHAR, TEXT).
  • Store temporal data using DATE and TIMESTAMP.
  • Understand the SERIAL auto-incrementing type.
  • Understand advanced Postgres types like UUID and JSONB.

3. Numeric Data Types

Used for math, counting, and ID numbers.
  • INTEGER (or INT): Stores standard whole numbers from -2 billion to +2 billion.
  • SMALLINT / BIGINT: Use SMALLINT for tiny numbers to save hard drive space. Use BIGINT for massive numbers (like global view counts).
  • SERIAL: This is a special Postgres type. It creates an INTEGER column, but automatically sets up a sequence generator behind the scenes so the number auto-increments (1, 2, 3...) on every new row. Perfect for Primary Keys!
  • DECIMAL(M, D) or NUMERIC(M, D): Stores exact decimal values. M is total digits, D is digits after the dot. NUMERIC(10, 2) means 10 total digits, 2 after the dot (e.g., 12345678.99). Always use this for Financial Data/Money!

4. String (Text) Data Types

Used for names, emails, and paragraphs.
  • VARCHAR(n): Variable-length string. The n specifies the maximum characters allowed. VARCHAR(50) blocks strings longer than 50 characters.
  • CHAR(n): Fixed-length string. If you specify CHAR(2) and insert "A", it pads the rest with a blank space. Good for 2-letter Country Codes (US, UK).
  • TEXT: Variable-length string with unlimited length. In PostgreSQL, TEXT is highly optimized. Many developers use TEXT for everything (even emails) unless they strictly *need* to enforce a length limit with VARCHAR.

5. Date and Time Data Types

Used for tracking when events happen.
  • DATE: Stores just the date (YYYY-MM-DD). Good for birthdays.
  • TIME: Stores just the time (HH:MM:SS).
  • TIMESTAMP: Combines Date and Time (YYYY-MM-DD HH:MM:SS). Good for "Account Created At" fields.
  • TIMESTAMPTZ: Timestamp *with Time Zone*. Always use this in production! It automatically converts the time to UTC before saving, preventing massive bugs when users log in from different countries.

6. Boolean Data Type

Used for True/False logic.
  • BOOLEAN: Stores TRUE, FALSE, or NULL.
  • *Best for:* isactive, isadmin, has_paid.

7. Advanced PostgreSQL Types (The Secret Weapons)

This is where Postgres destroys MySQL.
  • UUID: A Universally Unique Identifier (e.g., 550e8400-e29b-41d4-a716-446655440000). It is a mathematically random 128-bit string. Modern apps use UUIDs instead of SERIAL integers for Primary Keys so hackers cannot guess how many users the app has (e.g., user/5 vs user/550e...).
  • JSONB: The "JSON Binary" type. It allows you to store entire JSON documents (like a NoSQL MongoDB database!) directly inside a relational PostgreSQL column, and you can even search/index the keys inside the JSON!

8. Mini Project: A Modern User Table

Let's build a highly optimized table utilizing everything we just learned.
sql
1234567891011
CREATE TABLE employees (
    -- Modern apps often use UUIDs, but we will use SERIAL for simplicity here
    emp_id SERIAL PRIMARY KEY,                   
    first_name VARCHAR(50),                      
    email TEXT,                                  -- Unlimited length
    salary NUMERIC(10, 2),                       -- Exact money math!
    hire_date DATE,                              
    is_manager BOOLEAN,                          
    settings JSONB,                              -- Store unstructured app settings!
    created_at TIMESTAMPTZ DEFAULT NOW()         -- Auto-saves the exact UTC time
);

9. Common Mistakes

  • Using FLOAT or REAL for Money: Never do this! Floating-point numbers are mathematical approximations. If you do heavy math on them, they generate microscopic rounding errors (e.g., $10.00 becomes $9.9999999). Always use NUMERIC for currency.
  • Wasting Space with BIGINT: Using a BIGINT (8 bytes) to store a person's age is a waste of RAM when a SMALLINT (2 bytes) works perfectly.

10. Best Practices

  • Prefer TEXT over VARCHAR: In older databases, VARCHAR was faster. In modern PostgreSQL, there is absolutely zero performance difference between VARCHAR(255) and TEXT. Unless you have a strict business rule to enforce a character limit, just use TEXT.

11. Exercises

  1. 1. What data type should you use to store an exact financial transaction of $1,500.50?
  1. 2. Write the SQL syntax to create a column named is_verified that holds a True or False value.

12. SQL Challenges

Write the DDL command to create a table named logs with an auto-incrementing ID and a timestamp column that automatically records the UTC time zone.
sql
1234
CREATE TABLE logs (
    id SERIAL PRIMARY KEY,
    created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
);

13. MCQ Quiz with Answers

Question 1

Why is the TIMESTAMPTZ data type strongly recommended over the standard TIMESTAMP data type?

Question 2

What makes the JSONB data type one of PostgreSQL's most powerful features?

14. Interview Questions

  • Q: Explain the difference between SERIAL and UUID for a Primary Key. What are the security benefits of using a UUID in a REST API?
  • Q: Why is the NUMERIC (or DECIMAL) data type strictly required for financial data, rather than using FLOAT or DOUBLE PRECISION?

15. FAQs

Q: Is VARCHAR faster than TEXT in Postgres? A: No! The official PostgreSQL documentation explicitly states there is no performance difference between VARCHAR(n) and TEXT. Under the hood, they use the exact same internal structure.

16. Summary

Data Types are the gatekeepers of your database. By strictly defining whether a column accepts precise financials (NUMERIC), normalized temporal data (TIMESTAMPTZ), or flexible NoSQL documents (JSONB), you guarantee that your application's data remains clean, mathematically viable, and perfectly optimized for long-term storage.

17. Next Chapter Recommendation

Our tables are built and strictly enforced. It is finally time to put data into them! In Chapter 6: INSERT, UPDATE, and DELETE Queries, we will learn how to add new rows, modify existing data, and safely erase records using Data Manipulation Language (DML).

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·