Skip to main content

Config

Struct Config 

Source
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

Source

pub fn new() -> Self

Settings for localhost:5432, with no user yet and the default limits.

Source

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).

Source

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>.

Source

pub fn user(self, user: &str) -> Self

The user to log in as. Required.

Source

pub fn password(self, password: &str) -> Self

The password, if the server asks for one.

Source

pub fn dbname(self, dbname: &str) -> Self

The database; the server uses the user’s name when it is not set.

Source

pub fn application_name(self, name: &str) -> Self

The name the server shows for this connection in pg_stat_activity and in its logs.

Source

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.

Source

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).

Source

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).

Source

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.

Source

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).

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for Config

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Config

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Config

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl FromStr for Config

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(url: &str) -> Result<Self, Error>

Parses a string s to return a value of this type. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Config

§

impl !UnwindSafe for Config

§

impl Forget for Config

§

impl Freeze for Config

§

impl Move for Config

§

impl Send for Config

§

impl Sync for Config

§

impl Unpin for Config

§

impl UnsafeUnpin for Config

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.