[][src]Crate postgres

A synchronous client for the PostgreSQL database.

Example

use postgres::{Client, NoTls};

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);
}

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.

Re-exports

pub use crate::config::Config;
pub use crate::error::Error;
pub use crate::row::Row;
pub use crate::row::SimpleQueryRow;
pub use crate::tls::NoTls;

Modules

config

Connection configuration.

error

Errors.

row

Rows.

tls

TLS support.

types

Types.

Structs

Client

A synchronous PostgreSQL client.

Column

Information about a column of a Postgres query.

CopyOutReader

The reader returned by the copy_out method.

Portal

A portal.

QueryIter

The iterator returned by the query_iter method.

QueryPortalIter

The iterator returned by the query_portal_iter method.

SimpleQueryIter

The iterator returned by the simple_query_iter method.

Statement

A prepared statement.

Transaction

A representation of a PostgreSQL database transaction.

Enums

SimpleQueryMessage

Message returned by the SimpleQuery stream.

Traits

ToStatement

A trait abstracting over prepared and unprepared statements.