CodePark/ questions / Schema design & normalisation
START PRACTICINGPRACTICE
Backend question · Databases & SQL › Schema design & normalisation

Which PostgreSQL column type is the right choice for storing prices?

numeric(12,2), or an integer number of cents

numeric stores exact decimal values, so 0.10 + 0.20 is exactly 0.30. An integer number of cents is also exact.

"
double precision" and "real" are floating-point types. They store binary approximations, so rounding errors build up in sums. "varchar(20)" is a string column. It can't do arithmetic, and it can't make sure the value is a number.