[][src]Module sqlx::postgres::types

This is supported on feature="postgres" only.

Conversions between Rust and Postgres types.

Types

Rust typePostgres type(s)
boolBOOL
i16SMALLINT, SMALLSERIAL, INT2
i32INT, SERIAL, INT4
i64BIGINT, BIGSERIAL, INT8
f32REAL, FLOAT4
f64DOUBLE PRECISION, FLOAT8
&str, StringVARCHAR, CHAR(N), TEXT, NAME
&[u8], Vec<u8>BYTEA

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

time

Requires the time Cargo feature flag.

Rust typePostgres type(s)
time::PrimitiveDateTimeTIMESTAMP
time::OffsetDateTimeTIMESTAMPTZ
time::DateDATE
time::TimeTIME

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

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
);
ⓘThis example is not tested
#[derive(sqlx::Type)]
#[sqlx(rename = "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');
ⓘThis example is not tested
#[derive(sqlx::Type)]
#[sqlx(rename = "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.

ⓘThis example is not tested
#[derive(sqlx::Type)]
#[repr(i32)]
enum Mood { Sad = 0, Ok = 1, Happy = 2 }

Nullable

In addition, Option<T> is supported where T implements Type. An Option<T> represents a potentially NULL value from Postgres.