Crates.io | pgwire |
lib.rs | pgwire |
version | 0.28.0 |
source | src |
created_at | 2022-08-23 02:50:00.799569 |
updated_at | 2024-12-07 05:56:31.395379 |
description | Postgresql wire protocol implemented as a library |
homepage | https://github.com/sunng87/pgwire |
repository | https://github.com/sunng87/pgwire |
max_upload_size | |
id | 650714 |
size | 379,322 |
Build Postgres compatible access layer for your data service.
This library implements PostgreSQL Wire Protocol, and provide essential APIs to write PostgreSQL compatible servers and clients. If you are interested in related topic, you can check project ideas to build on top of this library.
server-api-scram-ring
or
server-api-scram-aws-lc-rs
)
postgres-types
Postgres Wire Protocol is a relatively general-purpose Layer-7 protocol. There are 6 parts of the protocol:
Also note that Postgres Wire Protocol has no semantics about SQL, so literally you can use any query language, data formats or even natural language to interact with the backend.
The response are always encoded as data row format. And there is a field description as header of the data to describe its name, type and format.
Jelte Fennema-Nio's on talk on PgConf.dev 2024 has a great coverage of how the wire protocol works: https://www.youtube.com/watch?v=nh62VgNj6hY
To use pgwire
in your server application, you will need to implement two key
components: startup processor and query processor. For query
processing, there are two kinds of queries: simple and extended. By adding
SimpleQueryHandler
to your application, you will get psql
command-line tool
compatibility. And for more language drivers and additional prepared statement,
binary encoding support, ExtendedQueryHandler
is required.
Examples are provided to demo the very basic usage of pgwire
on server side:
examples/sqlite.rs
: uses an in-memory sqlite database at its core and serves
it with postgresql protocol. This is a full example with both simple and
extended query implementation. cargo run --features _sqlite_ --example sqlite
examples/duckdb.rs
: similar to sqlite example but with duckdb backend. Note
that not all data types are implemented in this example. cargo run --features _duckdb_ --example duckdb
examples/gluesql.rs
: uses an in-memory
gluesql at its core and serves
it with postgresql protocol.examples/server.rs
: demos a server that always returns fixed results.examples/secure_server.rs
: demos a server with ssl support and always
returns fixed results.examples/scram.rs
: demos how to configure more secure authentication
mechanism:
SCRAMexamples/transaction.rs
: see how to control transaction state at wire
protocol level.examples/datafusion.rs
: Now moved to
datafusion-postgresI think in most case you do not need pgwire to build a postgresql client, existing postgresql client like rust-postgres should fit your scenarios. Please rise an issue if there is a scenario.
This library is released under MIT/Apache dual license.