EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL Integer
 

PostgreSQL Integer

Sohel Sayyad
Article bySohel Sayyad
EDUCBA
Reviewed byRavi Rathore

Updated May 19, 2023

PostgreSQL Integer

 

 

Introduction to PostgreSQL Integer

The integer data types may require different storage sizes and might store negative values. PostgreSQL Integer does not allow us to store unsigned integer data types. In order to store the whole number, we have to use the integer data types such as SMALLINT data type, INTEGER data type, and BIGINT data type, etc. Each of the integer data types is having a specified range to store the integer data values, we cannot store the data values which are outside the allowed range of the particular data type. PostgreSQL will throw an error or exception if we try to store values outside the defined range of the data type.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Types of PostgreSQL INTEGER

Consider the following table, which illustrates the types of the integer:

Name Storage Size Min Max
SMALLINT 2 bytes -32, 768 +32, 767
INTEGER 4 bytes -2, 14, 74, 83, 648 +2, 14, 74, 83, 647
BIGINT 8 bytes -92, 23, 37, 20, 36, 85, 47, 70, 000 +92, 23, 37, 20, 36, 85, 47, 70, 000

Examples

Let us consider all types one by one with some examples:

1. SMALLINT

The storage size required for the PostgreSQL SMALLINT data type is 2 bytes. PostgreSQL allows the SMALLINT data type to store values that are within the range of ( -32,767, 32,767 ). The PostgreSQL SMALLINT data type can store 16-bit integer data.

Consider the following example where we can use the PostgreSQL SMALLINT integer data type for storing values such as the count of students and the count of the teacher in a department. Consider the following table named ‘department’, which will store the number of students, and the number of the teacher in a department.

Code:

CREATE TABLE department
(
department_id SERIAL PRIMARY KEY,
department_name VARCHAR ( 255 ) NOT NULL,
students_count SMALLINT NOT NULL CHECK (students_count > 0),
teachers_count SMALLINT NOT NULL CHECK (teachers_count > 0)
);

Explanation: In the above example, we have added students_count and teachers_count columns with data type as the PostgreSQL SMALLINT. We must need at least one student and one teacher to form any department. So we have defined the CHECK constraint on the students_count and teachers_count columns.

2. INTEGER

The storage size required for the PostgreSQL INTEGER data type is 4 bytes. PostgreSQL allows the INTEGER data type to store values that are within the range of ( -2,147,483,648, 2,147,483,647 ) or ( -2^31 to 2^31 -1 (2 Gb) ). The PostgreSQL INTEGER data type is often used as it gives the best performance, range, and storage size. The PostgreSQL INTEGER data type can be used as INT, INTEGER, and INT4. The INTEGER data type can store 32-bit integer data.

Consider the following example where we can use the INTEGER data type for storing values which are big numbers such as the count of students in the university or the count of the teacher in the university.

Consider the table named ‘university’, which will store the number of students and the number of the teacher in a university using the CREATE TABLE statement.

Code:

CREATE TABLE university
(
university_id SERIAL PRIMARY KEY,
university_name VARCHAR ( 255 ) NOT NULL,
students_count INT NOT NULL CHECK (students_count > 0),
teachers_count INTEGER NOT NULL CHECK (teachers_count > 0)
);

Explanation: In the above example, we have added students_count and teachers_count columns with data type as INT.

3. BIGINT

The storage size required for the BIGINT data type is 8 bytes. PostgreSQL allows the BIGINT data type to store values which are within the range of ( -9,223, 372, 036, 854, 775, 808, + 9,223, 372, 036, 854, 775, 807 ) or ( -2^63 to 2^63 -1 ) which means the BIGINT type can store any number. The BIGINT data type is used to store the very big numbers which are out of the storage scope of the PostgreSQL INTEGER data type, such as the unpredictable value storage like memory required by any server. The PostgreSQL BIGINT data type might affect the system’s performance, so we should use BIGINT type if there is a valid reason to use it. The PostgreSQL BIGINT data type can store 64-bit integer data.

Conclusion

We hope from the above article you have understood the PostgreSQL INTEGER data type and the types of the PostgreSQL INTEGER data type such as SMALLINT, INTEGER, and BIGINT, etc. Also, we have added several examples of PostgreSQL INTEGER data types to understand it in detail.

Recommended Articles

We hope that this EDUCBA information on “PostgreSQL Integer” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. PostgreSQL Variables
  2. PostgreSQL Commands
  3. Procedures in PostgreSQL
  4. Ansible Variables

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Limited Time Offer! - ENROLL NOW