Rust and Postgres
Introductin
Quick into to Rust and Postgres
Rust and Postgres Types
PostgreSQL Type | Description | Rust Type | Notes |
---|---|---|---|
smallint, int2 | 2-byte signed integer | i16 | |
integer, int4 | 4-byte signed integer | i32 | Most common integer type |
bigint, int8 | 8-byte signed integer | i64 | |
serial, serial4 | Auto-incrementing int4 | i32 | Often used for primary keys |
bigserial, serial8 | Auto-incrementing int8 | i64 | |
real, float4 | 4-byte float | f32 | |
double precision, float8 | 8-byte float | f64 | |
numeric, decimal | Arbitrary precision | rust_decimal::Decimal | Requires `postgres-decimal` crate |
money | Currency amount | i64 or custom type | Often wrapped for precision |
boolean | True/false | bool | |
character varying(n), varchar(n) | Variable-length string | String | |
character(n), char(n) | Fixed-length string | String | |
text | Unbounded string | String | |
bytea | Binary data | Vec<u8> | |
date | Calendar date | chrono::NaiveDate | |
timestamp | Date and time (no timezone) | chrono::NaiveDateTime | |
timestamptz | Date and time with timezone | chrono::DateTime<Utc> or <FixedOffset> | |
time | Time of day | chrono::NaiveTime | |
timetz | Time with timezone | chrono::DateTime<FixedOffset> | |
interval | Time span | postgres_types::Interval or custom | |
uuid | Universally unique ID | uuid::Uuid | |
json, jsonb | JSON data | serde_json::Value | |
enum | Custom enum | Rust enum with #[derive(ToSql, FromSql)] | |
composite | Structured type | Rust struct with #[derive(ToSql, FromSql)] |
Macro to Do the Mapping
Here is a macro to enable getting of Postgres type to Rust type
#[macro_export]
macro_rules! get_field {
($row:expr, $col:expr, $type:ty) => {{
$row.try_get::<_, $type>($col).map_err(|e| {
crate::types::DatabaseError::Deserialization {
column: $col.to_string(),
source: e,
}
})?
}};
}