expression
SQLx: create a lazy connection pool
A pool manages reusable database connections. Lazy construction with zero minimum connections defers initial connection creation; it does not prove that the database exists. This fixture closes the empty pool without acquiring a connection. No database connection is required by this example. Expected stdout (not executed): connections: 0 Common mistake: Treating a successfully constructed lazy pool as a readiness check.
Formal Syntax & Structure
rust syntaxrust
PgPoolOptions::new().min_connections(0).connect_lazy_with(options)Working Example
Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() -> Result<(), Box<dyn std::error::Error>> { use sqlx::postgres::{PgConnectOptions, PgPoolOptions}; let options = PgConnectOptions::new_without_pgpass() .host("127.0.0.1").port(55432).database("bm_sqlx_lab"); let pool = PgPoolOptions::new().min_connections(0) .max_connections(2).connect_lazy_with(options); println!("connections: {}", pool.size()); pool.close().await; Ok(())}