Every column in a PostgreSQL table holds one kind of value: whole numbers, prices, text, dates or a yes or no. That kind is the column’s data type, and PostgreSQL checks it every time you save a value.
This part gets the PostgreSQL data types you’ll use most explained in plain words. You try each one in psql, see how PostgreSQL refuses a value that doesn’t fit, and choose a type for every column of the bookstore database, ready for Part 6.
This is Part 5 of the PostgreSQL Tutorial. The parts in Module B, Build the Database:
- Part 4: Create a Database
- Part 5: Data Types Explained (you are here)
- Part 6: Create Tables
- Part 7: Primary Keys, Foreign Keys and Constraints
- Part 8: Insert, Update and Delete Data
- Part 9: Import and Export CSV Files
All parts of the PostgreSQL Tutorial
Module A: Start Here
- Part 1: What Is PostgreSQL? Databases, Tables and SQL Explained
- Part 2: Connect with psql and Run Your First Commands
- Part 3: pgAdmin 4 for Beginners
Module B: Build the Database
- Part 4: Create a Database
- Part 5: Data Types Explained
- Part 6: Create Tables
- Part 7: Primary Keys, Foreign Keys and Constraints
- Part 8: Insert, Update and Delete Data
- Part 9: Import and Export CSV Files
Module C: Query Data
- Part 10: SELECT, WHERE, ORDER BY and LIMIT
- Part 11: LIKE, IN, BETWEEN, NULL and CASE
- Part 12: GROUP BY and Aggregate Functions
- Part 13: JOINs Explained
- Part 14: Subqueries and CTEs
- Part 15: UNION, INTERSECT and EXCEPT
- Part 16: String, Number and Conversion Functions
- Part 17: Dates, Times and Time Zones
Module D: Design and Performance
- Part 18: Database Design and Normalization
- Part 19: Views and Materialized Views
- Part 20: Indexes Explained
- Part 21: EXPLAIN and Query Performance
- Part 22: Transactions, Isolation Levels and Locks
Module E: Advanced SQL and Programming
- Part 23: Window Functions
- Part 24: Recursive Queries and LATERAL Joins
- Part 25: JSON and JSONB
- Part 26: Arrays, Enums, Ranges and Generated Columns
- Part 27: Full-Text Search
- Part 28: Functions and Stored Procedures
- Part 29: Triggers
Module F: Administration and Security
- Part 30: Users, Roles and Privileges
- Part 31: Row-Level Security
- Part 32: Configuration and Restarting the Server
- Part 33: Allow Remote Connections Securely
- Part 34: Backup and Restore
- Part 35: Automate Backups
- Part 36: VACUUM, ANALYZE and Autovacuum
- Part 37: Monitoring and Slow Queries
- Part 38: Table Partitioning
- Part 39: Streaming Replication
- Part 40: Upgrade to a New Major Version
Module G: PostgreSQL for Developers
- Part 41: Connect from C# .NET with Npgsql
- Part 42: Entity Framework Core with PostgreSQL
- Part 43: Connect from Python with psycopg
- Part 44: Connect from Node.js with node-postgres
Module H: Extensions, AI and Cloud
- Part 45: Extensions
- Part 46: Vector Search with pgvector
- Part 47: Azure Database for PostgreSQL
- Part 48: Migrate to Azure and What to Learn Next
Every command and output on this page is real, from PostgreSQL 19 on macOS.
Where the bookstore database is now: Part 4 created it, and it’s still empty. This part only tries values, so it adds nothing. If you’re starting here, create the database with this catch-up script, in psql connected to the postgres database:
CREATE DATABASE bookstore;
\c bookstore
Prerequisites
- The previous part, Part 4: Create a Database, so you have the
bookstoredatabase.
What Is a Data Type?
A data type tells PostgreSQL what kind of value a column holds. It decides three things:
- Which values are allowed. A date column refuses
2026-02-30, because February has no 30th. - How values compare and sort. Numbers sort as numbers and dates in calendar order.
- How much space a value takes. An
integeralways takes 4 bytes.
You choose a type for every column when you create a table, in Part 6. These are the types this tutorial uses most:
| Type | Holds | In the bookstore |
|---|---|---|
integer | Whole numbers up to about 2.1 billion | IDs, such as book_id |
numeric(10,2) | Exact decimal numbers, here with 2 digits after the point | Prices |
text | Text of any length | Titles, names and emails |
date | A calendar day | The day a book came out |
timestamptz | A moment in time, with its time zone | When a customer joined |
boolean | True or false | Whether a book is in stock |
PostgreSQL has many more, such as uuid for IDs and jsonb for JSON documents, and later parts use some of them.
Step 1: Connect to the bookstore Database
Open Terminal and connect as the postgres user, as Part 2 shows:
psql -U postgres
Then switch to bookstore:
\c bookstore
You are now connected to database "bookstore" as user "postgres".
Step 2: Try a Value with a Cast
You don’t need a table to see how a type treats a value. Write the value, two colons and the name of a type, and PostgreSQL converts the value to that type. This is called a cast:
SELECT '2026-11-27'::date;
date
------------
2026-11-27
(1 row)
The text '2026-11-27' became a date. Now try a date that doesn’t exist:
SELECT '2026-02-30'::date;
ERROR: date/time field value out of range: "2026-02-30"
LINE 1: SELECT '2026-02-30'::date;
^
PostgreSQL refuses it, and a date column refuses it in the same way, so a wrong date never gets into your table. A number type refuses text that isn’t a number:
SELECT 'twelve'::integer;
ERROR: invalid input syntax for type integer: "twelve"
LINE 1: SELECT 'twelve'::integer;
^
Advertisement
Step 3: Whole Numbers: integer and bigint
integer holds whole numbers from about −2.1 billion to +2.1 billion. The largest one fits:
SELECT 2147483647::integer;
int4
------------
2147483647
(1 row)
One more doesn’t:
SELECT 2147483648::integer;
ERROR: integer out of range
bigint holds far bigger numbers, up to about 9.2 quintillion:
SELECT 2147483648::bigint;
int8
------------
2147483648
(1 row)
psql names each result column after the type’s short internal name: int4 is integer and int8 is bigint. integer is right for most IDs and counts; choose bigint for a table that could pass 2 billion rows. smallint vs integer vs bigint in PostgreSQL compares all three whole-number types.
An ID column must also count up by itself as rows arrive. Part 6 does that with an identity column, and Identity vs Serial in PostgreSQL: Which to Use for IDs explains why it’s better than the older serial.
Step 4: Prices: numeric
Prices need exact decimals. PostgreSQL’s real and double precision types store close approximations instead, which is fine for measurements but wrong for money. real keeps only about 6 digits:
SELECT 1234567.89::real;
float4
---------------
1.2345679e+06
(1 row)
The price came back as about 1.2345679 million, and the cents are gone. numeric keeps every digit:
SELECT 1234567.89::numeric(10,2);
numeric
------------
1234567.89
(1 row)
numeric(10,2) means up to 10 digits in all, 2 of them after the decimal point. A value with more decimals is rounded to 2:
SELECT 14.999::numeric(10,2);
numeric
---------
15.00
(1 row)
How to Store Money in PostgreSQL: numeric vs money explains why numeric is also a better choice for prices than PostgreSQL’s money type.
Step 5: Text: text and varchar
For text, use text: it holds any length, from an empty string to a whole book. varchar(n) is the same with a limit of n characters, so use it only when a real rule limits the length. The PostgreSQL documentation says there’s no speed difference between them. text vs varchar in PostgreSQL shows what happens when a value is too long.
Step 6: Dates and Times: date and timestamptz
date holds a calendar day, as Step 2 showed. For a moment in time, such as when an order was placed, use timestamptz, short for timestamp with time zone:
SELECT '2026-11-27 10:30'::timestamptz;
timestamptz
---------------------------
2026-11-27 10:30:00+05:30
(1 row)
The value shows its offset from UTC, here +05:30, because this server’s time zone is India’s. PostgreSQL stores the moment in UTC and shows it in each session’s own time zone, so people in different countries see the same moment in their local time.
The other type, timestamp, has no time zone, and it silently drops one you give it:
SELECT '2026-11-27 10:30+00'::timestamp;
timestamp
---------------------
2026-11-27 10:30:00
(1 row)
The +00 is gone, and nothing says which time zone 10:30 was in. timestamp vs timestamptz in PostgreSQL shows the difference with a table.
Step 7: Yes or No: boolean
boolean holds true or false. PostgreSQL accepts true and false, and also words such as 'yes' and 'no':
SELECT 'yes'::boolean, 'no'::boolean;
bool | bool
------+------
t | f
(1 row)
psql shows true as t and false as f. A boolean column can also be empty, which SQL calls NULL: the answer isn’t known. Boolean and NULL in PostgreSQL: true, false and unknown explains how NULL behaves when you search.
Step 8: Choose the Types for the Bookstore
Part 6 creates the bookstore’s first three tables. Here are their columns and the type each one gets:
| Table | Column | Type | Why |
|---|---|---|---|
| authors | author_id | integer | A whole number that counts up by itself |
| authors | name | text | Names have no fixed length |
| books | book_id | integer | The same as author_id |
| books | title | text | Titles have no fixed length |
| books | author_id | integer | It holds an authors.author_id |
| books | price | numeric(10,2) | Exact, with cents |
| books | published_on | date | A day, with no time |
| books | in_stock | boolean | Yes or no |
| customers | customer_id | integer | The same as the other IDs |
| customers | name | text | Names have no fixed length |
| customers | email | text | No length rule to enforce |
| customers | joined_at | timestamptz | A moment, right in every time zone |
Once the tables exist, How to Check a Column’s Data Type in PostgreSQL shows how to see each column’s type. Another common choice for IDs is a UUID, a long ID that’s unique everywhere, as How to Generate a UUID in PostgreSQL shows.
Clean Up
This part only tried values, so there’s nothing to remove. Keep the bookstore database: Part 6 creates its first tables.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
real or double precision for prices | Values are stored as approximations: 1234567.89::real comes back as 1.2345679e+06. | Use numeric(10,2), or another precision that fits your prices. |
varchar(255) for every text column, a habit from other databases | It’s no faster than text in PostgreSQL, and a longer value is refused. | Use text, unless a real rule limits the length. |
timestamp for the moment something happened | A time zone in the value is silently dropped, as Step 6 showed. | Use timestamptz. |
Dates or numbers stored as text | Nothing stops a wrong value such as 2026-02-30, and they sort as text. | Use date or a number type, so PostgreSQL refuses wrong values. |
Troubleshooting
| Problem | Likely cause | Fix |
|---|---|---|
ERROR: integer out of range | The number is bigger than 2,147,483,647, the largest integer. | Use bigint for that column. |
ERROR: invalid input syntax for type integer: "twelve" | The value isn’t a number. | Write the number in digits: 12. |
ERROR: date/time field value out of range: "2026-02-30" | That date doesn’t exist, or its parts are in the wrong order. | Write dates year first, as YYYY-MM-DD. |
FAQ
What does :: mean?
It converts a value to another type: a cast. CAST('2026-11-27' AS date) does the same in standard SQL; :: is PostgreSQL’s shorter form, as the documentation (opens in a new tab) explains.
Are int and integer the same?
Yes. int and int4 are other names for integer, int8 is bigint and bool is boolean. The documentation’s list of data types (opens in a new tab) gives every type’s other names.
Can I change a column’s type later?
Yes, with ALTER TABLE. PostgreSQL converts the values already in the column, and refuses the change if one of them doesn’t fit the new type. Choosing well now saves that work.
Conclusion
A data type decides which values a column accepts. For the bookstore that means integer for IDs, numeric(10,2) for prices, text for names and titles, date for days, timestamptz for moments and boolean for yes or no. You tried each one with a cast and saw PostgreSQL refuse values that don’t fit.
Next, Part 6 uses these types to create the bookstore’s first tables.
← Previous: Part 4: Create a Database
Coming next: Part 6: Create Tables.
Additional Resources
- PostgreSQL 19 documentation: Data Types (opens in a new tab)
- PostgreSQL 19 documentation: Numeric Types (opens in a new tab)
- PostgreSQL 19 documentation: Character Types (opens in a new tab)
- PostgreSQL 19 documentation: Date/Time Types (opens in a new tab)
- PostgreSQL 19 documentation: Boolean Type (opens in a new tab)
- PostgreSQL 19 documentation: Type Casts (opens in a new tab)
- PostgreSQL Tutorial for Beginners: every part and setup guide
Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.
0 comments