real
Learning Focus
Use this lesson to understand PostgreSQL real (single-precision floating point) and where approximate math is acceptable.
real Overview
- Storage: 4 bytes
- Approximate floating point
- Best for: scientific/measurement data where small rounding errors are acceptable
Example
CREATE TABLE sensors (
sensor_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
temperature_c real NOT NULL,
recorded_at timestamptz NOT NULL DEFAULT now()
);
Common Pitfalls
| Pitfall | Consequence | Prevention |
|---|---|---|
| Using real for currency | Rounding errors | Use numeric(12,2) |