[−][src]Module sqlx::postgres::types
feature="postgres" only.Conversions between Rust and Postgres types.
Types
| Rust type | Postgres type(s) |
|---|---|
bool | BOOL |
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 |
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 |
time
Requires the time Cargo feature flag.
| Rust type | Postgres type(s) |
|---|---|
time::PrimitiveDateTime | TIMESTAMP |
time::OffsetDateTime | TIMESTAMPTZ |
time::Date | DATE |
time::Time | TIME |
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 |
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(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');
#[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.
#[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.