Skip to main content
0 reactions

smallint vs integer vs bigint in PostgreSQL

Featured card for smallint vs integer vs bigint in PostgreSQL, with a terminal window listing smallint: 32767, integer: 2147483647, bigint: up to 9.2e18 and integer out of range.
smallint vs integer vs bigint in PostgreSQL.

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

SyntaxSQL
column_name smallint
column_name integer
column_name bigint
TypeSizeRange
smallint2 bytes−32,768 to +32,767
integer4 bytes−2,147,483,648 to +2,147,483,647
bigint8 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:

postgres=#SQL
SELECT 32767::smallint;
OutputPlain text
 int2
-------
 32767
(1 row)

One more doesn’t:

postgres=#SQL
SELECT 32768::smallint;
OutputPlain text
ERROR:  smallint out of range

integer stops after 2,147,483,647:

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

bigint takes numbers up to over 9 quintillion:

postgres=#SQL
SELECT 9223372036854775807::bigint;
OutputPlain text
        int8
---------------------
 9223372036854775807
(1 row)

Step 3: See a Column Refuse a Big Number

Create a database for the example and connect to it:

postgres=#SQL
CREATE DATABASE counters;
OutputPlain text
CREATE DATABASE
postgres=#Plain text
\c counters
OutputPlain text
You are now connected to database "counters" as user "postgres".
counters=#SQL
CREATE TABLE stats (page text, visits integer);
OutputPlain text
CREATE TABLE
counters=#SQL
INSERT INTO stats (page, visits) VALUES ('home', 3000000000);
OutputPlain text
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

  • integer for most whole numbers: IDs, counts and quantities.
  • bigint when a value could pass about 2.1 billion, such as the IDs of a very large table or a busy site’s visit count.
  • smallint only 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:

counters=#Plain text
\c postgres
OutputPlain text
You are now connected to database "postgres" as user "postgres".
postgres=#SQL
DROP DATABASE counters;
OutputPlain text
DROP DATABASE

Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.

Common Mistakes

MistakeWhat happensDo this instead
smallint to save spaceA 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 growingPast 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

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