A comprehensive mapping of PostgreSQL, Diesel, and Rust types

by Steve Hoeksema, posted 31 Jan 2018.

Over the last year or so I’ve been learning Rust as time permits. With the release of Diesel 1.0, I’ve recently kicked off a pet project that makes heavy use of PostgreSQL types.

I couldn’t find a mapping between the PostgreSQL type, the Diesel schema type, and the Rust type, so I’ve compiled a mapping for what I could find.

Happily all the types I use have been implemented, it seems there’s only XML and PostGIS types remaining.

Updated as at Postgresql 10.1, Diesel 1.1.

PostgreSQL Type PostgreSQL Size Description Range Diesel Type Rust Type
Nullable Types   nullable   Nullable<T> Option<T>
Numeric Types          
smallint, int2 2 bytes signed integer -32768 to +32767 SmallInt i16
integer, int, int4 4 bytes signed integer -2147483648 to +2147483647 Integer i32
bigint, int8 8 bytes signed integer -9223372036854775808 to +9223372036854775807 BigInt i64
numeric(p, s), decimal(p, s) 2 bytes per 4 digits + 3 to 8 bytes exact numeric of selectable precision 131072.16383 digits Numeric bigdecimal::BigDecimal
real, float4 4 bytes single precision floating-point number 6 digits precision Float f32
double precision, float8 8 bytes double precision floating-point number 15 digits precision Double f64
smallserial, serial2 2 bytes autoincrementing integer 1 to 32767 SmallInt i16
serial, serial4 4 bytes autoincrementing integer 1 to 2147483647 Integer i32
bigserial, serial8 8 bytes autoincrementing integer 1 to 9223372036854775807 BigInt i64
Monetary Types          
money 8 bytes currency amount -92233720368547758.08 to +92233720368547758.07 Money Cents
Character Types          
character varying(n), varchar(n) n+1 or n+4 bytes variable-length character string   Text String, &str
character(n), char(n) n+1 or n+4 bytes fixed-length character string   Text String, &str
text n+1 or n+4 bytes variable-length character string   Text String, &str
Binary Data Types          
bytea n+1 to n+4 bytes binary data (“byte array”)   Binary Vec<u8>, &u8
Date/Time Types          
timestamp, timestamp(p) without time zone 8 bytes date and time of day 4713 BC to 294276 AD, 1 microsecond Timestamp chrono::NaiveDateTime
timestamptz, timestamp(p) with time zone 8 bytes date and time of day, with time zone 4713 BC to 294276 AD, 1 microsecond Timestamptz chrono::DateTime
date 4 bytes calendar date (year, month, day) 4713 BC to 5874897 AD, 1 day Date chrono::NaiveDate
time, time(p) without time zone 8 bytes time of day (no date) 00:00:00 to 24:00:00, 1 microsecond Time chrono::NaiveTime
timetz, time(p) with time zone 12 bytes time of day (no date), with time zone 00:00:00+1459 to 24:00:00-1459, 1 microsecond    
interval(fields)(p) 16 bytes time span -178000000 years to 178000000 years, 1 microsecond Interval PgInterval
Boolean Type          
boolean, bool 1 byte logical Boolean (true/false)   Bool bool
Geometric Types          
point (x,y) 16 bytes geometric point on a plane      
line {A,B,C} 32 bytes infinite line on a plane      
lseg ((x1,y1),(x2,y2)) 32 bytes finite line segment on a plane      
box ((x1,y1),(x2,y2)) 32 bytes rectangular box on a plane      
path ((x1,y1),...) 16+16n bytes closed geometric path on a plane      
path [(x1,y1),...] 16+16n bytes open geometric path on a plane      
polygon ((x1,y1),...) 40+16n bytes closed geometric path on a plane      
circle <(x,y),r\> 24 bytes circle on a plane      
Network Address Types          
cidr 7 or 19 bytes IPv4 or IPv6 network address   Cidr ipnetwork::IpNetwork
inet 7 or 19 bytes IPv4 or IPv6 host address   Inet ipnetwork::IpNetwork
macaddr 6 bytes MAC address   MacAddr [u8; 6]
macaddr8 8 bytes MAC address (EUI-64 format)      
Enumerated Types          
enum 4 bytes enumerated value   (user-defined) String, enum
Bit String Types          
bit(n) 1 byte per 8 bits + 5 or 8 bytes fixed-length bit string      
bit varying(n), varbit 1 byte per 8 bits + 5 or 8 bytes variable-length bit string      
Text Search Types          
tsvector   text search document   TsVector  
tsquery   text search query   TsQuery  
UUID Type          
uuid 16 bytes universally unique identifier   Uuid uuid::Uuid
XML Type          
xml   XML data      
JSON Types          
json   textual JSON data   Json serde_json::Value
jsonb   binary JSON data, decomposed   Jsonb serde_json::Value
Arrays          
t[]   array of values   Array<T> Vec<T>, Vec<Option<T>>, &[T], &[Option<T>]
Range Types          
int4range   range of integer   Range<Integer> (Bound<i32>, Bound<i32>)
int8range   range of bigint   Range<BigInt> (Bound<i64>, Bound<i64>)
numrange   range of numeric   Range<Numeric> (Bound<bigdecimal::BigDecimal>, Bound<bigdecimal::BigDecimal>)
tsrange   range of timestamp   Range<Timestamp> (Bound<chrono::NaiveDateTime>, Bound<chrono::NaiveDateTime>)
tstzrange   range of timestamptz   Range<Timestamptz> (Bound<chrono::DateTime>, Bound<chrono::DateTime>)
daterange   range of date   Range<Date> (Bound<chrono::NaiveDate>, Bound<chrono::NaiveDate>)
Also available as an Atom feed, or on Medium.
This post licensed as CC BY-NC-SA.