Site icon JD Bots

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

Syntax

column_name numeric(precision, scale)

See Arbitrary Precision Numbers 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=#

CREATE DATABASE cafe;

Output

CREATE DATABASE

postgres=#

\c cafe

Output

You are now connected to database "cafe" as user "postgres".

Step 2: Create the Menu Table

cafe=#

CREATE TABLE menu (item text, price numeric(6,2));

Output

CREATE TABLE

Step 3: Add Prices

cafe=#

INSERT INTO menu (item, price) VALUES ('Coffee', 3.5);

Output

INSERT 0 1

cafe=#

INSERT INTO menu (item, price) VALUES ('Cake', 4.999);

Output

INSERT 0 1

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

cafe=#

INSERT INTO menu (item, price) VALUES ('Golden latte', 12345.67);

Output

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=#

SELECT * FROM menu;

Output

  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=#

SELECT 1234567.89::real;

Output

    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=#

SELECT 3.5::money;

Output

 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=#

SET lc_monetary TO 'en_GB.UTF-8';

Output

SET

cafe=#

SELECT 3.5::money;

Output

 money
-------
 £3.50
(1 row)

The amount didn’t change, only its symbol. The documentation 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=#

\c postgres

Output

You are now connected to database "postgres" as user "postgres".

postgres=#

DROP DATABASE cafe;

Output

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

Related

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

Exit mobile version