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 postgres::Client is simply a wrapper around a
tokio_postgres::Client along side a tokio Runtime. The client simply blocks on the futures provided by the async
client.
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
postgres-openssl and postgres-native-tls crates provide implementations backed by the openssl and native-tls
crates, respectively.