Which PostgreSQL column type should be used to store a Java BigDecimal?

Tags: , ,



What PostgreSQL column type should I use to store a Java BigDecimal?

Answer

See PostgreSQL datatypes – perhaps Numeric, which can act as an arbitrary precision type (this is a PostgreSQL extension).

…without any precision or scale creates a column in which numeric values of any precision and scale can be stored, up to the implementation limit on precision.

I am not entirely sure what “implementation limit on precision is”, though. Have never tried really large numbers. If the limit is reached, then a fallback to text is possible. But, I suspect there will be significant other issues before then 😉 If you wish to store less, then specify a precision and/or scale to numeric.

Edit as sjr pointed out, the restriction is 1000 decimal digits of precision (from the same link):

The type numeric can store numbers with up to 1000 digits of precision [in currentl implementations] and perform calculations exactly. It is especially recommended for storing monetary amounts and other quantities where exactness is required…

If more precision is needed – despite having a much bigger problem at hand – then a numeric column will not be suitable (by itself). But this is really more of a very extreme “what if” and likely does not play a limiting role.



Source: stackoverflow