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
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:
CREATE DATABASE cafe;
CREATE DATABASE
\c cafe
You are now connected to database "cafe" as user "postgres".
Step 2: Create the Menu Table
CREATE TABLE menu (item text, price numeric(6,2));
CREATE TABLE
Step 3: Add Prices
INSERT INTO menu (item, price) VALUES ('Coffee', 3.5);
INSERT 0 1
INSERT INTO menu (item, price) VALUES ('Cake', 4.999);
INSERT 0 1
Both are saved. A price too big for numeric(6,2) is refused:
INSERT INTO menu (item, price) VALUES ('Golden latte', 12345.67);
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:
SELECT * FROM menu;
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:
SELECT 1234567.89::real;
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:
SELECT 3.5::money;
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:
SET lc_monetary TO 'en_GB.UTF-8';
SET
SELECT 3.5::money;
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:
\c postgres
You are now connected to database "postgres" as user "postgres".
DROP DATABASE cafe;
DROP DATABASE
Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Prices in real or double precision | 1234567.89::real comes back as 1.2345679e+06. | Use numeric(10,2). |
| A precision too small for your prices | ERROR: numeric field overflow | Leave room for your largest price, as numeric(10,2) does. |
Relying on the money type’s symbol | The 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
- PostgreSQL 19 documentation: Numeric Types (opens in a new tab)
- PostgreSQL 19 documentation: Monetary Types (opens in a new tab)
Related
- PostgreSQL Tutorial — Part 5: Data Types Explained
- smallint vs integer vs bigint in PostgreSQL
- text vs varchar in PostgreSQL
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