pub struct Connection { /* private fields */ }Expand description
A live connection to one PostgreSQL server.
Connection is a cheap, cloneable handle to a background actor that owns
the socket. Cloning shares the same connection. All clones must be dropped
for the connection to close.
Queries are queued and executed one at a time. The queue is unbounded, so a
connection is best treated as “one in-flight query at a time”: spawning
many queries against a single connection without awaiting them queues their
encoded parameters in memory. Use a Pool for concurrency.
Implementations§
Source§impl Connection
impl Connection
Sourcepub async fn connect(url: &str) -> Result<Self, Error>
pub async fn connect(url: &str) -> Result<Self, Error>
Connects using a postgres:// URL.
§TLS
TLS is always required; plaintext connections are not possible.
sslmode | Chain verified | Hostname verified |
|---|---|---|
require (default) | no | no |
verify-ca | yes | no |
verify-full | yes | yes |
disable, prefer and allow are rejected. verify-ca and
verify-full read trusted roots from sslrootcert (a PEM file) or the
operating system trust store.
use postgresql::Connection;
// Encrypted; the server certificate is not verified (default).
let conn = Connection::connect("postgres://user:pass@db.example.com/app").await?;
// Verify the certificate chain and the hostname.
let verified = Connection::connect(
"postgres://user:pass@db.example.com/app?sslmode=verify-full&sslrootcert=/etc/ssl/db-ca.pem",
)
.await?;§Errors
Returns a configuration error for an invalid URL, or an I/O, TLS, authentication or protocol error if the handshake fails.
Sourcepub fn is_closed(&self) -> bool
pub fn is_closed(&self) -> bool
Returns true if the connection has been closed or hit an I/O error.
Sourcepub fn notifications(&self) -> Receiver<Notification>
pub fn notifications(&self) -> Receiver<Notification>
Subscribes to LISTEN / NOTIFY notifications.
The returned receiver yields notifications delivered on this connection
(after LISTEN). Multiple subscribers are supported.
Sourcepub fn cancel_token(&self) -> CancelToken
pub fn cancel_token(&self) -> CancelToken
Returns a token that can cancel the currently running query.