Module sqlx_postgres::types

source ·
Expand description

Conversions between Rust and Postgres types.

Types

Rust typePostgres type(s)
boolBOOL
i8“CHAR”
i16SMALLINT, SMALLSERIAL, INT2
i32INT, SERIAL, INT4
i64BIGINT, BIGSERIAL, INT8
f32REAL, FLOAT4
f64DOUBLE PRECISION, FLOAT8
&str, StringVARCHAR, CHAR(N), TEXT, NAME
&[u8], Vec<u8>BYTEA
()VOID
PgIntervalINTERVAL
PgRange<T>INT8RANGE, INT4RANGE, TSRANGE, TSTZRANGE, DATERANGE, NUMRANGE
PgMoneyMONEY
PgLTreeLTREE
PgLQueryLQUERY

bigdecimal

Requires the bigdecimal Cargo feature flag.

Rust typePostgres type(s)
bigdecimal::BigDecimalNUMERIC

rust_decimal

Requires the rust_decimal Cargo feature flag.

Rust typePostgres type(s)
rust_decimal::DecimalNUMERIC

chrono

Requires the chrono Cargo feature flag.

Rust typePostgres type(s)
chrono::DateTime<Utc>TIMESTAMPTZ
chrono::DateTime<Local>TIMESTAMPTZ
chrono::NaiveDateTimeTIMESTAMP
chrono::NaiveDateDATE
chrono::NaiveTimeTIME
[PgTimeTz]TIMETZ

time

Requires the time Cargo feature flag.

Rust typePostgres type(s)
time::PrimitiveDateTimeTIMESTAMP
time::OffsetDateTimeTIMESTAMPTZ
time::DateDATE
time::TimeTIME
[PgTimeTz]TIMETZ

uuid

Requires the uuid Cargo feature flag.

Rust typePostgres type(s)
uuid::UuidUUID

ipnetwork

Requires the ipnetwork Cargo feature flag.

Rust typePostgres type(s)
ipnetwork::IpNetworkINET, CIDR
std::net::IpAddrINET, CIDR

Note that because IpAddr does not support network prefixes, it is an error to attempt to decode an IpAddr from a INET or CIDR value with a network prefix smaller than the address’ full width: /32 for IPv4 addresses and /128 for IPv6 addresses.

IpNetwork does not have this limitation.

mac_address

Requires the mac_address Cargo feature flag.

Rust typePostgres type(s)
mac_address::MacAddressMACADDR

bit-vec

Requires the bit-vec Cargo feature flag.

Rust typePostgres type(s)
bit_vec::BitVecBIT, VARBIT

json

Requires the json Cargo feature flag.

Rust typePostgres type(s)
Json<T>JSON, JSONB
serde_json::ValueJSON, JSONB
&serde_json::value::RawValueJSON, 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

Traits

  • Provides information necessary to encode and decode Postgres arrays as compatible Rust types.