AI Hints
Home›PostgreSQL Tutorial›Numbers, Text and Booleans
PostgreSQL · Lesson 10 of 120

Numbers: integer, numeric, double

Pick the right one.

⏱ 2 min read▶ Video at 7:53🎓 Free
Watch this lesson in the PostgreSQL courseWatch on YouTube ↗Full PostgreSQL course

Postgres has several number types, and picking the right one matters, especially for money. Floating point numbers can't store some decimals exactly.

  • integer
  • numeric
  • double precision

Exact vs approximate

Point one plus point two as double precision isn't exactly point three. As numeric, it is. Run it. Double gives false, numeric gives true. That's why prices use numeric.

query.sqlsql
SELECT 0.1::double precision + 0.2 = 0.3 AS double_ok,
       0.1::numeric + 0.2 = 0.3          AS numeric_ok;
Output
 double_ok | numeric_ok
-----------+------------
 f         | t
(1 row)

Which type?

Integer is for counts and IDs. Big int is for very large counts. Numeric stores exact decimals, perfect for money. Double precision is fast, for science and measurements.

TypeUse for
integercounts, IDs
biginthuge counts
numeric(10,2)money
double precisionmeasurements

Summary

Use integer for counts, numeric for money, and double precision for measurements. Next, text types.

Key takeaways
  • integer
  • numeric
  • double
Next up: Text Types — text, varchar, char