83 releases

0.15.2 Feb 12, 2018
0.15.1 Jul 23, 2017
0.14.2 Jun 11, 2017
0.14.0 Feb 21, 2017
0.1.2 Nov 28, 2014

#9 in Database interfaces

Download history 968/week @ 2018-05-27 1188/week @ 2018-06-03 1176/week @ 2018-06-10 1627/week @ 2018-06-17 1245/week @ 2018-06-24 2107/week @ 2018-07-01 857/week @ 2018-07-08 1223/week @ 2018-07-15 1178/week @ 2018-07-22 1307/week @ 2018-07-29 1915/week @ 2018-08-05 1412/week @ 2018-08-12 906/week @ 2018-08-19

4,409 downloads per month
Used in 55 crates (46 directly)

MIT license

290KB
6.5K SLoC


lib.rs:

A pure-Rust frontend for the popular PostgreSQL database.

extern crate postgres;

use postgres::{Connection, TlsMode};

struct Person {
    id: i32,
    name: String,
    data: Option<Vec<u8>>
}

fn main() {
    let conn = Connection::connect("postgresql://postgres@localhost:5433", TlsMode::None)
            .unwrap();

    conn.execute("CREATE TABLE person (
                    id              SERIAL PRIMARY KEY,
                    name            VARCHAR NOT NULL,
                    data            BYTEA
                  )", &[]).unwrap();
    let me = Person {
        id: 0,
        name: "Steven".to_owned(),
        data: None
    };
    conn.execute("INSERT INTO person (name, data) VALUES ($1, $2)",
                 &[&me.name, &me.data]).unwrap();

    for row in &conn.query("SELECT id, name, data FROM person", &[]).unwrap() {
        let person = Person {
            id: row.get(0),
            name: row.get(1),
            data: row.get(2)
        };
        println!("Found person {}", person.name);
    }
}

SSL/TLS

This crate supports TLS secured connections. The TlsMode enum is passed to connection methods and indicates if the connection will not, may, or must be secured by TLS. The TLS implementation is pluggable through the TlsHandshake trait. Implementations for OpenSSL, Secure Transport, SChannel, and the native-tls crate are provided behind the with-openssl, with-security-framework, with-schannel, and with-native-tls feature flags respectively.

Examples

Connecting using native-tls:

extern crate postgres;

use postgres::{Connection, TlsMode};
# #[cfg(feature = "with-native-tls")]
use postgres::tls::native_tls::NativeTls;

# #[cfg(not(feature = "with-native-tls"))] fn main() {}
# #[cfg(feature = "with-native-tls")]
fn main() {
    let negotiator = NativeTls::new().unwrap();
    let conn = Connection::connect("postgres://postgres@localhost:5433", TlsMode::Require(&negotiator))
        .unwrap();
}

Dependencies

~7MB
~180K SLoC