postgres 0.13.6

A native PostgreSQL driver
Documentation

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", 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", TlsMode::Require(&negotiator))
        .unwrap();
}