how-to-connect-rust-with-postgresql
This guide helps you connect your Rust project to a PostgreSQL database. It shows how to send and receive data easily between them.
[package] name = "rust-recap" version = "0.1.0" edition = "2021" [dependencies] postgres = "0.19.10"
use postgres::{Client, NoTls, Error}; fn main() -> Result<(), Error> { let mut client = Client::connect( "postgresql://postgres:qwerty@localhost:5432/keynvalues", NoTls, )?; // Insert a new key-value pair client.batch_execute( "INSERT INTO keyandvalues (key, value) VALUES ('mishal', '1');" )?; println!("Insert query successfully done"); for row in client.query("SELECT * FROM keyandvalues", &[])? { let key: &str = row.get("key"); let value: &str = row.get("value"); println!("Key: {}, Value: {}", key, value); } Ok(()) }