What is the Equivalent of Java Long in PostgreSQL

Java users commonly use different databases to efficiently store and manipulate data. PostgreSQL is a popular choice among developers due to its notable features like security, scalability, and ease of use. However, when working with large numeric datasets, storing and managing enormous data can be challenging. For instance, Postgres offers BIGINT data type to store gigantic numeric values while Java keeps them in Long data type.

To address this problem, the "BIGINT" data type is used in PostgreSQL, which serves as an alternative to Java’s Long data type.

This post will present a comprehensive guide on Postgres’ equivalent of Java’s long data type.

What is Java’s Long Data Type?

Before understanding how BIGINT works, first, you must understand Java's LONG data type. In Java, the long data type is commonly used to handle extremely large whole numbers. It allows us to store numbers as small as "-9,223,372,036,854,775,808" and as large as "9,223,372,036,854,775,807". To declare a variable of type long, simply use the keyword "long" followed by the desired variable name:

long num = 9223372036854775L;

"L" must be appended at the end of the number.

What is the Equivalent of Java Long in Postgres?

In Postgres, the "BIGINT" data type corresponds to Java’s long data type and is used to store extremely large positive or negative numbers. The range of BIGINT spans from “-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807”.

Example: How BIGINT Works in PostgreSQL?

To illustrate the usage of the "BIGINT" data type in Postgres, we can create a table using the "CREATE TABLE" command and define a column with the "BIGINT" data type, as shown below:

CREATE TABLE cp_table (
 cp_id INT PRIMARY KEY,
 cp_name TEXT NOT NULL,
 cp_number BIGINT
);
img

The “cp_table” has been created successfully. Let’s insert a really big value into the “cp_table” using the INSERT query:

INSERT INTO cp_table (cp_id, cp_name, cp_number) 
VALUES (1, 'Dean', 922337203685476800),
(2, 'Joe', 922337203685477800),
(3, 'Ambrose', 922337203685476802),
(4, 'Joseph', 92233720368776804),
(5, 'Mike', 922337203685768001);
img

Let’s fetch the table’s data using the below-provided query:

SELECT * FROM cp_table;
img

This is how you can store and manipulate really big numeric values in Postgres.

Conclusion

In PostgreSQL, the "BIGINT" data type is used to store extremely large positive or negative numbers and can serve as an alternative to Java’s Long data type. It allows us to store numbers as small as "-9,223,372,036,854,775,808" and as large as "9,223,372,036,854,775,807". This post has explained how to use BIGINT as an alternative to Java’s LONG data type.