MCPcopy Index your code
hub / github.com/Hexilee/async-postgres

github.com/Hexilee/async-postgres @async-postgres-v0.5.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release async-postgres-v0.5.1 ↗ · + Follow
17 symbols 32 edges 4 files 3 documented · 18%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

async-postgres

A runtime-independent, asynchronous PostgreSQL client.

Stable Test codecov Rust Docs Crate version Download MSRV-1.40 License: MIT

This crate is a wrapper of tokio-postgres.

Pros

Runtime-independent, can be used on any async runtime.

Usage

Almost the same with tokio-postgres.

  • TCP or UDS
use async_postgres::connect;
use std::error::Error;
use async_std::task::spawn;

async fn play() -> Result<(), Box<dyn Error>> {
    let url = "host=localhost user=postgres";
    let (client, conn) = connect(url.parse()?).await?;
    spawn(conn);
    let row = client.query_one("SELECT * FROM user WHERE id=$1", &[&0]).await?;
    let value: &str = row.get(0);
    println!("value: {}", value);
    Ok(())
}
  • TLS
use async_postgres::connect_tls;
use native_tls::{Certificate, TlsConnector};
use postgres_native_tls::MakeTlsConnector;
use std::fs;
use std::error::Error;
use async_std::task::spawn;

async fn play() -> Result<(), Box<dyn Error>> {
    let cert = fs::read("database_cert.pem")?;
    let cert = Certificate::from_pem(&cert)?;
    let connector = TlsConnector::builder()
        .add_root_certificate(cert)
        .build()?;
    let connector = MakeTlsConnector::new(connector);
    let url = "host=localhost user=postgres sslmode=require";
    let (client, conn) = connect_tls(url.parse()?, connector).await?;
    spawn(conn);
    let row = client.query_one("SELECT * FROM user WHERE id=$1", &[&0]).await?;
    let value: &str = row.get(0);
    println!("value: {}", value);
    Ok(())
}

Performance

Almost the same with tokio-postgres, you can see a live benchmark here.

Develop

Running tests needs a postgres server and environment variables: - TCP_URL="postgresql:///<db>?host=<tcp host>&port=<port>&user=<user>&password=<passwd>" - UDS_URL="postgresql:///<db>?host=<postgres uds dir>&port=<port>&user=<user>&password=<passwd>"

Extension points exported contracts — how you extend this code

AsyncReadWriter (Interface)
A alias for 'static + Unpin + Send + Read + Write [1 implementers]
src/socket.rs

Core symbols most depended-on inside this repo

connect
called by 5
src/lib.rs
timeout
called by 2
src/connect.rs
async_runtime
called by 2
tests/benchmark.rs
tokio_runtime
called by 2
tests/benchmark.rs
tokio_postgres
called by 2
tests/benchmark.rs
connect_tls
called by 1
src/connect.rs
connect_once
called by 1
src/connect.rs
connect_socket
called by 1
src/connect.rs

Shape

Function 9
Method 6
Class 1
Interface 1

Languages

Rust100%

Modules by API surface

src/socket.rs8 symbols
tests/benchmark.rs4 symbols
src/connect.rs4 symbols
src/lib.rs1 symbols

For agents

$ claude mcp add async-postgres \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact