Skip to content

Instantly share code, notes, and snippets.

@omid
Last active December 11, 2023 11:27
Show Gist options
  • Save omid/fcc7c93173644988aac0c80c658607fa to your computer and use it in GitHub Desktop.
Save omid/fcc7c93173644988aac0c80c658607fa to your computer and use it in GitHub Desktop.
A comprehensive mapping of PostgreSQL, Diesel, and Rust types
PostgreSQL Type Diesel Type Rust Type Description Range
Nullable Types Nullable<T> Option<T> nullable
Numeric Types
smallint, int2 SmallInt i16 signed integer -32768 to +32767
integer, int, int4 Integer i32 signed integer -2147483648 to +2147483647
bigint, int8 BigInt i64 signed integer -9223372036854775808 to +9223372036854775807
numeric(p, s), decimal(p, s) Numeric bigdecimal::BigDecimal exact numeric of selectable precision 131072.16383 digits
real, float4 Float f32 single precision floating-point number 6 digits precision
double precision, float8 Double f64 double precision floating-point number 15 digits precision
smallserial, serial2 SmallInt i16 autoincrementing integer 1 to 32767
serial, serial4 Integer i32 autoincrementing integer 1 to 2147483647
bigserial, serial8 BigInt i64 autoincrementing integer 1 to 9223372036854775807
Monetary Types
money Money Cents currency amount -92233720368547758.08 to +92233720368547758.07
Character Types
character varying(n), varchar(n) Text String, &str variable-length character string
character(n), char(n) Text String, &str fixed-length character string
text Text String, &str variable-length character string
Binary Data Types
bytea Binary Vec<u8>, &u8 binary data (“byte array”)
Date/Time Types
timestamp, timestamp(p) without time zone Timestamp chrono::NaiveDateTime date and time of day 4713 BC to 294276 AD, 1 microsecond
timestamptz, timestamp(p) with time zone Timestamptz chrono::DateTime date and time of day, with time zone 4713 BC to 294276 AD, 1 microsecond
date Date chrono::NaiveDate calendar date (year, month, day) 4713 BC to 5874897 AD, 1 day
time, time(p) without time zone Time chrono::NaiveTime time of day (no date) 00:00:00 to 24:00:00, 1 microsecond
timetz, time(p) with time zone time of day (no date), with time zone 00:00:00+1459 to 24:00:00-1459, 1 microsecond
interval(fields)(p) Interval PgInterval time span -178000000 years to 178000000 years, 1 microsecond
Boolean Type
boolean, bool Bool bool logical Boolean (true/false)
Geometric Types
point (x,y) geometric point on a plane
line {A,B,C} infinite line on a plane
lseg ((x1,y1),(x2,y2)) finite line segment on a plane
box ((x1,y1),(x2,y2)) rectangular box on a plane
path ((x1,y1),...) closed geometric path on a plane
path [(x1,y1),...] open geometric path on a plane
polygon ((x1,y1),...) closed geometric path on a plane
circle <(x,y),r&gt; circle on a plane
Network Address Types
cidr Cidr ipnetwork::IpNetwork IPv4 or IPv6 network address
inet Inet ipnetwork::IpNetwork IPv4 or IPv6 host address
macaddr MacAddr [u8; 6] MAC address
macaddr8 MAC address (EUI-64 format)
Enumerated Types
enum (user-defined) String, enum enumerated value
Bit String Types
bit(n) fixed-length bit string
bit varying(n), varbit variable-length bit string
Text Search Types
tsvector TsVector text search document
tsquery TsQuery text search query
UUID Type
uuid Uuid uuid::Uuid universally unique identifier
XML Type
xml XML data
JSON Types
json Json serde_json::Value textual JSON data
jsonb Jsonb serde_json::Value binary JSON data, decomposed
Arrays
t[] Array<T> Vec<T>, Vec<Option<T>>, &[T], &[Option<T>] array of values
Range Types
int4range Range<Integer> (Bound<i32>, Bound<i32>) range of integer
int8range Range<BigInt> (Bound<i64>, Bound<i64>) range of bigint
numrange Range<Numeric> (Bound<bigdecimal::BigDecimal>, Bound<bigdecimal::BigDecimal>) range of numeric
tsrange Range<Timestamp> (Bound<chrono::NaiveDateTime>, Bound<chrono::NaiveDateTime>) range of timestamp
tstzrange Range<Timestamptz> (Bound<chrono::DateTime>, Bound<chrono::DateTime>) range of timestamptz
daterange Range<Date> (Bound<chrono::NaiveDate>, Bound<chrono::NaiveDate>) range of date
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment