PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
Home / PostgreSQL Tutorial / A Look At Various PostgreSQL Integer Data Types

A Look At Various PostgreSQL Integer Data Types

Summary: this tutorial introduces you to various PostgreSQL integer types including SMALLINT, INTEGER, and BIGINT.

Introduction to PostgreSQL integer types

postgresql integerTo store the whole numbers in PostgreSQL, you use one of the following integer types: SMALLINT, INTEGER, and BIGINT.

The following table illustrates the specification of each integer type:

NameStorage SizeMinMax
 SMALLINT2 bytes-32,768+32,767
 INTEGER4 bytes-2,147,483,648+2,147,483,647
 BIGINT8 bytes-9,223,372,036,854,775,808+9,223,372,036,854,775,807

If you try to store a value outside of the permitted range, PostgreSQL will issue an error.

Unlike MySQL integer, PostgreSQL does not provide unsigned integer types.

SMALLINT

The SMALLINT requires 2 bytes storage size which can store any integer numbers that is in the range of (-32,767, 32,767).

You can use the SMALLINT type for storing something like ages of people, the number of pages of a book, etc.

The following statement creates a table named books:

1
2
3
4
5
CREATE TABLE books (
    book_id SERIAL PRIMARY KEY,
    title VARCHAR (255) NOT NULL,
    pages SMALLINT NOT NULL CHECK (pages > 0)
);

In this example, the pages column is a SMALLINT column. Because the number of pages of a book must be positive, we added a CHECK constraint to enforce this rule.

INTEGER

The INTEGER is the most common choice between integer types because it offers the best balance between storage size, range, and performance.

The INTEGER type requires 4 bytes storage size that can store numbers in the range of (-2,147,483,648, 2,147,483,647).

You can use the INTEGER type for a column that stores quite big whole numbers like the population of a city or even country as the following example:

1
2
3
4
5
CREATE TABLE cities (
    city_id serial PRIMARY KEY,
    city_name VARCHAR (255) NOT NULL,
    population INT NOT NULL CHECK (population >= 0)
);

Notice that INT is the synonym of INTEGER.

BIGINT

In case you want to store the whole numbers that are out of the range of the INTEGER type, you can use the BIGINT type.

The BIGINT type requires 8 bytes storage size that can store any number in the range of (-9,223,372,036,854,775,808,+9,223,372,036,854,775,807).

Using BIGINT type is not only consuming a lot of storage but also decreasing the performance of the database, therefore, you should have a good reason to use it.

Previous Tutorial: An Overview Of PostgreSQL NUMERIC Type
Next Tutorial: Using PostgreSQL SERIAL To Create Auto-increment Column

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Fundamentals

  • PostgreSQL Select
  • PostgreSQL Order By
  • PostgreSQL Select Distinct
  • PostgreSQL Where
  • PostgreSQL LIMIT
  • PostgreSQL IN
  • PostgreSQL Between
  • PostgreSQL Like
  • PostgreSQL Inner Join
  • PostgreSQL Left Join
  • PostgreSQL Full Outer Join
  • PostgreSQL Cross Join
  • PostgreSQL Natural Join
  • PostgreSQL Group By
  • PostgreSQL Having
  • PostgreSQL Union
  • PostgreSQL Intersect
  • PostgreSQL Except
  • PostgreSQL Subquery
  • PostgreSQL Insert
  • PostgreSQL Update
  • PostgreSQL Delete
  • PostgreSQL Data Types
  • PostgreSQL Create Table
  • PostgreSQL Alter Table
  • PostgreSQL Drop Table
  • PostgreSQL Truncate Table
  • PostgreSQL CHECK Constraint
  • PostgreSQL Not-Null Constraint
  • PostgreSQL Foreign Key
  • PostgreSQL Primary Key
  • PostgreSQL UNIQUE Constraint

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL vs. MySQL
  • A Step-by-Step Guide To PostgreSQL Temporary Table
  • PostgreSQL RENAME COLUMN: Renaming One or More Columns of a Table
  • PostgreSQL Rename Table: A Step-by-Step Guide
  • PostgreSQL Change Column Type: Step-by-Step Examples
  • PostgreSQL DROP COLUMN: Remove One or More Columns of a Table
  • A Quick Guide To The PostgreSQL TIME Data Type
  • A Comprehensive Look at PostgreSQL Interval Data Type
  • PostgreSQL Rename Database: A Quick Guide

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.