Connect to PostgreSQL
besu_postgres::Sqlx wraps a sqlx PgPool. The pool is created with sqlx's defaults — at
most ten connections, opened lazily — and every constructor takes a libpq-style URL.
A URL
Sqlx::new is an alias for Sqlx::connect, so either name works. The URL carries TLS mode
and the application name in its query string, which is how sqlx expects them:
Reading it from the environment is the usual shape, and it keeps the password out of the binary:
Your own pool
from_pool is the only way to change the connection limit, the acquire timeout or the idle
timeout. This needs sqlx as a direct dependency:
The pool underneath
pool() is the escape hatch: closing it at shutdown, or passing it to a library that wants a
PgPool directly. db.driver() returns the Sqlx, so the pool is one call away.
Transactions
begin borrows the database and runs every statement on one connection. db.transaction(..)
commits on Ok and rolls back on Err, and tx.savepoint("name", ..) nests inside an open
transaction. A Transaction is an executor, so the same builders work through tx.
No connection at all
That renders the exact statement the driver would send, including the /* besu:ii */
parameter-shape suffix, with no server and no runtime.
Gotchas
- Every statement with parameters carries a
/* besu:... */comment describing the bound value shapes. PostgreSQL fixes parameter types at prepare time and sqlx caches statements by SQL text, so two calls with the same text but different parameter types would otherwise hit the same cache entry with the wrong types. An assertion on the whole string must include it. pushis not transactional. A failed statement leaves the earlier ones applied; usebesu generateandbesu migratefor production changes.- The default schema is implicit. Introspection reports a table in
current_schema()without its schema, so#[besu(schema = "public")]on a table inpublicmakes the declared table look like a different one and reports drift. - Besu never runs
CREATE SCHEMAorCREATE DATABASE; both have to exist. - A table in another schema has
(schema, name)as its identity, soapp.usersandusersare two tables that can both exist. pushmanages only the schemas the declared schema mentions. A table in some other schema is left alone rather than reported as a drop.- The default connection limit is ten. A service that opens several
Sqlxvalues opens several pools, each with ten connections.
