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
DATEandTIMESTAMP.
-
Understand the
SERIALauto-incrementing type.
-
Understand advanced Postgres types like
UUIDandJSONB.
3. Numeric Data Types
Used for math, counting, and ID numbers.-
INTEGER(orINT): Stores standard whole numbers from -2 billion to +2 billion.
-
SMALLINT/BIGINT: UseSMALLINTfor tiny numbers to save hard drive space. UseBIGINTfor massive numbers (like global view counts).
-
SERIAL: This is a special Postgres type. It creates anINTEGERcolumn, 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)orNUMERIC(M, D): Stores exact decimal values.Mis total digits,Dis 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. Thenspecifies the maximum characters allowed.VARCHAR(50)blocks strings longer than 50 characters.
-
CHAR(n): Fixed-length string. If you specifyCHAR(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,TEXTis highly optimized. Many developers useTEXTfor everything (even emails) unless they strictly *need* to enforce a length limit withVARCHAR.
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: StoresTRUE,FALSE, orNULL.
-
*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 ofSERIALintegers for Primary Keys so hackers cannot guess how many users the app has (e.g.,user/5vsuser/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
9. Common Mistakes
-
Using
FLOATorREALfor 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 useNUMERICfor currency.
-
Wasting Space with BIGINT: Using a
BIGINT(8 bytes) to store a person's age is a waste of RAM when aSMALLINT(2 bytes) works perfectly.
10. Best Practices
-
Prefer TEXT over VARCHAR: In older databases,
VARCHARwas faster. In modern PostgreSQL, there is absolutely zero performance difference betweenVARCHAR(255)andTEXT. Unless you have a strict business rule to enforce a character limit, just useTEXT.
11. Exercises
- 1. What data type should you use to store an exact financial transaction of $1,500.50?
-
2.
Write the SQL syntax to create a column named
is_verifiedthat holds a True or False value.
12. SQL Challenges
Write the DDL command to create a table namedlogs with an auto-incrementing ID and a timestamp column that automatically records the UTC time zone.
sql
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
SERIALandUUIDfor a Primary Key. What are the security benefits of using a UUID in a REST API?
-
Q: Why is the
NUMERIC(orDECIMAL) data type strictly required for financial data, rather than usingFLOATorDOUBLE PRECISION?
15. FAQs
Q: IsVARCHAR 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.