<skill_overview> Configure SQLx safely and consistently across environments
Setting up a database pool Adding or running migrations Configuring database URLs and features Bootstrapping DB on startup
SQLx GitHub
</skill_overview>
Use a single shared pool for the app lifetime Configure max connections based on DB limits Store the pool in app state and reuse it
let pool = sqlx::PgPool::connect(&database_url).await?;
Use DATABASE_URL from environment, never hardcode creds Enable only the database and runtime features you need Fail fast if the database is unreachable at startup
Generate migrations with sqlx migrate add Keep migrations in a dedicated migrations/ directory Apply migrations via sqlx migrate run or migrate! at startup In production, apply migrations in a controlled step
sqlx::migrate!("./migrations").run(&pool).await?;
Use separate databases for dev, test, and prod Never run destructive migrations against prod by default