Skip to main content
0 reactions

PostgreSQL Tutorial — Part 5: Data Types Explained

Featured card for PostgreSQL Tutorial Part 5, Data Types Explained, with a terminal window listing integer: 2147483647, numeric(10,2): 15.00, date: 2026-11-27 and boolean: t.
PostgreSQL Tutorial, Part 5: Data Types Explained.

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

Module B: Build the Database

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:

postgres=#SQL
CREATE DATABASE bookstore;
\c bookstore

Prerequisites

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 integer always 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:

TypeHoldsIn the bookstore
integerWhole numbers up to about 2.1 billionIDs, such as book_id
numeric(10,2)Exact decimal numbers, here with 2 digits after the pointPrices
textText of any lengthTitles, names and emails
dateA calendar dayThe day a book came out
timestamptzA moment in time, with its time zoneWhen a customer joined
booleanTrue or falseWhether 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:

TerminalBash
psql -U postgres

Then switch to bookstore:

postgres=#Plain text
\c bookstore
OutputPlain text
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:

bookstore=#SQL
SELECT '2026-11-27'::date;
OutputPlain text
    date
------------
 2026-11-27
(1 row)

The text '2026-11-27' became a date. Now try a date that doesn’t exist:

bookstore=#SQL
SELECT '2026-02-30'::date;
OutputPlain text
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:

bookstore=#SQL
SELECT 'twelve'::integer;
OutputPlain text
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:

bookstore=#SQL
SELECT 2147483647::integer;
OutputPlain text
    int4
------------
 2147483647
(1 row)

One more doesn’t:

bookstore=#SQL
SELECT 2147483648::integer;
OutputPlain text
ERROR:  integer out of range

bigint holds far bigger numbers, up to about 9.2 quintillion:

bookstore=#SQL
SELECT 2147483648::bigint;
OutputPlain text
    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:

bookstore=#SQL
SELECT 1234567.89::real;
OutputPlain text
    float4
---------------
 1.2345679e+06
(1 row)

The price came back as about 1.2345679 million, and the cents are gone. numeric keeps every digit:

bookstore=#SQL
SELECT 1234567.89::numeric(10,2);
OutputPlain text
  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:

bookstore=#SQL
SELECT 14.999::numeric(10,2);
OutputPlain text
 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:

bookstore=#SQL
SELECT '2026-11-27 10:30'::timestamptz;
OutputPlain text
        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:

bookstore=#SQL
SELECT '2026-11-27 10:30+00'::timestamp;
OutputPlain text
      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':

bookstore=#SQL
SELECT 'yes'::boolean, 'no'::boolean;
OutputPlain text
 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:

TableColumnTypeWhy
authorsauthor_idintegerA whole number that counts up by itself
authorsnametextNames have no fixed length
booksbook_idintegerThe same as author_id
bookstitletextTitles have no fixed length
booksauthor_idintegerIt holds an authors.author_id
bookspricenumeric(10,2)Exact, with cents
bookspublished_ondateA day, with no time
booksin_stockbooleanYes or no
customerscustomer_idintegerThe same as the other IDs
customersnametextNames have no fixed length
customersemailtextNo length rule to enforce
customersjoined_attimestamptzA 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

MistakeWhat happensDo this instead
real or double precision for pricesValues 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 databasesIt’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 happenedA time zone in the value is silently dropped, as Step 6 showed.Use timestamptz.
Dates or numbers stored as textNothing 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

ProblemLikely causeFix
ERROR: integer out of rangeThe 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

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