PostgreSQL's DOUBLE PRECISION type (also called FLOAT8 or FLOAT) is an inexact, variable-precision numeric type implementing the IEEE 754 double-precision standard. It is best suited for scientific measurements and calculations where approximate results are acceptable.
Key characteristics
- Storage: 8 bytes
- Range: approximately 1E-307 to 1E+308
- Precision: at least 15 significant decimal digits
- Synonyms:
DOUBLE PRECISION,FLOAT8,FLOAT
Basic usage: storing temperature readings
CREATE TABLE temperatures (
id SERIAL PRIMARY KEY,
location TEXT NOT NULL,
temperature DOUBLE PRECISION
);
INSERT INTO temperatures (location, temperature)
VALUES
('Lab Room 1', 23.5),
('Server Room 1', 21.8),
('Server Room 2', 24.3)
RETURNING *;
id | location | temperature
----+---------------+-------------
1 | Lab Room 1 | 23.5
2 | Server Room 1 | 21.8
3 | Server Room 2 | 24.3
SELECT AVG(temperature) FROM temperatures;
avg
------
23.2
The inexact nature of DOUBLE PRECISION
Because DOUBLE PRECISION uses binary floating-point, some decimal fractions cannot be stored exactly:
CREATE TABLE t (c double precision);
INSERT INTO t(c) VALUES (0.1), (0.1), (0.1);
SELECT SUM(c) FROM t;
sum
---------------------
0.30000000000000004
This is expected IEEE 754 behavior. For financial data, use NUMERIC instead.
Numbers outside the valid range cause an error:
INSERT INTO t(c) VALUES (1E-400);
-- ERROR: "0.000...0001" is out of range for type double precision
Production tips
- Never use
DOUBLE PRECISIONfor monetary values — useNUMERICto avoid floating-point rounding errors in financial calculations. - Avoid equality comparisons with
DOUBLE PRECISIONvalues (WHERE temp = 23.5) — use range comparisons instead (WHERE temp BETWEEN 23.4 AND 23.6). - Use
DOUBLE PRECISIONfor scientific measurements, GPS coordinates, and machine learning feature values where approximate precision is acceptable. - Arithmetic on
DOUBLE PRECISIONis significantly faster thanNUMERIC, making it appropriate for analytical workloads on large datasets.
Reference: PostgreSQL documentation — Floating-Point Types.