84 releases

✓ Uses Rust 2018 edition

0.16.0-rc.1 Apr 7, 2019
0.15.2 Feb 12, 2018
0.15.1 Jul 23, 2017
0.14.0 Feb 21, 2017
0.1.2 Nov 28, 2014

#11 in Database interfaces

Download history 1263/week @ 2018-12-23 991/week @ 2018-12-30 1561/week @ 2019-01-06 1501/week @ 2019-01-13 2410/week @ 2019-01-20 2419/week @ 2019-01-27 2997/week @ 2019-02-03 2909/week @ 2019-02-10 2749/week @ 2019-02-17 3808/week @ 2019-02-24 3867/week @ 2019-03-03 3835/week @ 2019-03-10 3591/week @ 2019-03-17 5185/week @ 2019-03-24 3664/week @ 2019-03-31

13,397 downloads per month
Used in 77 crates (64 directly)

MIT/Apache

219KB
4.5K SLoC


lib.rs:

A synchronous client for the PostgreSQL database.

Example

use postgres::{Client, NoTls};

# fn main() -> Result<(), postgres::Error> {
let mut client = Client::connect("host=localhost user=postgres", NoTls)?;

client.simple_query("
    CREATE TABLE person (
        id      SERIAL PRIMARY KEY,
        name    TEXT NOT NULL,
        data    BYTEA
    )
")?;

let name = "Ferris";
let data = None::<&[u8]>;
client.execute(
    "INSERT INTO person (name, data) VALUES ($1, $2)",
    &[&name, &data],
)?;

for row in client.query("SELECT id, name, data FROM person", &[])? {
    let id: i32 = row.get(0);
    let name: &str = row.get(1);
    let data: Option<&[u8]> = row.get(2);

    println!("found person: {} {} {:?}", id, name, data);
}
# Ok(())
# }

Implementation

This crate is a lightweight wrapper over tokio-postgres. The tokio_postgres::Connection is spawned onto an executor, and the tokio_postgres::Client is wrapped in the postgres::Client, which simply waits on the futures the nonblocking client creates.

Runtime

A client can be constructed directly from a tokio-postgres client via a From implementation, but the runtime Cargo feature (enabled by default) provides a more convenient interface. By default, connections will be spawned onto a static tokio Runtime, but a custom Executor can also be used instead.

SSL/TLS support

TLS support is implemented via external libraries. Client::connect and Config::connect take a TLS implementation as an argument. The NoTls type in this crate can be used when TLS is not required. Otherwise, the tokio-postgres-openssl and tokio-postgres-native-tls crates provide implementations backed by the openssl and native-tls crates, respectively.

Dependencies

~4MB
~76K SLoC