Is this your sudden foray into Rust? Although not totally for beginners, this post shows how to connect to a PostgreSQL database from Rust. Note that we have updated the content of this post to use a newer version of postgres crate.
Rust and PostgreSQL Requirements
- Cargo that comes with Rust 1.53.0
- A Rust package manager akin to Apache Maven
- It also gets installed when we install Rust with rustup-init.exe
- A Rust crate called postgres
- A running instance of a PostgreSQL database
PostgreSQL Database Table And Test Data
Before running any Rust codes and connecting to PostgreSQL, please ensure we have a running instance of a PostgreSQL database with a table and test data. This post uses a specific database table which we can create by running the following SQL DDL against PostgreSQL.
1 2 3 4 5 6 7 | create table students ( student_id serial not null constraint students_pk primary key, student_name varchar(50) ); |
The students table has only two fields – an auto-incrementing field and a string field. Then, insert some data into the table.
1 2 3 4 5 6 | +----------+------------+ |student_id|student_name| +----------+------------+ |1 |karl | |2 |turreta | +----------+------------+ |
Create a Rust Project With Dependency on Postgres crate
Next, we create a Rust project with a dependency on the postgres crate, as shown below. Under the [dependencies] section, add the postgres = "0.19.1".
1 2 3 4 5 6 7 8 | [package] name = "rust-project1" version = "0.1.0" authors = ["Karl San Gabriel"] edition = "2018" [dependencies] postgres = "0.19.1" |
Rust Codes To Connect To PostgreSQL
Before we create the codes to connect to PostgreSQL and retrieve data, we need to import some structs from the postgres crate, as shown below. We will use these structs to connect to the database.
1 2 | use postgres::{Client, TlsMode}; ... |
Then, we need to create a struct that represents the students table in our database. The struct has only two fields like the students table.
1 2 3 4 5 6 | ... struct Student { id: i32, name: String } ... |
Next, we modify the main function. As the first line of the function, get a connection using the following line of codes. These very Rust codes connect to the PostgreSQL database. Note that we are passing both the database user name and password. In addition to that, we are not using TLS.
1 | let mut conn = Client::connect("postgresql://turreta:a1128f69-e6f7-4e93-a2df-3d4db6030abc@localhost:5442", NoTls).unwrap(); |
Now with a database connection, we can retrieve records from a table.
1 2 3 4 5 6 7 | for row in &conn.query("SELECT student_id, student_name FROM students", &[]).unwrap() { let student = Student { id: row.get(0), name: row.get(1) }; println!("Found student {} with ID:{}", student.name, student.id); } |
When we run our application, we get the following output.
1 2 3 4 5 6 7 | C:/Users/karldev/.cargo/bin/cargo.exe run --color=always --package rust-postgresql --bin rust-postgresql Finished dev [unoptimized + debuginfo] target(s) in 0.48s Running `target\debug\rust-postgresql.exe` Found student karl with ID:1 Found student turreta with ID:2 Process finished with exit code 0 |