Expand description
Conversions between Rust and Postgres types.
Types
Rust type | Postgres type(s) |
---|---|
bool | BOOL |
i8 | “CHAR” |
i16 | SMALLINT, SMALLSERIAL, INT2 |
i32 | INT, SERIAL, INT4 |
i64 | BIGINT, BIGSERIAL, INT8 |
f32 | REAL, FLOAT4 |
f64 | DOUBLE PRECISION, FLOAT8 |
&str , String | VARCHAR, CHAR(N), TEXT, NAME |
&[u8] , Vec<u8> | BYTEA |
PgInterval | INTERVAL |
PgRange<T> | INT8RANGE, INT4RANGE, TSRANGE, TSTZRANGE, DATERANGE, NUMRANGE |
PgMoney | MONEY |
bigdecimal
Requires the bigdecimal
Cargo feature flag.
Rust type | Postgres type(s) |
---|---|
bigdecimal::BigDecimal | NUMERIC |
decimal
Requires the decimal
Cargo feature flag.
Rust type | Postgres type(s) |
---|---|
rust_decimal::Decimal | NUMERIC |
chrono
Requires the chrono
Cargo feature flag.
Rust type | Postgres type(s) |
---|---|
chrono::DateTime<Utc> | TIMESTAMPTZ |
chrono::DateTime<Local> | TIMESTAMPTZ |
chrono::NaiveDateTime | TIMESTAMP |
chrono::NaiveDate | DATE |
chrono::NaiveTime | TIME |
[PgTimeTz ] | TIMETZ |
time
Requires the time
Cargo feature flag.
Rust type | Postgres type(s) |
---|---|
time::PrimitiveDateTime | TIMESTAMP |
time::OffsetDateTime | TIMESTAMPTZ |
time::Date | DATE |
time::Time | TIME |
[PgTimeTz ] | TIMETZ |
uuid
Requires the uuid
Cargo feature flag.
Rust type | Postgres type(s) |
---|---|
uuid::Uuid | UUID |
ipnetwork
Requires the ipnetwork
Cargo feature flag.
Rust type | Postgres type(s) |
---|---|
ipnetwork::IpNetwork | INET, CIDR |
mac_address
Requires the mac_address
Cargo feature flag.
Rust type | Postgres type(s) |
---|---|
mac_address::MacAddress | MACADDR |
bit-vec
Requires the bit-vec
Cargo feature flag.
Rust type | Postgres type(s) |
---|---|
bit_vec::BitVec | BIT, VARBIT |
json
Requires the json
Cargo feature flag.
Rust type | Postgres type(s) |
---|---|
[Json<T> ] | JSON, JSONB |
serde_json::Value | JSON, JSONB |
&serde_json::value::RawValue | JSON, JSONB |
Value
and RawValue
from serde_json
can be used for unstructured JSON data with
Postgres.
Json<T>
can be used for structured JSON data with Postgres.
Composite types
User-defined composite types are supported through a derive for Type
.
CREATE TYPE inventory_item AS (
name text,
supplier_id integer,
price numeric
);
#[derive(sqlx::Type)]
#[sqlx(type_name = "inventory_item")]
struct InventoryItem {
name: String,
supplier_id: i32,
price: BigDecimal,
}
Anonymous composite types are represented as tuples. Note that anonymous composites may only be returned and not sent to Postgres (this is a limitation of postgres).
Arrays
One-dimensional arrays are supported as Vec<T>
or &[T]
where T
implements Type
.
Enumerations
User-defined enumerations are supported through a derive for Type
.
CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
#[derive(sqlx::Type)]
#[sqlx(type_name = "mood", rename_all = "lowercase")]
enum Mood { Sad, Ok, Happy }
Rust enumerations may also be defined to be represented as an integer using repr
.
The following type expects a SQL type of INTEGER
or INT4
and will convert to/from the
Rust enumeration.
#[derive(sqlx::Type)]
#[repr(i32)]
enum Mood { Sad = 0, Ok = 1, Happy = 2 }
Structs
The PostgreSQL OID
type stores an object identifier,
used internally by PostgreSQL as primary keys for various system tables.
Container for a Label Tree Query (lquery
) in Postgres.
Modifiers that can be set to non-star labels
Container for a Label Tree (ltree
) in Postgres.
The PostgreSQL MONEY
type stores a currency amount with a fixed fractional
precision. The fractional precision is determined by the database’s
lc_monetary
setting.
Enums
Represents ltree specific errors