158

What are the uses of the decimal and numeric datatypes in PostgreSQL?

The explanation given to these datatypes in documentation at https://www.postgresql.org/docs/current/datatype-numeric.html says

Description: user specified precision, exact
Range: up to 131072 digits before the decimal point; up to 16383 digits after the decimal point

2
  • 1
    Cross post: dba.stackexchange.com/q/121196/1822 Commented Nov 16, 2015 at 8:40
  • I created a table with DECIMAL columns, and Pg12 converted them to NUMERIC. Commented Jan 13, 2023 at 17:28

4 Answers 4

157

Right from the manual:

The types decimal and numeric are equivalent. Both types are part of the SQL standard.

As for the "why do I need to use it", this is also explained in the manual:

The type numeric can store numbers with a very large number of digits and perform calculations exactly

(Emphasis mine).

If you need numbers with decimals, use decimal (or numeric) if you need numbers without decimals, use integer or bigint. A typical use of decimal as a column type would be a "product price" column or an "interest rate". A typical use of an integer type would be e.g. a column that stores how many products were ordered (assuming you can't order "half" a product).

double and real are also types that can store decimal values, but they are approximate types. This means you don't necessarily retrieve the value you stored. For details please see: http://floating-point-gui.de/

Sign up to request clarification or add additional context in comments.

2 Comments

The phrase 'there is no difference' is neither a quote from the manual nor from your answer. 'decimal and numeric are equivalent' is a quote from both, but the OP was aware of that and asked for the difference, which the other answers addressed.
just because they are both part of the standard, does not mean they are equivalent.
75

Quoted straight from https://www.postgresql.org/message-id/[email protected]

There isn't any difference, in Postgres. There are two type names because the SQL standard requires us to accept both names. In a quick look in the standard it appears that the only difference is this:

     17)NUMERIC specifies the data type exact numeric, with the decimal
        precision and scale specified by the <precision> and <scale>.

     18)DECIMAL specifies the data type exact numeric, with the decimal
        scale specified by the <scale> and the implementation-defined
        decimal precision equal to or greater than the value of the
        specified <precision>.

ie, for DECIMAL the implementation is allowed to allow more digits than requested to the left of the decimal point. Postgres doesn't exercise that freedom so there's no difference between these types for us.

      regards, tom lane

Comments

26

They are the synonym of each other and functionally same. The SQL:2003 standard says:

21) NUMERIC specifies the data type
    exact numeric, with the decimal
    precision and scale specified by the
    <precision> and <scale>.

22) DECIMAL specifies the data type
    exact numeric, with the decimal scale
    specified by the <scale> and the
    implementation-defined decimal
    precision equal to or greater than the
    value of the specified <precision>.

1 Comment

I know it's been almost ten years, but.. 22 above has different words, and twice the paragraph with more conditions, you can't use that text to say they are the same because according to the standard, if that's what you pasted, is not the same..
1

There are two points of view - are these data types the same in the ANSI SQL standard, and are these data types the same in PostgreSQL.

For PostgreSQL the answer is yes, they are exactly the same (https://www.postgresql.org/docs/current/datatype-numeric.html):

The types decimal and numeric are equivalent. Both types are part of the SQL standard.

They are not exactly equivalent in the ANSI SQL standard. NUMERIC requires the database to store the floating point number exactly as specified, with this exact precision and scale. DECIMAL is required to store the floating point with precision at least as requested, but with scale equal to one than requested.

In most modern RDBMs, DECIMAL and NUMERIC are equivalent.

2 Comments

There is no "answer above", the order of the answers depends on your choice in your GUI. Click on "share" under an answer to get a link to it.
Quote credit should be in always-visible text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.