Skip to main content

Config

Struct Config 

Source
#[non_exhaustive]
pub struct Config { /* private fields */ }
Expand description

Configuration for a PostgreSQL connection.

Use Config::new() to create a default configuration and then set fields using the builder methods, or parse from a connection string with Config::from_uri or Config::from_key_value.

Implementations§

Source§

impl Config

Source

pub fn new() -> Self

Creates a new configuration with default values.

Defaults:

  • host: "localhost"
  • port: 5432
  • user: "postgres"
  • password: None
  • database: None
  • ssl_mode: VerifyFull (if tls feature enabled) / Disable (otherwise)
  • connect_timeout: None
  • target_session_attrs: Any
Source

pub fn host(self, host: impl Into<String>) -> Self

Sets the hostname or IP address.

Source

pub fn port(self, port: u16) -> Self

Sets the port number.

Source

pub fn user(self, user: impl Into<String>) -> Self

Sets the username.

Source

pub fn password(self, password: impl Into<String>) -> Self

Sets the password.

Source

pub fn database(self, database: impl Into<String>) -> Self

Sets the database name.

Source

pub fn application_name(self, name: impl Into<String>) -> Self

Sets the application name.

Source

pub fn ssl_mode(self, mode: SslMode) -> Self

Sets the SSL mode.

Source

pub fn connect_timeout(self, timeout: Duration) -> Self

Sets the connection timeout.

Source

pub fn statement_timeout(self, timeout: Duration) -> Self

Sets the statement timeout (sent as a startup parameter).

Source

pub fn target_session_attrs(self, attrs: TargetSessionAttrs) -> Self

Sets the target session attributes.

Source

pub fn option(self, key: impl Into<String>, value: impl Into<String>) -> Self

Adds an extra startup option.

Source

pub fn use_tls(self, use_tls: bool) -> Self

Sets whether to use TLS (legacy alias for ssl_mode).

Source

pub fn accept_invalid_certs(self, accept: bool) -> Self

Accept invalid/self-signed TLS certificates. WARNING: Only for development/testing. Never use in production.

Source

pub fn allow_insecure_auth(self, allow: bool) -> Self

Allow legacy password authentication over plaintext transports.

This enables cleartext-password and MD5 authentication without TLS. WARNING: Only use this in development or tightly controlled environments.

Source

pub fn keepalive(self, keepalive: Duration) -> Self

Sets the TCP keepalive interval.

Source

pub fn reconnect(self, config: ReconnectConfig) -> Self

Sets the reconnection policy.

Source

pub fn enable_reconnect(self) -> Self

Enable automatic reconnection with default settings.

Source

pub fn max_reconnect_attempts(self, n: u32) -> Self

Set the maximum number of reconnection attempts.

Source

pub fn stale_threshold(self, threshold: Duration) -> Self

Set the stale connection detection threshold.

Source

pub fn stale(self, config: StaleConfig) -> Self

Sets the stale connection detection configuration.

Source

pub fn get_host(&self) -> &str

Source

pub fn get_port(&self) -> u16

Source

pub fn get_user(&self) -> &str

Source

pub fn get_password(&self) -> Option<&str>

Source

pub fn get_database(&self) -> Option<&str>

Source

pub fn get_application_name(&self) -> Option<&str>

Source

pub fn get_ssl_mode(&self) -> SslMode

Source

pub fn get_connect_timeout(&self) -> Option<Duration>

Source

pub fn get_statement_timeout(&self) -> Option<Duration>

Source

pub fn get_target_session_attrs(&self) -> TargetSessionAttrs

Source

pub fn get_use_tls(&self) -> bool

Source

pub fn get_accept_invalid_certs(&self) -> bool

Source

pub fn get_allow_insecure_auth(&self) -> bool

Source

pub fn get_keepalive(&self) -> Option<Duration>

Source

pub fn get_reconnect(&self) -> &ReconnectConfig

Source

pub fn get_stale(&self) -> &StaleConfig

Source

pub fn startup_params(&self) -> Vec<(String, String)>

Returns the startup parameters to send in the StartupMessage.

Source

pub fn from_uri(uri: &str) -> Result<Self, ConfigError>

Parse a PostgreSQL connection URI.

Supported format:

postgresql://[user[:password]@][host][:port][/dbname][?param1=value1&...]
Source

pub fn from_key_value(s: &str) -> Result<Self, ConfigError>

Parse a key-value connection string.

Format:

host=localhost port=5432 dbname=mydb user=myuser password=secret sslmode=require

Values containing spaces may be wrapped in single quotes:

host='my host' user=postgres
Source

pub fn from_env() -> Result<Self, ConfigError>

Build a configuration from standard PostgreSQL environment variables.

Variables: PGHOST, PGPORT, PGDATABASE, PGUSER, PGPASSWORD, PGSSLMODE, PGCONNECT_TIMEOUT, PGAPPNAME.

Trait Implementations§

Source§

impl Clone for Config

Source§

fn clone(&self) -> Config

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

Auto Trait Implementations§

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<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

Source§

type Output = T

Should always be Self
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more