5 unstable releases

0.2.1 Oct 6, 2024
0.2.0 Sep 28, 2024
0.1.1 Sep 8, 2024
0.1.0 Sep 7, 2024
0.0.9 Sep 5, 2024

#1906 in Database interfaces

Download history 351/week @ 2024-09-03 54/week @ 2024-09-10 24/week @ 2024-09-17 106/week @ 2024-09-24 136/week @ 2024-10-01 44/week @ 2024-10-08 2/week @ 2024-10-15

202 downloads per month

MIT license

7KB

crates.io Documentation

tokio-postgres is a database connection tool similar to sqlx. Unlike sqlx, it only focuses on implementing postgresql database connections.

Dependencies

spring-postgres = { version = "0.1.1" }

Configuration items

[postgres]
connect = "postgres://root:12341234@localhost:5432/myapp_development" # Database address to connect to

Components

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>);

Extract the Component registered by the plugin

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

Dependencies

~15–27MB
~394K SLoC