6 releases (3 breaking)

0.3.0 Dec 1, 2024
0.2.1 Oct 6, 2024
0.2.0 Sep 28, 2024
0.1.1 Sep 8, 2024
0.0.9 Sep 5, 2024

#2456 in Database interfaces

Download history 205/week @ 2024-09-01 173/week @ 2024-09-08 47/week @ 2024-09-15 75/week @ 2024-09-22 63/week @ 2024-09-29 148/week @ 2024-10-06 6/week @ 2024-10-13 172/week @ 2024-12-01 11/week @ 2024-12-08 1/week @ 2024-12-15

184 downloads per month

MIT license

69KB
1.5K SLoC

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 = "<version>" }

optional features:

  • array-impls
  • js
  • with-bit-vec-0_6
  • with-chrono-0_4
  • with-eui48-0_4
  • with-eui48-1
  • with-geo-types-0_6
  • with-geo-types-0_7
  • with-serde_json-1
  • with-smol_str-01
  • with-time-0_2
  • with-time-0_3
  • with-uuid-0_8
  • with-uuid-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–26MB
~395K SLoC