rustrust-sqlx

sqlx this functionality requires a Tokio context


I'm looking at the quickstart of SQLx to connect to a posgresql database:

Cargo.toml:

[dependencies]
sqlx = { version = "0.7", features = [ "postgres", "runtime-tokio", "tls-rustls" ] }
tokio = { version = "0.2.22", features = ["full"] }

and here is my main.rs (copied from the read me).

use sqlx::PgPool;

#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
    let pool = PgPool::connect("postgres://root:password@localhost:5555/test").await?;

    let row: (i64,) = sqlx::query_as("SELECT $1")
        .bind(150_i64)
        .fetch_one(&pool)
        .await?;

    assert_eq!(row.0, 150);

    Ok(())
}

When I run cargo run I get the error:

thread 'main' panicked at /Users/valentin/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sqlx-core-0.7.4/src/pool/inner.rs:500:5:
this functionality requires a Tokio context

I can connect to my PostgreSQL database with the command:

 psql -h localhost -p 5555 -U root -d test

and the password password.

What am I missing?


Solution

  • Your Tokio version is too low. SQLx requires a higher version. Tokio is already at 1.37 at the time of writing. If you change it, it should work straight away.