PostgreSQL has three types for whole numbers, and they differ only in size: smallint takes 2 bytes, integer 4 and bigint 8. A bigger type holds bigger numbers. Here’s smallint vs integer vs bigint in PostgreSQL, with each type’s limit tried live.
Every command and output on this page is real, from PostgreSQL 19 on macOS.
Syntax
column_name smallint
column_name integer
column_name bigint
| Type | Size | Range |
|---|---|---|
smallint | 2 bytes | −32,768 to +32,767 |
integer | 4 bytes | −2,147,483,648 to +2,147,483,647 |
bigint | 8 bytes | −9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 |
The ranges come from Integer Types (opens in a new tab) in the PostgreSQL documentation. int2, int4 and int8 are other names for the three.
The Example
A website counts page visits. First, try each type’s limit; then watch a column refuse a number that’s too big.
Step 1: Connect
Connect to your server with psql as the postgres user, as Part 2 of the PostgreSQL Tutorial shows. psql connects you to the postgres database, and its prompt is postgres=#.
Step 2: Try Each Type’s Limit
The largest smallint fits:
SELECT 32767::smallint;
int2
-------
32767
(1 row)
One more doesn’t:
SELECT 32768::smallint;
ERROR: smallint out of range
integer stops after 2,147,483,647:
SELECT 2147483648::integer;
ERROR: integer out of range
bigint takes numbers up to over 9 quintillion:
SELECT 9223372036854775807::bigint;
int8
---------------------
9223372036854775807
(1 row)
Step 3: See a Column Refuse a Big Number
Create a database for the example and connect to it:
CREATE DATABASE counters;
CREATE DATABASE
\c counters
You are now connected to database "counters" as user "postgres".
CREATE TABLE stats (page text, visits integer);
CREATE TABLE
INSERT INTO stats (page, visits) VALUES ('home', 3000000000);
ERROR: integer out of range
Three billion visits don’t fit in an integer column. For a count that can grow that large, make the column bigint.
Which One to Use
integerfor most whole numbers: IDs, counts and quantities.bigintwhen a value could pass about 2.1 billion, such as the IDs of a very large table or a busy site’s visit count.smallintonly when values stay small for sure; it saves 2 bytes a row, but 32,767 comes quickly.
Clean Up
To remove the example, connect to another database and drop the test database:
\c postgres
You are now connected to database "postgres" as user "postgres".
DROP DATABASE counters;
DROP DATABASE
Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
smallint to save space | A value over 32,767 is refused: ERROR: smallint out of range. | Use integer unless the values are small for certain. |
integer for a counter that keeps growing | Past 2,147,483,647 it fails with ERROR: integer out of range. | Use bigint. |
FAQ
What about numbers with decimals?
These three types hold whole numbers only. For decimals, such as prices, use numeric, as How to Store Money in PostgreSQL: numeric vs money shows.
Is int the same as integer?
Yes. int and int4 are other names for integer.
Conclusion
smallint, integer and bigint hold whole numbers of 2, 4 and 8 bytes. Use integer by default, and bigint for anything that could pass 2.1 billion.
Additional Resources
Related
- PostgreSQL Tutorial — Part 5: Data Types Explained
- How to Store Money in PostgreSQL: numeric vs money
- Identity vs Serial in PostgreSQL: Which to Use for IDs
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