expression

SQLx: configure a PostgreSQL connection

Connection settings identify a PostgreSQL server, database and login. Creating a settings value does not authenticate or contact the server. In a real deployment obtain secrets from approved secret storage and require server identity verification. This example only inspects non-secret settings. No database connection is required by this example. Expected stdout (not executed): database: bm_sqlx_lab Common mistake: Printing a full connection URL or secret-bearing configuration in logs.

Formal Syntax & Structure

rust syntaxrust
PgConnectOptions::new_without_pgpass().host(host).port(port).database(db)

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() -> Result<(), Box<dyn std::error::Error>> {    use sqlx::postgres::{PgConnectOptions, PgSslMode};    let options = PgConnectOptions::new_without_pgpass()        .host("127.0.0.1").port(55432)        .database("bm_sqlx_lab").username("bm_learner")        .ssl_mode(PgSslMode::VerifyFull);    println!("database: {}", options.get_database().unwrap_or("missing"));    Ok(())}
Rust Reference