Newbie can't setup PostgreSQL

I'm a new comer to Rust but I've been programming other stuff for decades. I've done my "hello world" and now I want to progress to database stuff.

I watched a tutorial on Crate, so I have an idea how that works.

Now I'm trying to follow and postgres - Rust but the example code doesn't work on my machine.

This is my Cargo.toml file. I've added all the dependencies that were listed

name = "prj_test_db"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at

base64 = "0.13.1"
byteorder = "1.4.3"
bytes = "1.4.0"
fallible-iterator = "0.2.0"
hmac = "0.12.1"
md-5 = "0.10.5"
memchr = "2.5.0"
postgres-protocol = "0.6.4"
rand = "0.8.5"
sha2 = "0.10.6"
stringprep = "0.1.2"

This is my where I put the exact example code from the website inside a main function.

use postgres::{Client, NoTls};

fn main() {
    let mut client = Client::connect("host=localhost user=postgres", NoTls)?;

        CREATE TABLE person (
            id      SERIAL PRIMARY KEY,
            name    TEXT NOT NULL,
            data    BYTEA

    let name = "Ferris";
    let data = None::<&[u8]>;
        "INSERT INTO person (name, data) VALUES ($1, $2)",
        &[&name, &data],

    for row in client.query("SELECT id, name, data FROM person", &[])? {
        let id: i32 = row.get(0);
        let name: &str = row.get(1);
        let data: Option<&[u8]> = row.get(2);

        println!("found person: {} {} {:?}", id, name, data);

    println!("Hello, world!");

And the command line gives me...

matthew@matthew-QEMU:~/Rust/004 PostgreSQL/prj_test_db$ cargo run
   Compiling prj_test_db v0.1.0 (/home/matthew/Rust/004 PostgreSQL/prj_test_db)
error[E0432]: unresolved import `postgres`
 --> src/
1 | use postgres::{Client, NoTls};
  |     ^^^^^^^^ use of undeclared crate or module `postgres`

error[E0308]: mismatched types
  --> src/
18 |         &[&name, &data],
   |                  ^^^^^ expected `&str`, found enum `Option`
   = note: expected reference `&&str`
              found reference `&Option<&[u8]>`

Some errors have detailed explanations: E0308, E0432.
For more information about an error, try `rustc --explain E0308`.
error: could not compile `prj_test_db` due to 2 previous errors

I've run and read rustc --explain E0308 and rustc --explain E0432 and I still have no idea what is wrong.

What am I missing?

I don't see the postgres dependency in your dependencies (which is the reason for your error, i.e. the postgres crate can't be found). Can you try and run cargo add postgres or add it manually to your dependencies:

postgres = "0.19.4"
1 Like

Fantastic thank you for that.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.