Store single-precision measurements
A measurement or sensor table needs compact floating-point storage.
Drizzle ORM · Function
real() declares a PostgreSQL single-precision floating-point column in a Drizzle schema. PostgreSQL also names this type float4.
In Drizzle’s PostgreSQL schema API, call real() inside a pgTable definition to create a single-precision floating-point column. The column can also receive a default value, including a SQL expression when the default needs an explicit PostgreSQL cast.
real()A Drizzle PostgreSQL column builder representing the database real type.
.default(...) to real().real.real stores a single-precision floating-point value and occupies 4 bytes.import { real, pgTable } from "drizzle-orm/pg-core";
export const measurements = pgTable("measurements", {
value: real(),
});Defines a PostgreSQL table with a single-precision floating-point column.
A measurement or sensor table needs compact floating-point storage.
A schema requires PostgreSQL's native real/float4 type.
A column needs a numeric database default.
real column in Drizzle?Use real() from drizzle-orm/pg-core as a column builder inside pgTable. It maps to PostgreSQL’s real type, also known as float4.
Follow-up: How would you define a database default? Use .default(10.10), or provide a SQL expression such as sql'10.10'::real when an explicit cast is needed.
real() represent?The generated PostgreSQL type is real, and the documentation identifies it as float4. It represents a four-byte, single-precision floating-point number.
Follow-up: What trade-off follows from single precision? It uses less storage than higher-precision numeric types but cannot represent values with the same precision.
real() belong in a Drizzle schema?Import real from drizzle-orm/pg-core and place real() in the object passed to pgTable. This produces a typed schema declaration that Drizzle can use for migrations and queries.
Follow-up: How can the declaration be customized? Chain supported column methods, such as .default(...), on the builder before placing it in the table definition.
float4pgTablesqldefault