| Crates.io | spring-postgres |
| lib.rs | spring-postgres |
| version | 0.4.1 |
| created_at | 2024-09-05 12:10:13.11324+00 |
| updated_at | 2025-07-20 15:17:52.440217+00 |
| description | Integrate tokio-postgres with spring-rs |
| homepage | |
| repository | https://github.com/spring-rs/spring-rs |
| max_upload_size | |
| id | 1364517 |
| size | 60,942 |
tokio-postgres is a database connection tool similar to sqlx. Unlike sqlx, it only focuses on implementing postgresql database connections.
spring-postgres = { version = "<version>" }
optional features:
array-implsjswith-bit-vec-0_6with-chrono-0_4with-eui48-0_4with-eui48-1with-geo-types-0_6with-geo-types-0_7with-serde_json-1with-smol_str-01with-time-0_2with-time-0_3with-uuid-0_8with-uuid-1[postgres]
connect = "postgres://root:12341234@localhost:5432/myapp_development" # Database address to connect to
After configuring the above configuration items, the plugin will automatically register a Postgres object. This object wraps tokio_postgres::Client.
pub struct Postgres(Arc<tokio_postgres::Client>);
The PgPlugin plugin automatically registers a Postgres object for us. We can use Component to extract this connection pool from AppState. Component is an axum extractor.
#[get("/postgres")]
async fn hello_postgres(Component(pg): Component<Postgres>) -> Result<impl IntoResponse> {
let rows = pg
.query("select version() as version", &[])
.await
.context("query postgresql failed")?;
let version: String = rows[0].get("version");
Ok(Json(version))
}
Complete code reference postgres-example