lpil / pgo

🐘 Use PostgreSQL databases with PGO
https://hexdocs.pm/gleam_pgo/
Apache License 2.0
123 stars 12 forks source link

url_config is not parsing valid connection string. #24

Closed JohnBakhmat closed 3 months ago

JohnBakhmat commented 3 months ago

Hi, so ive been trying to connect to db using pgo.url_config, and it seams to be denying a valid string.

Here is how i use this function https://github.com/JohnBakhmat/shorty_gleam/blob/68fea8d78fae847a1efa779dc80070a7939754a2/src/app/db.gleam#L7

pub fn connect(connection_string cs: String) -> Result(pgo.Connection, Nil) {
  use config <- result.try(pgo.url_config(cs))
  Ok(pgo.connect(config))
}

Im loading connection string from .env using envoy https://github.com/JohnBakhmat/shorty_gleam/blob/68fea8d78fae847a1efa779dc80070a7939754a2/src/app/router.gleam#L38

let assert Ok(db_string) =
      envoy.get("DATABASE_URL")
      |> result.replace_error("No DATABASE_URL provided")

    let assert Ok(db_conn) =
      db.connect(db_string)
      |> result.replace_error("Couldn't connect to db")

And the connection string im trying to use is postgresql://postgres:supfuckers@127.0.0.1:5432/postgres

I thought maybe i didnt provide username and password correctly but there is no example on how to do it correctly in gleam/uri docs https://hexdocs.pm/gleam_stdlib/gleam/uri.html#parse. the string does work when using psql tool

image

lpil commented 3 months ago

Thank you

JohnBakhmat commented 3 months ago

Oh alright i found where the issue is. When pgo tries to parse url protocol out of string, it asserts that it is postgres where as in postgres docs https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING it is stated that it can be either postgres or postgresql https://github.com/lpil/pgo/assets/34962307/ccdc6afb-cd83-4fa0-a919-e16633ab93b3. The problematic line is located at https://github.com/lpil/pgo/blob/1f2307b13ed46c1ef4ea7967729e34dd7423132b/src/gleam/pgo.gleam#L89 I made a pr https://github.com/lpil/pgo/pull/25, if you want some changes im at your service :)