I am trying to store some hashes (originally in hexadecimal) but I cast them to int to save in pgsql but get
select 12347933502038296527::bigint
ERROR: bigint out of range
********** Error **********
ERROR: bigint out of range
SQL state: 22003
I realize the number is too large for int8
type, what data type should I be using instead of int8? would decimal
work for my case? any other strategy that would work to save such large numbers?
PostgreSQL allows a type of integer type namely BIGINT . It requires 8 bytes of storage size and can store integers in the range of -9, 223, 372, 036, 854, 775, 808 to +9, 223, 372, 036, 854, 775, 807.
INT8 is a synonym for BIGINT.
The int data type is the primary integer data type in SQL Server. The bigint data type is intended for use when integer values might exceed the range that is supported by the int data type. bigint fits between smallmoney and int in the data type precedence chart.
Bigint's are pretty big. It is unusual to need anything larger than that. In most databases, that is as far as you can go with a native binary representation. Decimal
/numeric
often allows a slightly larger precision.
Postgres, by contrast, supports numeric
/decimal
with basically unlimited precision, as explained in the documentation.
The following works directly:
select 12347933502038296527::decimal
If you want to be more specific:
select 12347933502038296527::decimal(20, 0)
That said, often such large numbers are used as ids. In that case, you might want to use a string instead of a numeric representation. A string has the advantage of keeping leading zeros, for instance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With