3

I create a table like this

CREATE TABLE foo (
    id serial CONSTRAINT id_pk PRIMARY KEY,
    bar varchar(256),
    test decimal(5,5)
);

I then want to populate the test column

 INSERT INTO foo (test) VALUES (12345.12345)

This gives me

Numeric field overflow Detail: A field with precision 5, scale 5must round to an absolute value less than 1.

I do not understand this. I though if I set a column to decimal(5,5), I must have five digits to the left of the comma and five digits to the right of the comma. I have that with the number 12345.12345.

Why does it give me this error?

3
  • 1
    Because decimal precision is not what you think it is. The first number is the total number of decimal digits, not the number of digits before the separator. You want decimal(10, 5). Commented Jul 9, 2021 at 7:08
  • Try with DECIMAL(10,5). Parameter on left side of comma is total length, parameter on right side I number of decimals. Commented Jul 9, 2021 at 7:09
  • The psql tag refers to the command line tool and is irrelevant here. Please do not add it Commented Jul 9, 2021 at 7:15

1 Answer 1

11

You misunderstood how specifying a decimal works (and it is a bit confusing I agree).

Quote from the manual

The precision of a numeric is the total count of significant digits in the whole number, that is, the number of digits to both sides of the decimal point. The scale of a numeric is the count of decimal digits in the fractional part, to the right of the decimal point

So the first number defines the total number of digits, the second one defines the number of decimals.

If you want 5 digits before the decimal point and five after it, you need to use decimal(10,5)

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

1 Comment

@Stophface . . . Just to emphasize: This is how SQL defines scale and precision, so it works like this in all databases, not just Postgres.

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.