tokio-postgres

A native, asynchronous PostgreSQL client for Rust built on Tokio

Library
Cargo
v0.7.18
4,021 stars
Apache License 2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
72 /100 Good
Development Activity 52
Maintenance 56
Community 80
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
80 /100 Excellent
Architecture 84
Code Quality 85
Innovation 80
Learning Curve 70

tokio-postgres is the asynchronous PostgreSQL driver in the rust-postgres workspace, providing a native, pipelined client built directly on the Tokio async runtime. It underpins most async Rust database stacks — including SQLx-adjacent tooling and connection pools like deadpool-postgres and bb8 — by handling the wire protocol, authentication, prepared statements, and streaming query results without going through libpq.

Because requests are pipelined rather than sent one at a time, the client can queue multiple queries and let the server process them concurrently, which matters for high-throughput services. It supports COPY in/out streaming, LISTEN/NOTIFY, binary and simple query protocols, and pluggable TLS via separate postgres-native-tls or postgres-openssl crates, keeping the core crate free of a hard-coded TLS dependency.

What You Get

  • Fully asynchronous, pipelined query execution built on Tokio’s I/O primitives
  • Prepared statement caching and parameterized queries with compile-time-friendly type conversions via postgres-types
  • Streaming COPY in/out support for bulk data transfer
  • LISTEN/NOTIFY support for Postgres pub/sub notifications
  • Pluggable TLS negotiation through separate native-tls and openssl integration crates
  • A Transaction and TransactionBuilder API for isolation-level-aware transactional work

Common Use Cases

  • Building async web services and APIs that talk directly to PostgreSQL without an ORM
  • Powering higher-level connection pool crates such as deadpool-postgres and bb8-postgres
  • Streaming large datasets in or out of Postgres via COPY for ETL and bulk-load jobs
  • Implementing pub/sub or cache-invalidation patterns using LISTEN/NOTIFY

Under The Hood

Architecture - The crate splits responsibilities across a small set of focused modules: connection.rs drives the actual socket I/O loop and message dispatch, client.rs exposes the public Client API (query, prepare, transaction, copy_in/out) that sends requests over an internal mpsc channel to the connection task, and codec.rs frames raw bytes into postgres-protocol backend/frontend messages. Wire-level encoding/decoding and type conversion are deliberately factored out into the sibling postgres-protocol and postgres-types crates within the same workspace, so tokio-postgres itself focuses purely on async orchestration — pipelining requests, matching responses back to callers via RequestMessages, and managing connection lifecycle (connect.rs, connect_raw.rs, connect_tls.rs).

Tech Stack - Built on Tokio’s AsyncRead/AsyncWrite traits (runtime-agnostic beyond that), using futures-channel/futures-util for the internal request/response plumbing, bytes for zero-copy buffer handling, fallible-iterator for streaming rows, and parking_lot::Mutex for lightweight internal locking. TLS is not baked in; instead MakeTlsConnect/TlsConnect traits are implemented by separate crates (postgres-native-tls, postgres-openssl), keeping the core dependency tree minimal. The crate targets Rust 2024 edition with MSRV 1.85 and even supports a js/wasm32 feature path for browser/WASI targets.

Code Quality - The workspace has a dedicated test/ directory plus per-crate tests/ (e.g. tokio-postgres/tests/test/) exercising connection, query, COPY, transaction, and type-conversion behavior against a real Postgres instance via docker-compose, rather than relying purely on mocks. Error handling flows through a dedicated error module (error/mod.rs, error/sqlstate.rs) mapping Postgres SQLSTATE codes to typed Error variants instead of stringly-typed errors, and unwrap() usage in the library source is minimal (under 20 call sites across ~6,700 lines), concentrated in non-panicking paths. Naming is consistent with the sync postgres crate, which shares most of its API surface.

API Design - The client API is intentionally close to the synchronous postgres crate’s shape (query, query_one, execute, prepare, transaction), which lowers the learning curve for anyone coming from either side. Getting started requires spawning the returned Connection future alongside using the Client, a slightly unusual two-piece handshake that’s well documented in the crate-level example but is the one non-obvious wrinkle in an otherwise ergonomic, strongly-typed API.

Used by 17 apps in this directory

Java
93%
Apache 2.0

QuestDB

Analytics · Databases

17,466

A high-performance, open-source time-series database built for financial market data, IoT telemetry, and real-time analytics, combining a zero-GC Java/C++ core with SIMD-accelerated SQL and a WAL-to-Parquet storage engine.

View details
91
Repo Health
86
Technical
67
Dependency
Built with
Java 93%
Updated 5 days ago
Java
93%
Apache 2.0

QuestDB

Analytics · Databases

17,466

A high-performance, open-source time-series database built for financial market data, IoT telemetry, and real-time analytics, combining a zero-GC Java/C++ core with SIMD-accelerated SQL and a WAL-to-Parquet storage engine.

View details
91
Repo Health
86
Technical
67
Dependency
Built with
Java 93%
Updated 5 days ago
Java
93%
Apache 2.0

QuestDB

Analytics · Databases

17,466

A high-performance, open-source time-series database built for financial market data, IoT telemetry, and real-time analytics, combining a zero-GC Java/C++ core with SIMD-accelerated SQL and a WAL-to-Parquet storage engine.

View details
91
Repo Health
86
Technical
67
Dependency
Built with
Java 93%
Updated 5 days ago
Rust
98%

Stalwart

Collaboration

15,268

All-in-one secure mail and collaboration server covering IMAP, JMAP, SMTP, CalDAV, CardDAV, and WebDAV in a single memory-safe Rust binary.

View details
88
Repo Health
81
Technical
65
Dependency
Built with
Rust 98%
Updated 1 weeks ago
TypeScript
67%
Apache 2.0

tabularis

Databases · Developer Tools

5,270

Open-source desktop SQL workspace with built-in PostgreSQL, MySQL, and SQLite drivers, SQL notebooks, a visual query builder, and a built-in MCP server for AI agents like Claude and Cursor.

View details
84
Repo Health
89
Technical
66
Dependency
Built with
TypeScript 67%
Rust 31%
Updated 5 days ago
Rust
66%
Apache 2.0

Temps

Analytics · Devops · Monitoring

865

A self-hosted Rust PaaS that replaces Vercel, Sentry, PostHog, Pingdom, Resend, and E2B with one binary — plus 440+ CLI operations agents like Claude Code can drive directly.

View details
81
Repo Health
86
Technical
66
Dependency
Built with
Rust 66%
TypeScript 33%
Updated 6 days ago
Rust
71%
MIT

Trieve

AI Development · Developer Tools · Search

2,721

All-in-one self-hostable platform for hybrid search, RAG, recommendations, and analytics built on Rust and Qdrant.

View details
42
Repo Health
74
Technical
65
Dependency
Built with
Rust 71%
Updated 9 months ago
Rust
71%
MIT

Trieve

AI Development · Developer Tools · Search

2,721

All-in-one self-hostable platform for hybrid search, RAG, recommendations, and analytics built on Rust and Qdrant.

View details
42
Repo Health
74
Technical
65
Dependency
Built with
Rust 71%
Updated 9 months ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers