Struct sqlx_core::postgres::PgConnectOptions[][src]

pub struct PgConnectOptions { /* fields omitted */ }
Expand description

Options and flags which can be used to configure a PostgreSQL connection.

A value of PgConnectOptions can be parsed from a connection URI, as described by libpq.

The general form for a connection URI is:

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

Parameters

ParameterDefaultDescription
sslmodepreferDetermines whether or with what priority a secure SSL TCP/IP connection will be negotiated. See PgSslMode.
sslrootcertNoneSets the name of a file containing a list of trusted SSL Certificate Authorities.
statement-cache-capacity100The maximum number of prepared statements stored in the cache. Set to 0 to disable.
hostNonePath to the directory containing a PostgreSQL unix domain socket, which will be used instead of TCP if set.
hostaddrNoneSame as host, but only accepts IP addresses.
application-nameNoneThe name will be displayed in the pg_stat_activity view and included in CSV log entries.
userresult of whoamiPostgreSQL user name to connect as.
passwordNonePassword to be used if the server demands password authentication.
port5432Port number to connect to at the server host, or socket file name extension for Unix-domain connections.
dbnameNoneThe database name.

The URI scheme designator can be either postgresql:// or postgres://. Each of the URI parts is optional.

postgresql://
postgresql://localhost
postgresql://localhost:5433
postgresql://localhost/mydb
postgresql://user@localhost
postgresql://user:secret@localhost
postgresql://localhost?dbname=mydb&user=postgres&password=postgres

Example

// URI connection string
let conn = PgConnection::connect("postgres://localhost/mydb").await?;

// Manually-constructed options
let conn = PgConnectOptions::new()
    .host("secret-host")
    .port(2525)
    .username("secret-user")
    .password("secret-password")
    .ssl_mode(PgSslMode::Require)
    .connect().await?;

Implementations

Creates a new, default set of options ready for configuration.

By default, this reads the following environment variables and sets their equivalent options.

  • PGHOST
  • PGPORT
  • PGUSER
  • PGPASSWORD
  • PGDATABASE
  • PGSSLROOTCERT
  • PGSSLMODE
  • PGAPPNAME

Example

let options = PgConnectOptions::new();

Sets the name of the host to connect to.

If a host name begins with a slash, it specifies Unix-domain communication rather than TCP/IP communication; the value is the name of the directory in which the socket file is stored.

The default behavior when host is not specified, or is empty, is to connect to a Unix-domain socket

Example

let options = PgConnectOptions::new()
    .host("localhost");

Sets the port to connect to at the server host.

The default port for PostgreSQL is 5432.

Example

let options = PgConnectOptions::new()
    .port(5432);

Sets a custom path to a directory containing a unix domain socket, switching the connection method from TCP to the corresponding socket.

By default set to None.

Sets the username to connect as.

Defaults to be the same as the operating system name of the user running the application.

Example

let options = PgConnectOptions::new()
    .username("postgres");

Sets the password to use if the server demands password authentication.

Example

let options = PgConnectOptions::new()
    .username("root")
    .password("safe-and-secure");

Sets the database name. Defaults to be the same as the user name.

Example

let options = PgConnectOptions::new()
    .database("postgres");

Sets whether or with what priority a secure SSL TCP/IP connection will be negotiated with the server.

By default, the SSL mode is Prefer, and the client will first attempt an SSL connection but fallback to a non-SSL connection on failure.

Ignored for Unix domain socket communication.

Example

let options = PgConnectOptions::new()
    .ssl_mode(PgSslMode::Require);

Sets the name of a file containing SSL certificate authority (CA) certificate(s). If the file exists, the server’s certificate will be verified to be signed by one of these authorities.

Example

let options = PgConnectOptions::new()
    // Providing a CA certificate with less than VerifyCa is pointless
    .ssl_mode(PgSslMode::VerifyCa)
    .ssl_root_cert("./ca-certificate.crt");

Sets PEM encoded trusted SSL Certificate Authorities (CA).

Example

let options = PgConnectOptions::new()
    // Providing a CA certificate with less than VerifyCa is pointless
    .ssl_mode(PgSslMode::VerifyCa)
    .ssl_root_cert_from_pem(vec![]);

Sets the capacity of the connection’s statement cache in a number of stored distinct statements. Caching is handled using LRU, meaning when the amount of queries hits the defined limit, the oldest statement will get dropped.

