A connection pool maintains a set of open database connections and lends them out to your application as needed. Rather than opening and closing a connection for every query — which involves DNS resolution, TCP handshakes, authentication, and query plan cache warm-up — a pool reuses existing connections, amortizing those costs across many operations. SQLx ships with a production-ready async pool viaDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/launchbadge/sqlx/llms.txt
Use this file to discover all available pages before exploring further.
sqlx::Pool.
Why use a pool?
Opening a new database connection is expensive. For server-based databases like PostgreSQL and MySQL, the server typically spawns a new thread or process for each connection and enforces a hard connection limit (100 for Postgres by default, 150 for MySQL). Exceeding this limit causes errors for new connections. A pool solves both problems: it reuses connections to avoid per-connection overhead, and it queuesacquire() calls in a fair, first-come-first-serve order when all connections are in use — preventing your application from exceeding the server’s connection limit under load.
Creating a pool
The simplest way to create a pool isPool::connect(), which opens one connection immediately to verify the configuration:
PoolOptions:
acquire() call, use connect_lazy():
Per-database pool type aliases
SQLx provides type aliases so you can use a concrete type rather thanPool<Postgres> throughout your codebase:
PgPool
sqlx::postgres::PgPool — PostgreSQLMySqlPool
sqlx::mysql::MySqlPool — MySQL / MariaDBSqlitePool
sqlx::sqlite::SqlitePool — SQLiteAnyPool
sqlx::any::AnyPool — runtime-selected driverPool<DB> and share the same API.
Using a pool
Pool implements Executor, so you can pass &pool directly to any query finalizer. SQLx automatically acquires a connection from the pool, runs your query, and returns the connection when the future resolves:
Pool is Clone, Send, and Sync. Create it once and share it across your application — clone it into request handlers, pass it to spawned tasks, or store it in application state:
Configuring PoolOptions
PoolOptions exposes a builder API for tuning pool behavior. The default configuration is suitable for development and light workloads; production applications will want to adjust at least max_connections.
max_connections
max_connections
The maximum number of connections the pool will hold open at any time. Defaults to Be mindful of your database server’s connection limit and the limits required by other applications connecting to the same server.
10.min_connections
min_connections
The minimum number of connections the pool tries to keep open at all times. Defaults to
0 (connections are opened on demand).Setting this to a nonzero value pre-warms the pool at startup so that the first requests do not incur connection-open latency:acquire_timeout
acquire_timeout
The maximum time to wait for a connection from the pool before returning
Error::PoolTimedOut. Defaults to 30 seconds.idle_timeout
idle_timeout
Connections that have been idle for longer than this duration are closed and removed from the pool. Defaults to 10 minutes. Set to
None to disable.max_lifetime
max_lifetime
The maximum age of any individual connection. Connections older than this are closed and replaced. Defaults to 30 minutes. Set to
None to allow unlimited lifetimes.Retiring connections periodically lets the database server clean up session-level resources (parse trees, query caches, thread-local storage):test_before_acquire
test_before_acquire
When
true (the default), each idle connection is verified with a ping before being returned to a caller. Disable if you are managing liveness checks yourself with before_acquire:Lifecycle hooks
PoolOptions provides three async callback hooks for customizing connection behavior.
after_connect
Called once when a new connection is first opened. Use it to set session-level parameters that are not available through ConnectOptions:
before_acquire
Called before an idle connection is returned from the pool. Return Ok(true) to accept the connection or Ok(false) to discard it and try another. Useful for custom liveness checks:
after_release
Called when a connection is returned to the pool. Return Ok(true) to put it back in the idle queue or Ok(false) to close it. Useful for releasing memory-heavy connections:
Graceful shutdown
When your application exits, callpool.close().await to gracefully close all connections. Without this, the server side may not learn that connections are gone until a TCP keepalive timeout fires:
AnyPool for runtime database selection
AnyPool uses the URL scheme to select a driver at runtime, letting you write database-agnostic code or switch drivers via configuration:
You must call
sqlx::any::install_default_drivers() before using AnyPool to register the available drivers.