PostgreSQL - INTEGER Data Type
Last Updated :
18 Nov, 2024
In PostgreSQL, the INTEGER data type is widely used for storing numerical data efficiently. It is a 4-byte data type that allows us to store whole numbers within a specified range, making it ideal for various use cases like population counts, active user statistics, and more.
In this article, we will provide a comprehensive explanation of the INTEGER data type in PostgreSQL, including syntax, practical use cases, examples with outputs, and some important considerations when using this data type in our PostgreSQL database.
What is PostgreSQL INTEGER Data Type?
The INTEGER data type, also known as INT, is one of the most commonly used data types in PostgreSQL. It stores whole numbers (i.e., numbers without decimal points) and requires 4 bytes of storage. The range of values it can store is between -2,147,483,648 to 2,147,483,647.
This makes it suitable for various applications, such as:
- Storing the population of a country.
- Tracking user counts on websites or social media platforms.
- Representing counts in inventory management.
Syntax
variable_name INTEGER
Examples of PostgreSQL INTEGER Data Type
Now let's look into some examples of use cases of INTEGER Data Type, which can be applied in a variety of real-world scenarios where whole numbers are needed, such as tracking inventory, user counts, and more.
Example 1: Storing Country Population Data
In this example, we will create a table that stores the population of various countries by using the below commands. This approach is ideal for storing large numerical values like national populations efficiently.
Query:
CREATE TABLE countries_population(
country_id SERIAL PRIMARY KEY,
name VARCHAR (255) NOT NULL,
population INTEGER NOT NULL CHECK (population> 0)
);
INSERT INTO countries_population(name, population)
VALUES
('India', 1352600000),
('Russia', 14450000),
('Canada', 37600000),
('Japan', 126500000);
SELECT * FROM countries_population;
Output

Explanation:
In this case, the population column is of type INTEGER, ensuring that the stored data represents whole numbers only. We also use the CHECK constraint to enforce that the population value must always be greater than zero, ensuring valid data.
Example 2: Storing Active Users on Social Media Platforms
In this example, we will create a table that stores the number of active users on various social media apps by using the below commands. We also use the CHECK constraint to enforce that the population value must always be greater than zero, ensuring valid data.
Query:
CREATE TABLE social_media(
id SERIAL PRIMARY KEY,
name VARCHAR (255) NOT NULL,
active_users INTEGER NOT NULL CHECK (active_users> 0)
);
INSERT INTO social_media(name, active_users)
VALUES
('Facebook', 249279585),
('Twitter', 330000000),
('Instagram', 1070000000),
('Linkedin', 210000000);
SELECT * FROM social_media;
Output

Explanation:
In this case, the population column is of type INTEGER, ensuring that the stored data represents whole numbers only. The SERIAL data type for the country_id field automatically generates unique identifiers for each row, making it easier to reference and maintain the records.
Important Points About PostgreSQL INTEGER Data Type
- When no value is specified, the
INTEGER
type defaults to NULL
unless a NOT NULL
constraint is applied.
- PostgreSQL offers the
SERIAL
pseudo-type which is an auto-incrementing INTEGER
. This is handy for creating unique identifiers.
INTEGER
operations are generally faster than operations on larger numeric types (BIGINT
, NUMERIC
).
- While
INTEGER
can store up to 2,147,483,647, calculations or intermediate results exceeding this range can cause overflow errors. Consider using BIGINT
for operations where values may exceed this range.
Conclusion
The INTEGER data type in PostgreSQL is a highly flexible and efficient choice for storing whole numbers in our database. It is widely used in applications that require the representation of counts, population statistics, and more. However, if our application requires values beyond the INTEGER range, consider using BIGINT. By following best practices for defining and using INTEGER in PostgreSQL, we can ensure both data accuracy and performance in our database applications.
Similar Reads
SQL Tutorial Structured Query Language (SQL) is the standard language used to interact with relational databases. Whether you want to create, delete, update or read data, SQL provides the structure and commands to perform these operations. SQL is widely supported across various database systems like MySQL, Oracl
8 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
Window Functions in SQL SQL window functions are essential for advanced data analysis and database management. It is a type of function that allows us to perform calculations across a specific set of rows related to the current row. These calculations happen within a defined window of data and they are particularly useful
6 min read
Top 60 DBMS Interview Questions with Answers for 2025 A Database Management System (DBMS) is the backbone of modern data storage and management. Understanding DBMS concepts is critical for anyone looking to work with databases. Whether you're preparing for your first job in database management or advancing in your career, being well-prepared for a DBMS
15+ min read
SQL Exercises : SQL Practice with Solution for Beginners and Experienced SQL (Structured Query Language) is a powerful and flexible tool for managing and manipulating relational databases. Regardless of our experience level, practising SQL exercises is essential for improving our skills. Regular practice not only enhances our understanding of SQL concepts but also builds
15+ min read
SQL Cheat Sheet ( Basic to Advanced) Creating and managing databases in SQL involves various commands and concepts that handle the structuring, querying, and manipulation of data. In this guide, we will see a comprehensive cheat sheet for essential SQL operations, offering a practical reference for tasks ranging from database creation
15 min read
SQL Views Views in SQL are a type of virtual table that simplifies how users interact with data across one or more tables. Unlike traditional tables, a view in SQL does not store data on disk; instead, it dynamically retrieves data based on a pre-defined query each time itâs accessed. SQL views are particular
7 min read
MySQL Tutorial This MySQL Tutorial is made for both beginners and experienced professionals. Whether you're starting with MYSQL basics or diving into advanced concepts, this free tutorial is the ideal guide to help you learn and understand MYSQL, no matter your skill level. From setting up your database to perform
11 min read
Indexing in Databases - Set 1 Indexing is a crucial technique used in databases to optimize data retrieval operations. It improves query performance by minimizing disk I/O operations, thus reducing the time it takes to locate and access data. Essentially, indexing allows the database management system (DBMS) to locate data more
8 min read