pub struct Config { /* private fields */ }Expand description
Connection settings: where the server is, who to log in as, and the limits that apply to the connection.
Build one with Config::from_url or with the setters:
use std::time::Duration;
use giong_postgres::Config;
let config = Config::from_url("postgres://app:secret@db.internal:5432/shop?sslmode=verify-full")?;
let same_place = Config::new()
.host("db.internal")
.user("app")
.password("secret")
.dbname("shop")
.statement_timeout(Duration::from_secs(5));Every limit has a finite default, documented on its setter: a connection never waits forever, and a result never grows without bound.
TLS. Unless tls or sslmode says otherwise, a TCP
connection to anything but a loopback address uses TLS and checks the
server’s certificate against the certificate authorities installed on the
machine; connections over a Unix socket or to a loopback address do not
use TLS.
Implementations§
Source§impl Config
impl Config
Sourcepub fn host(self, host: &str) -> Self
pub fn host(self, host: &str) -> Self
The server’s host: a DNS name, an IP address, or the absolute path of
the directory holding its Unix socket (such as
/var/run/postgresql).
Sourcepub fn port(self, port: u16) -> Self
pub fn port(self, port: u16) -> Self
The server’s port, 5432 by default. For a Unix socket it picks the
socket file, .s.PGSQL.<port>.
Sourcepub fn dbname(self, dbname: &str) -> Self
pub fn dbname(self, dbname: &str) -> Self
The database; the server uses the user’s name when it is not set.
Sourcepub fn application_name(self, name: &str) -> Self
pub fn application_name(self, name: &str) -> Self
The name the server shows for this connection in pg_stat_activity
and in its logs.
Sourcepub fn param(self, name: &str, value: &str) -> Self
pub fn param(self, name: &str, value: &str) -> Self
Sets a server setting for the whole session when connecting, such as
search_path or TimeZone.
Sourcepub fn tls(self, tls: Tls) -> Self
pub fn tls(self, tls: Tls) -> Self
Whether and how to use TLS (see the type’s documentation for what happens when this is not set).
Sourcepub fn require_auth(self, methods: &[AuthMethod]) -> Self
pub fn require_auth(self, methods: &[AuthMethod]) -> Self
The only authentication methods the server may use; by default, any
this crate supports. Connecting fails with
ErrorKind::Auth, before anything is sent
for the password, when the server asks for another method or lets the
connection in without one.
Without TLS (over a Unix socket, to a loopback address, or with
sslmode=disable), nothing proves that the server is the real one: an
impostor can ask for MD5 instead of SCRAM and attack the answer
offline, or let the client in without asking anything.
require_auth(&[AuthMethod::ScramSha256]) rules that out, since SCRAM
makes the server prove that it knows the password too. In a URL:
require_auth=scram-sha-256 (a comma-separated list, as in libpq,
without libpq’s ! negations).
Sourcepub fn connect_timeout(self, timeout: Duration) -> Self
pub fn connect_timeout(self, timeout: Duration) -> Self
The longest that opening a connection may take, from name resolution to the end of authentication: 10 seconds by default.
Sourcepub fn statement_timeout(self, timeout: impl Into<Option<Duration>>) -> Self
pub fn statement_timeout(self, timeout: impl Into<Option<Duration>>) -> Self
The longest a statement may run on the server before the server cancels it: 30 seconds by default.
It is sent as the session’s statement_timeout when connecting, so the
server enforces it exactly, and only on the statement that runs too
long (SQLSTATE 57014). It is rounded up to whole milliseconds and
must be from 1 ms to i32::MAX milliseconds (24.8 days): connecting
fails with ErrorKind::Config otherwise.
Raise it for the connections that run migrations or reports, rather
than turning it off with SET.
None sends nothing, leaving the limit to the server’s own setting,
which is no limit unless someone set one. Through a connection pooler
such as PgBouncer, give None and set the limit on the role
(ALTER ROLE app SET statement_timeout = '30s'): PgBouncer refuses a
connection that sends statement_timeout, and its settings that
accept it (track_extra_parameters, ignore_startup_parameters)
do not pass it on to the server (checked with PgBouncer 1.22).
Sourcepub fn max_result_size(self, bytes: usize) -> Self
pub fn max_result_size(self, bytes: usize) -> Self
The largest result one statement may return, in bytes of row data:
64 MiB by default. A larger result fails with
ErrorKind::TooLarge, and the
connection stays usable.
Sourcepub fn statement_cache_capacity(self, statements: usize) -> Self
pub fn statement_cache_capacity(self, statements: usize) -> Self
How many prepared statements each connection keeps, 256 by default.
Every statement is prepared on the server the first time a connection
runs it, and kept, so running it again takes one round trip instead
of two. The least recently used statement is closed when the cache is
full. 0 prepares and closes every statement each time it runs, which
takes two round trips; the statement is still a named one, prepared
in one request and run in the next, so 0 does not make a connection
pooler in transaction mode work: that needs PgBouncer 1.21 or newer
with max_prepared_statements above 0, which supports any cache
size.
Sourcepub fn from_url(url: &str) -> Result<Self, Error>
pub fn from_url(url: &str) -> Result<Self, Error>
Reads a connection URL:
postgres://user:password@host:port/dbname?param=value&...postgresql:// works too. User, password and database are
percent-decoded. The host can be an IPv6 address in brackets, or, as
with libpq, empty with the socket directory in a host parameter:
postgres:///shop?host=/var/run/postgresql.
The parameters understood are sslmode, sslrootcert, host,
port, user, password, dbname, application_name,
connect_timeout (seconds) and require_auth (see
require_auth); any other is an error rather
than being ignored. sslmode is disable, or require, verify-ca or
verify-full, which all check the certificate and the host name
(stricter than libpq, which checks nothing for require).
sslrootcert is a PEM file of authorities to trust instead of the
machine’s, or system.