|
| 1 | +(connect-rust)= |
| 2 | + |
| 3 | +# Rust |
| 4 | + |
| 5 | +:::{div} sd-text-muted |
| 6 | +Connect to CrateDB from Rust applications. |
| 7 | +::: |
| 8 | + |
| 9 | +:::{rubric} About |
| 10 | +::: |
| 11 | + |
| 12 | +[postgres] is a synchronous Rust client for the PostgreSQL database. |
| 13 | + |
| 14 | +:::{rubric} Synopsis (localhost) |
| 15 | +::: |
| 16 | + |
| 17 | +`main.rs` |
| 18 | +```rust |
| 19 | +use postgres::{Client, NoTls}; |
| 20 | + |
| 21 | +fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 22 | + let mut client = Client::connect("postgresql://crate@localhost:5432/?sslmode=disable", NoTls)?; |
| 23 | + |
| 24 | + for row in client.query( |
| 25 | + "SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3", |
| 26 | + &[], |
| 27 | + )? { |
| 28 | + let mountain: &str = row.get(0); |
| 29 | + let height: i32 = row.get(1); |
| 30 | + println!("{}: {}", mountain, height); |
| 31 | + } |
| 32 | + Ok(()) |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +Start CrateDB using Docker or Podman, then compile and invoke example program. |
| 37 | +```shell |
| 38 | +docker run --rm --publish=5432:5432 crate '-Cdiscovery.type=single-node' |
| 39 | +``` |
| 40 | +```shell |
| 41 | +cargo init |
| 42 | +cargo add postgres |
| 43 | +cargo run |
| 44 | +``` |
| 45 | + |
| 46 | +:::{rubric} Synopsis (CrateDB Cloud) |
| 47 | +::: |
| 48 | +For CrateDB Cloud, add TLS support and update the connection string with |
| 49 | +your cluster details. |
| 50 | + |
| 51 | +`main.rs` |
| 52 | +```rust |
| 53 | +use postgres::Client; |
| 54 | +use native_tls::TlsConnector; |
| 55 | +use postgres_native_tls::MakeTlsConnector; |
| 56 | + |
| 57 | +fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 58 | + let tls = MakeTlsConnector::new(TlsConnector::new()?); |
| 59 | + let mut client = Client::connect("postgresql://crate:<password>@<cluster-name>.<region>.cratedb.net:5432/?sslmode=require", tls)?; |
| 60 | + |
| 61 | + for row in client.query( |
| 62 | + "SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3", |
| 63 | + &[], |
| 64 | + )? { |
| 65 | + let mountain: &str = row.get(0); |
| 66 | + let height: i32 = row.get(1); |
| 67 | + println!("{}: {}", mountain, height); |
| 68 | + } |
| 69 | + Ok(()) |
| 70 | +} |
| 71 | +``` |
| 72 | +```shell |
| 73 | +cargo init |
| 74 | +cargo add postgres postgres-native-tls native-tls |
| 75 | +cargo run |
| 76 | +``` |
| 77 | + |
| 78 | + |
| 79 | +## Example |
| 80 | + |
| 81 | +:::{card} |
| 82 | +:link: https://github.com/crate/cratedb-examples/tree/main/by-language/rust-postgres |
| 83 | +:link-type: url |
| 84 | +{material-outlined}`play_arrow;2em` |
| 85 | +Connecting to CrateDB with Rust. |
| 86 | ++++ |
| 87 | +Demonstrates a basic example program that uses the Rust postgres package. |
| 88 | +::: |
| 89 | + |
| 90 | +[](https://github.com/crate/cratedb-examples/actions/workflows/lang-rust-postgres.yml) |
| 91 | + |
| 92 | + |
| 93 | +[postgres]: https://crates.io/crates/postgres |
0 commit comments