Crate postgres [] [src]

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};
use postgres::tls::native_tls::NativeTls;

fn main() {
    let negotiator = NativeTls::new().unwrap();
    let conn = Connection::connect("postgres://postgres@localhost:5433", TlsMode::Require(&negotiator))
        .unwrap();
}

Modules

error

Errors.

notification

Asynchronous notifications.

params

Connection parameters

rows

Query result rows.

stmt

Prepared statements

tls

Types and traits for TLS support.

transaction

Transactions

types

Types.

Structs

CancelData

Contains information necessary to cancel queries for a session.

Connection

A connection to a Postgres database.

Error

An error communicating with the Postgres server.

LoggingNoticeHandler

A notice handler which logs at the info level.

Enums

TlsMode

Specifies the TLS support requested for a new connection.

Traits

GenericConnection

A trait allowing abstraction over connections and transactions

HandleNotice

A trait implemented by types that can handle Postgres notice messages.

Functions

cancel_query

Attempts to cancel an in-progress query.

Type Definitions

Result

A type alias of the result returned by many methods.