PostgreSQL Error 22003: Numeric Value Out of Range Fix
In this article
PostgreSQL error 22003 means a number you tried to store or cast does not fit the target column's precision and scale, so the server rejects it. The fix is to find the offending value, compare it with the column definition (for example NUMERIC(10,2) allows at most eight digits before the decimal point), and either widen the column, round the value, or correct the input. Nothing is corrupted by the error itself; the statement simply fails and rolls back.
What You'll Need
- Access to the database with a role that can read the table definition and, if a schema change is needed, alter it.
- A SQL client:
psql, pgAdmin, DBeaver, or any tool that shows the full error message and the failing statement. - The exact error text, which usually names the column or the cast that failed.
- For application code: the ability to change the insert/update statement or the model definition.
Step-by-Step
- Read the full error message. PostgreSQL returns something like
ERROR: numeric field overflowwithDETAIL: A field with precision 10, scale 2 must round to an absolute value less than 10^8.That detail tells you the limit. If you only see22003, check the server log or your client's message pane for the detail line. - Identify the failing statement. In
psql, the error appears right after the statement you ran. In an application, enable statement logging or check the stack trace to see which INSERT, UPDATE, or CAST triggered it. If the error comes from a function, the detail may name the internal cast rather than your column. - Inspect the target column definition. Run
\d+ your_tableinpsql, or queryinformation_schema.columnsfornumeric_precisionandnumeric_scale. Note the difference betweenNUMERIC(unconstrained),NUMERIC(10,2)(eight integer digits, two decimals), andNUMERIC(10)(ten integer digits, zero decimals). - Compare the value with the limit. For
NUMERIC(p,s), the maximum absolute value is just under 10^(p-s). ForNUMERIC(10,2)that is 99,999,999.99. Anything larger, including a value that rounds up to that boundary, fails. Also check negative values; the limit is symmetric. - Decide on the correct fix. If the data is genuinely valid and the column is too narrow, widen it:
ALTER TABLE your_table ALTER COLUMN amount TYPE NUMERIC(14,2);. This rewrites the table and takes an exclusive lock, so plan it for a quiet period on large tables. - If the value is wrong, fix the source. A common cause is arithmetic that multiplies by 100 twice, a currency conversion applied twice, or a unit mismatch (cents vs. dollars). Correct the calculation rather than the column.
- If rounding is acceptable, cast explicitly. Use
ROUND(value, 2)::NUMERIC(10,2)so the rounding is deliberate. Do not rely on implicit casts, which can silently change scale in other contexts. - Check for overflow in expressions, not just columns. A cast like
'1e20'::NUMERIC(10,2)fails even if no table is involved. Review any::NUMERIC(p,s)casts in views, functions, and generated columns. - Re-run the statement and verify. Confirm the row inserts or updates, then run a quick aggregate such as
SELECT MAX(ABS(amount)) FROM your_table;to make sure no other value is near the boundary. - Prevent recurrence. Add a CHECK constraint or application-side validation that rejects values outside the column's range before they reach the database, and log the rejected value so the source can be traced.
Common Mistakes
- Confusing precision and scale.
NUMERIC(10,2)does not mean ten digits total including two decimals in the way many people assume; it means ten significant digits with two after the point, leaving eight before it. - Assuming
NUMERICwithout parameters is safe. UnconstrainedNUMERICaccepts very large values but is slower and does not enforce a business limit, so bad data can still slip in. - Widening the column when the calculation is wrong. If a value is 100 times too large, a wider column hides the bug instead of fixing it.
- Forgetting generated columns, views, and function return types. The failing cast may be in a view definition, not the base table.
- Ignoring the rounding boundary. A value of 99,999,999.995 rounds to 100,000,000.00, which overflows
NUMERIC(10,2)even though the raw input looked smaller. - Running a widening ALTER on a busy table without planning. The lock can block writes for the duration of the rewrite.
Tips
- Use
NUMERICfor money and exact quantities, andDOUBLE PRECISIONonly when approximate values are acceptable; floating-point types have their own overflow behaviour and different error paths. - Store monetary amounts in the smallest unit (cents) as
BIGINTif you want to avoid scale decisions entirely, then format on display. - When importing data, run a pre-check query that flags values outside the target range before the load, so the import fails fast with a useful list.
- Keep the original error detail in your logs;
22003alone is not enough to diagnose which column or cast failed. - Test schema changes on a copy of the table first, especially when the table is large or has dependent views.
Troubleshooting
If the error persists after widening the column, check whether the value passes through an intermediate cast with a smaller precision, such as a function parameter typed NUMERIC(10,2) or a CTE that casts early. Inspect views with \d+ view_name and function definitions with \df+ function_name.
If the error appears only in your application and not in psql, the driver may be sending the value as a string and letting PostgreSQL cast it, or the ORM may be applying its own precision. Log the exact SQL and parameters the driver sends.
If the error occurs during COPY or bulk insert, the row number in the error message points to the offending line; extract that line and compare it with the column definition.
If you cannot change the schema because of permissions, ask the owner to widen the column or add a cast in the query, and validate values in the application in the meantime.
Frequently Asked Questions
What does error 22003 mean exactly?
It is the SQLSTATE for numeric value out of range: a value could not be represented in the target numeric type, precision, or scale.
Does the failed statement leave partial data behind?
No. The statement is rolled back. If it was part of a larger transaction, the whole transaction is aborted unless you used a savepoint.
Why does a value that looks small still overflow?
Because scale consumes digits. NUMERIC(5,2) allows only three integer digits, so 1,000.00 overflows even though it is a modest number.
Can I just change the column to plain NUMERIC?
Yes, but you lose the enforced limit and may see slower arithmetic. Prefer a wider explicit precision that matches your business rules.
Does this error affect indexes or other rows?
No. Only the failing statement is rejected; existing rows and indexes are unchanged.
How do I find which column caused it in a large INSERT?
Check the DETAIL line, which usually names the column, or split the insert into single-column tests. Statement logging also shows the exact parameters.