The default cache capacity is 100 statements.

Sets the application name. Defaults to None

Example

let options = PgConnectOptions::new()
    .application_name("my-app");

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Establish a new database connection with the options specified by self.

Log executed statements with the specified level

Log executed statements with a duration above the specified duration at the specified level. Read more

Entirely disables statement logging (both slow and regular).

Formats the value using the given formatter. Read more

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

Performs the conversion.

The associated error which can be returned from parsing.

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Converts self into T using Into<T>. Read more

Converts self into a target type. Read more

Causes self to use its Binary implementation when Debug-formatted.

Causes self to use its Display implementation when Debug-formatted. Read more

Causes self to use its LowerExp implementation when Debug-formatted. Read more

Causes self to use its LowerHex implementation when Debug-formatted. Read more

Causes self to use its Octal implementation when Debug-formatted.

Causes self to use its Pointer implementation when Debug-formatted. Read more

Causes self to use its UpperExp implementation when Debug-formatted. Read more

Causes self to use its UpperHex implementation when Debug-formatted. Read more

Performs the conversion.

Performs the conversion.

Pipes by value. This is generally the method you want to use. Read more

Borrows self and passes that borrow into the pipe function. Read more

Mutably borrows self and passes that borrow into the pipe function. Read more

Borrows self, then passes self.borrow() into the pipe function. Read more

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more

Borrows self, then passes self.as_ref() into the pipe function.

Mutably borrows self, then passes self.as_mut() into the pipe function. Read more

Borrows self, then passes self.deref() into the pipe function.

Mutably borrows self, then passes self.deref_mut() into the pipe function. Read more

Pipes a value into a function that cannot ordinarily be called in suffix position. Read more

Pipes a trait borrow into a function that cannot normally be called in suffix position. Read more

Pipes a trait mutable borrow into a function that cannot normally be called in suffix position. Read more

Pipes a trait borrow into a function that cannot normally be called in suffix position. Read more

Pipes a trait mutable borrow into a function that cannot normally be called in suffix position. Read more

Pipes a dereference into a function that cannot normally be called in suffix position. Read more

Pipes a mutable dereference into a function that cannot normally be called in suffix position. Read more

Pipes a reference into a function that cannot ordinarily be called in suffix position. Read more

Pipes a mutable reference into a function that cannot ordinarily be called in suffix position. Read more

Should always be Self

Immutable access to a value. Read more

Mutable access to a value. Read more

Immutable access to the Borrow<B> of a value. Read more

Mutable access to the BorrowMut<B> of a value. Read more

Immutable access to the AsRef<R> view of a value. Read more

Mutable access to the AsMut<R> view of a value. Read more

Immutable access to the Deref::Target of a value. Read more

Mutable access to the Deref::Target of a value. Read more

Calls .tap() only in debug builds, and is erased in release builds.

Calls .tap_mut() only in debug builds, and is erased in release builds. Read more

Calls .tap_borrow() only in debug builds, and is erased in release builds. Read more

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds. Read more

Calls .tap_ref() only in debug builds, and is erased in release builds. Read more

Calls .tap_ref_mut() only in debug builds, and is erased in release builds. Read more

Calls .tap_deref() only in debug builds, and is erased in release builds. Read more

Calls .tap_deref_mut() only in debug builds, and is erased in release builds. Read more

Provides immutable access for inspection. Read more

Calls tap in debug builds, and does nothing in release builds.

Provides mutable access for modification. Read more

Calls tap_mut in debug builds, and does nothing in release builds.

Provides immutable access to the reference for inspection.

Calls tap_ref in debug builds, and does nothing in release builds.

Provides mutable access to the reference for modification.

Calls tap_ref_mut in debug builds, and does nothing in release builds.

Provides immutable access to the borrow for inspection. Read more

Calls tap_borrow in debug builds, and does nothing in release builds.

Provides mutable access to the borrow for modification.

Calls tap_borrow_mut in debug builds, and does nothing in release builds. Read more

Immutably dereferences self for inspection.

Calls tap_deref in debug builds, and does nothing in release builds.

Mutably dereferences self for modification.

Calls tap_deref_mut in debug builds, and does nothing in release builds. Read more

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

Attempts to convert self into T using TryInto<T>. Read more

Attempts to convert self into a target type. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.