Sqlx::query!() not connecting/recognizing Postgress port number

Let me preface this by stating Im new to server side development and relatively new to Rust. Im trying to connect my DB that I have set up in PG to my rust code using sqlx but I keep getting an error that states that the port number is invalid:

error with configuration: invalid port number
   --> tutor-db/src/bin/iter1.rs:22:23
  let course_rows = sqlx::query!(
    |  _____________________ ^
    | |         r#"select course_id, tutor_id, course_id, posted_time
    | |            from ezy_course_c4 where course_id = $1"#,
    | |         1
    | |          )
    | |_____^
    |

my dbPath
DATABASE_URL=postgres://{{MY_USER_NAME}}:{{MY_PASSWORD}}@127.0.0.1:5432/{{MY_DB}}

my file that's using sqlx to connect, I know its not optimal code but im using this to learn:

use chrono::NaiveDateTime;
use dotenv::dotenv;
use sqlx::postgres::PgPool;
use std::env;
use std::io;

#[derive(Debug)]
pub struct Course {
    pub course_id: i32,
    pub tutor_id: i32,
    pub course_name: String,
    pub posted_time: Option<NaiveDateTime>,
}

#[actix_rt::main]
async fn main() -> io::Result<()> {
    dotenv().ok();
    let database_url = env::var("DATABASE_URL").expect("DATABASE_URL is not set in the .env file");
    let database_pool = PgPool::connect(&database_url).await.unwrap();
    let course_rows = sqlx::query!(
        r#"select course_id, tutor_id, course_id, posted_time
           from ezy_course_c4 where course_id = $1"#,
        1
    )
    .fetch_all(&database_pool)
    .await
    .unwrap();
    let mut course_list = vec![];
    for course in course_rows {
        course_list.push(Course {
            course_id: course_rows.course_id,
            tutor_id: course_rows.tutor_id,
            course_name: course_rows.course_name,
            posted_time: Some(chrono::NaiveDateTime::from(
                course_rows.posted_time.unwrap(),
            )),
        })
    }
    println!("Course = {:?}", course_list);
    Ok(())
}

Any help is appreciate. if any more information is needed from me please let me know

Do you have special characters like # in your password, see issue?

You need to write sth. like this
DATABASE_URL="postgres://user:abc'def\$ghi"\""jkl@localhost:5432/db" which is for password abc'def$ghi"jkl.

2 Likes

I fixed it I had many typos in my .spl and database config. thank you. I can close this now.

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.