Types and adapters¶
Types and adapters explains when each representation applies.
Decoding and adapters¶
Decoding
dataclass
¶
Decoding(
*,
arrays: Literal['list', 'exact'] | None = None,
geometry: Literal['exact', 'gometry'] | None = None,
json: Literal['parsed', 'document'] | None = None,
temporal: Literal['standard', 'exact'] | None = None,
unknown: Literal['value', 'error'] | None = None,
)
Row decoding policy for a connection or individual call. Unspecified fields inherit the connection policy.
With no overrides, arrays become lists, PostGIS rows use gometry, JSON is
parsed, temporal values use standard datetime types, and unknown types return
qualified PgValue objects. Use geometry='exact' for EWKB without gometry.
Arrow result fields have their own schema and do not use these row defaults.
Codec
dataclass
¶
Codec(
type: Identifier,
python_type: type[_T],
*,
format: Literal['text', 'binary'],
decode: Callable[[memoryview], _T] | None = None,
encode: Callable[[_T], Buffer] | None = None,
)
Encode or decode raw field bytes in PostgreSQL 'text' or 'binary' format.
type must be a schema-qualified two-part Identifier. Provide decode,
encode, or both; omitted directions use native conversion.
Adapter
dataclass
¶
Adapter(
type: Identifier,
python_type: type[_T],
*,
decode: Callable[[_Native], _T] | None = None,
encode: Callable[[_T], _Native] | None = None,
)
Map an application value to or from the native Python representation of a SQL type.
type must be a schema-qualified two-part Identifier. Provide decode,
encode, or both; omitted directions use native conversion.
EnumBinding
dataclass
¶
EnumBinding(
type: Identifier,
enum: type[_E],
*,
labels: Literal['name', 'value'] | Mapping[_E, str],
)
Bind a schema-qualified two-part Identifier for a PostgreSQL enum to a Python Enum.
Match members by 'name', by 'value', or via an explicit {member: label} mapping.
CompositeBinding
dataclass
¶
CompositeBinding(
type: Identifier,
factory: Callable[..., _T],
*,
fields: Mapping[str, str] | None = None,
encode: Callable[[_T], Mapping[str, object]] | None = None,
)
Bind a schema-qualified two-part Identifier for a PostgreSQL composite type to a Python factory.
Decoding passes SQL attributes to factory as keyword arguments. fields,
when given, maps {sql_attr: python_kwarg}; encode, when given, returns
{sql_attr: value}.
encode
class-attribute
instance-attribute
¶
encode: Callable[[_T], Mapping[str, object]] | None = None
PgValue
dataclass
¶
PgValue(data: str | Buffer, /, *, type: Identifier)
Owning text or binary payload labelled with its PostgreSQL schema and type name.
A label identifies the intended type, not an equivalent definition in another database. Text uses the destination's input routine and session settings; binary data must already have the representation expected by that type's receive function.
format
property
¶
format: Literal['text', 'binary']
PostgreSQL protocol format of the owned payload.
Temporal¶
PgDate
¶
PgDate(year: int, month: int, day: int)
PgTime
¶
PgTime(
hour: int = 0,
minute: int = 0,
second: int = 0,
microsecond: int = 0,
*,
utc_offset: timedelta | None = None,
)
PgTimestamp
¶
A finite PostgreSQL timestamp or timestamptz outside or inside Python's datetime range.
from_datetime
staticmethod
¶
from_datetime(value: datetime) -> PgTimestamp
Construct from a datetime.datetime. Aware values are normalized to UTC; naive values stay naive.
to_datetime
¶
to_datetime() -> datetime
Convert to a datetime.datetime (UTC-aware when zoned, naive otherwise). Raises OverflowError outside years 1-9999.
Infinity
¶
Interval
¶
Interval(*, months: int = 0, days: int = 0, microseconds: int = 0)
A finite PostgreSQL interval of months, days, and microseconds, with no normalization between them.
Containers, text, and binary¶
Range
dataclass
¶
Range(
lower: _T_co | None = None,
upper: _T_co | None = None,
*,
bounds: _Bounds = '[)',
)
A pair of optional endpoints. None means unbounded; only Range.empty() is empty.
MultiRange
dataclass
¶
MultiRange(ranges: Iterable[Range[_T_co]] = ())
An immutable sequence of ranges; input order is preserved without merging or sorting.
Array
dataclass
¶
Array(
values: Iterable[_T_co],
*,
dimensions: Sequence[int] | None = None,
lower_bounds: Sequence[int] | None = None,
)
Shape-preserving PostgreSQL array owning flat elements, dimension lengths, and lower bounds. Empty arrays have no axes.
BitString
¶
Json
dataclass
¶
Json(value: _T_co)
Bind a Python value as a JSON value, encoded when the statement is admitted.
Needed only where a bare parameter means something else: Json('text') is a JSON
string, whereas a bare str is JSON source text, and Json(None) is JSON null,
whereas a bare None is SQL NULL.
JsonDocument
dataclass
¶
JsonDocument(text: str | Buffer)
Geometric¶
PgPoint
¶
PgPoint(x: float, y: float)
PgLine
¶
PgLine(a: float, b: float, c: float)
PgLineSegment
¶
PgLineSegment(start: _PointLike, end: _PointLike)
PgBox
¶
PgBox(lower: _PointLike, upper: _PointLike)
PgPath
¶
PgPath(points: Iterable[_PointLike], *, closed: bool = False)
PgPolygon
¶
PgPolygon(points: Iterable[_PointLike])
PgCircle
¶
PgCircle(center: _PointLike, radius: float)
pgvector¶
Vector
¶
Vector(values: Iterable[float] | Buffer)
An immutable pgvector vector of finite float32 components.
Supports the sequence and buffer protocols. Rejects non-finite values
(at float32 precision) and raw bytes/bytearray inputs.
HalfVector
¶
HalfVector(values: Iterable[float] | Buffer)
An immutable pgvector halfvec of finite float16 components.
Supports the sequence and buffer protocols. Rejects non-finite values
(at float16 precision) and raw bytes/bytearray inputs.
SparseVector
¶
An immutable pgvector sparsevec: sorted zero-based indices, finite nonzero float32 components, and a positive dimension count.