Source: https://gres.monicz.dev/

![Gres](https://gres.monicz.dev/assets/wordmark.9113baa4aaedc6b4.svg)

# 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](https://gres.monicz.dev/benchmarks/).

[Get started](https://gres.monicz.dev/installation/)
[User guide](https://gres.monicz.dev/guide/)

- **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.

  [Queries and transactions](https://gres.monicz.dev/guide/queries/)
- **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.

  [Types and adapters](https://gres.monicz.dev/guide/types/)
- **Arrow and COPY**

  ---

  Results stream as [Apache Arrow](https://arrow.apache.org/) record
  batches. Binary `COPY` loads tables from Python rows or Arrow data.
  Memory stays bounded in both directions.

  [Streaming, Arrow and COPY](https://gres.monicz.dev/guide/bulk/)
- **PostGIS**

  ---

  `geometry` and `geography` values arrive as
  [gometry](https://gometry.monicz.dev/) objects carrying the database's
  CRS, as [GeoArrow](https://geoarrow.org/) columns, or as exact EWKB.

  [PostGIS](https://gres.monicz.dev/guide/spatial/)
- **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.

  [Connections](https://gres.monicz.dev/guide/connections/)
- **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.

  [Errors and retries](https://gres.monicz.dev/guide/errors/) ·
  [Pools and PgBouncer](https://gres.monicz.dev/guide/pools/)

## A first query

```python
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](https://gres.monicz.dev/quickstart/) — pooling, transactions, error handling, and binary `COPY`.
- [Migrating](https://gres.monicz.dev/migrating/) — asyncpg and Psycopg 3 equivalents.
- [API reference](https://gres.monicz.dev/api/) — public classes and functions.
- [Support](https://gres.monicz.dev/about/support/) — bug reports and security disclosures.
