Skip to main content
0 reactions

How to Store Money in PostgreSQL: numeric vs money

Featured card for How to Store Money in PostgreSQL, with a terminal window listing numeric(6,2), 3.5 → 3.50, 4.999 → 5.00 and money: $3.50 → £3.50.
How to store money in PostgreSQL.

To store money in PostgreSQL, use numeric with two digits after the decimal point, such as numeric(10,2). It keeps every cent exact. Here’s how to store prices that way, and why the money and real types are worse choices.

Every command and output on this page is real, from PostgreSQL 19 on macOS.

Syntax

SyntaxSQL
column_name numeric(precision, scale)
  • precision: the most digits the number can have in all.
  • scale: how many of those digits come after the decimal point. For prices, 2.
  • So numeric(6,2) holds up to 9999.99. A value with more decimals is rounded to the scale, and a value with too many digits before the point is refused.

See Arbitrary Precision Numbers (opens in a new tab) in the PostgreSQL documentation.

The Example

A small café keeps its menu in a table: each item and its price, in a numeric(6,2) column.

Step 1: Create a Test Database

Connect to your server with psql as the postgres user, as Part 2 of the PostgreSQL Tutorial shows. psql connects you to the postgres database, and its prompt is postgres=#.

Create a database for the example and connect to it:

postgres=#SQL
CREATE DATABASE cafe;
OutputPlain text
CREATE DATABASE
postgres=#Plain text
\c cafe
OutputPlain text
You are now connected to database "cafe" as user "postgres".

Step 2: Create the Menu Table

cafe=#SQL
CREATE TABLE menu (item text, price numeric(6,2));
OutputPlain text
CREATE TABLE

Step 3: Add Prices

cafe=#SQL
INSERT INTO menu (item, price) VALUES ('Coffee', 3.5);
OutputPlain text
INSERT 0 1
cafe=#SQL
INSERT INTO menu (item, price) VALUES ('Cake', 4.999);
OutputPlain text
INSERT 0 1

Both are saved. A price too big for numeric(6,2) is refused:

cafe=#SQL
INSERT INTO menu (item, price) VALUES ('Golden latte', 12345.67);
OutputPlain text
ERROR:  numeric field overflow
DETAIL:  A field with precision 6, scale 2 must round to an absolute value less than 10^4.

Here’s the menu:

cafe=#SQL
SELECT * FROM menu;
OutputPlain text
  item  | price
--------+-------
 Coffee |  3.50
 Cake   |  5.00
(2 rows)

Every price has exactly two decimals: 3.5 is shown as 3.50, and 4.999 was rounded to 5.00.

Why Not real or double precision

real and double precision store close approximations, not exact values. real keeps only about 6 digits:

cafe=#SQL
SELECT 1234567.89::real;
OutputPlain text
    float4
---------------
 1.2345679e+06
(1 row)

The cents are gone. Don’t use these types for money.

Why Not the money Type

PostgreSQL also has a money type, which shows a currency symbol:

cafe=#SQL
SELECT 3.5::money;
OutputPlain text
 money
-------
 $3.50
(1 row)

But the symbol and the format come from a setting, lc_monetary, not from the value. Change the setting for your session, and the same amount shows another currency:

cafe=#SQL
SET lc_monetary TO 'en_GB.UTF-8';
OutputPlain text
SET
cafe=#SQL
SELECT 3.5::money;
OutputPlain text
 money
-------
 £3.50
(1 row)

The amount didn’t change, only its symbol. The documentation (opens in a new tab) warns that money values may not load correctly into a database with a different lc_monetary. With numeric, keep the currency in a column of its own when you need more than one.

Clean Up

To remove the example, connect to another database and drop the test database:

cafe=#Plain text
\c postgres
OutputPlain text
You are now connected to database "postgres" as user "postgres".
postgres=#SQL
DROP DATABASE cafe;
OutputPlain text
DROP DATABASE

Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.

Common Mistakes

MistakeWhat happensDo this instead
Prices in real or double precision1234567.89::real comes back as 1.2345679e+06.Use numeric(10,2).
A precision too small for your pricesERROR: numeric field overflowLeave room for your largest price, as numeric(10,2) does.
Relying on the money type’s symbolThe symbol follows lc_monetary, so $3.50 became £3.50.Use numeric, with the currency in its own column.

FAQ

Is numeric slower than real?

Yes. The documentation says calculations on numeric are very slow compared with the integer and floating-point types, but for money an exact answer matters more.

Is decimal the same as numeric?

Yes. decimal(10,2) and numeric(10,2) are the same type.

Conclusion

Store prices in numeric with a scale of 2, and a precision big enough for your largest amount. It keeps every cent, rounds extra decimals and refuses values that don’t fit. Leave real, double precision and money out of it.

Additional Resources

Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.

0 comments