Tokio_postgres hangs when trying to perform simple query

390 Views Asked by At

I'm trying to connect to a postgres server from my Rust program, but I can't seem to run even simple queries, they just hang with no error or output.

I start my database with:

docker run -p 9897:5432 --name postgres-server -e POSTGRES_PASSWORD=postgres -d postgres

And try to connect to it:

#[tokio::main]
async fn main() {
    println!("Connecting to Postgres.");
    let (client, conn) = tokio_postgres::Config::new()
        .user("postgres")
        .password("postgres")
        .host("localhost")
        .port(9897)
        .dbname("postgres")
        .connect(tokio_postgres::NoTls)
        .await
        .unwrap();
    println!("Connected to Postgres.");

    client.simple_query("SELECT 1")
        .await
        .unwrap();
    
    println!("Query finished!");
}

With these versions of tokio & tokio-postgres

[dependencies]
tokio = {version="1.27.0", features = ["rt", "full"]}
tokio-postgres = "0.7.8"

But it never finishes the query. I am running on a MacBook Pro M1.

1

There are 1 best solutions below

0
On

From the documentation of tokio_postgres::Client::simple_query:

Executes a sequence of SQL statements using the simple query protocol, returning the resulting rows.

Statements should be separated by semicolons.

Your statement is never completed, because it is never executed, since you didn't finish it.

Note that this is crate-specific behavior. For example sqlx crate in it's query function expects a single query without terminating semicolon.