|
15 | 15 | </div> |
16 | 16 | <br> |
17 | 17 |
|
18 | | -This crate is an out-of-the-box wrapper of [tokio-postgres](https://crates.io/crates/tokio-postgres). |
| 18 | +This crate is a wrapper of [tokio-postgres](https://crates.io/crates/tokio-postgres). |
19 | 19 |
|
20 | 20 | ### Pros |
21 | 21 |
|
22 | | -- runtime-independent, can be used on any async runtime. |
23 | | -- build-in tls support, based on [tokio-rustls](https://github.com/quininer/tokio-rustls). |
| 22 | +Runtime-independent, can be used on any async runtime. |
| 23 | + |
| 24 | +### Usage |
| 25 | + |
| 26 | +Almost the same with tokio-postgres. |
| 27 | + |
| 28 | +- TCP or UDS |
| 29 | + |
| 30 | +```rust |
| 31 | +use async_postgres::connect; |
| 32 | +use std::error::Error; |
| 33 | +use async_std::task::spawn; |
| 34 | + |
| 35 | +async fn play() -> Result<(), Box<dyn Error>> { |
| 36 | + let url = "host=localhost user=postgres"; |
| 37 | + let (client, conn) = connect(url.parse()?).await?; |
| 38 | + spawn(conn); |
| 39 | + let row = client.query_one("SELECT * FROM user WHERE id=$1", &[&0]).await?; |
| 40 | + let value: &str = row.get(0); |
| 41 | + println!("value: {}", value); |
| 42 | + Ok(()) |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +- TLS |
| 47 | + |
| 48 | +```rust |
| 49 | +use async_postgres::connect_tls; |
| 50 | +use native_tls::{Certificate, TlsConnector}; |
| 51 | +use postgres_native_tls::MakeTlsConnector; |
| 52 | +use std::fs; |
| 53 | +use std::error::Error; |
| 54 | +use async_std::task::spawn; |
| 55 | + |
| 56 | +async fn play() -> Result<(), Box<dyn Error>> { |
| 57 | + let cert = fs::read("database_cert.pem")?; |
| 58 | + let cert = Certificate::from_pem(&cert)?; |
| 59 | + let connector = TlsConnector::builder() |
| 60 | + .add_root_certificate(cert) |
| 61 | + .build()?; |
| 62 | + let connector = MakeTlsConnector::new(connector); |
| 63 | + let url = "host=localhost user=postgres sslmode=require"; |
| 64 | + let (client, conn) = connect_tls(url.parse()?, connector).await?; |
| 65 | + spawn(conn); |
| 66 | + let row = client.query_one("SELECT * FROM user WHERE id=$1", &[&0]).await?; |
| 67 | + let value: &str = row.get(0); |
| 68 | + println!("value: {}", value); |
| 69 | + Ok(()) |
| 70 | +} |
| 71 | +``` |
24 | 72 |
|
25 | 73 | ### Performance |
26 | 74 |
|
| 75 | +Almost the same with tokio-postgres, |
| 76 | +you can see a live benchmark [here](https://github.com/Hexilee/async-postgres/actions?query=workflow%3ABenchmark). |
| 77 | + |
27 | 78 | ### Develop |
| 79 | + |
| 80 | +Run tests need a running postgres server and environment variables: |
| 81 | +- `TCP_URL="postgresql:///<db>?host=<tcp host>&port=<port>&user=<user>&password=<passwd>"` |
| 82 | +- `UDS_URL="postgresql:///<db>?host=<postgres uds dir>&port=<port>&user=<user>&password=<passwd>"` |
0 commit comments