Rust speed. Python feel. All of PostgreSQL.¶
Gres is a PostgreSQL and PostGIS driver for Python, with a Rust core, strict types end to end, and Arrow, COPY and spatial data built in.
Gres delivered up to 2.5× the pooled throughput and up to 2.4× the binary COPY speed of asyncpg and Psycopg. Explore the benchmarks.
-
Rust core
The wire protocol, type codecs and bulk transfer are implemented in Rust and run on a dedicated I/O thread. Repeated statements are prepared automatically, and batched writes are pipelined.
-
Static typing
Select a scalar, tuple or dataclass as the row type. Use exact representations when you need to retain array bounds, infinite dates or the JSON text returned by PostgreSQL.
-
Arrow and COPY
Results stream as Apache Arrow record batches. Binary
COPYloads tables from Python rows or Arrow data. Memory stays bounded in both directions. -
PostGIS
geometryandgeographyvalues arrive as gometry objects carrying the database's CRS, as GeoArrow columns, or as exact EWKB. -
TLS and authentication
TCP connections verify the server certificate and host name by default. SCRAM-SHA-256 with channel binding, OAuth, Kerberos, and client certificates are supported.
-
Defined failure behavior
Gres distinguishes a statement it did not send from one that may have executed. It never replays a write. After cancellation, a pool reuses the session only if it can restore a known state.
A first query¶
import asyncio
from dataclasses import dataclass
from gres import Pool
@dataclass
class Station:
id: int
name: str
async def main() -> None:
async with Pool() as db:
station = await db.fetch_one(
"SELECT id, name FROM (VALUES (1, 'Harbour'), (2, 'Airport'))"
' AS s(id, name) WHERE id = $1',
(1,),
row=Station,
)
print(station) # Station(id=1, name='Harbour')
asyncio.run(main())
Reads PGHOST, PGDATABASE, and PGUSER from the environment, like libpq clients.
- Quickstart — pooling, transactions, error handling, and binary
COPY. - Migrating — asyncpg and Psycopg 3 equivalents.
- API reference — public classes and functions.
- Support — bug reports and security disclosures.