sfackler / rust-postgres

Native PostgreSQL driver for the Rust programming language
Apache License 2.0
3.42k stars 436 forks source link

Connection::parse rejects URLs without a host #1071

Closed PuercoPop closed 10 months ago

PuercoPop commented 10 months ago

Ej. the URL postgresql:://myuser@/mydb is rejected by rust-postgres but is accepted by psql. ej. The following command succeds

$ psql -d postgresql:://myuser@/mydb.

But when calling Client::connect("postgresql:://myuser@/mydb", NoTls) we get

Error: DBerror(Error { kind: Config, cause: Some("host missing") })

From the PostgreSQL documentation this is a valid URL. Quoting relevant sections below:

In particular, a Unix-domain socket connection is chosen if the host part is either empty or looks like an absolute path name

And the grammar marks the host as optional as well

postgresql://[userspec@][hostspec][/dbname][?paramspec] ... and hostspec is:

[host][:port][,...]


I looked briefly into how would fixing the issue would look like and iiuc I need to make the following test pass.

// tokio-postgres/tests/test/parse.rs
    #[cfg(unix)]
    check(
        "postgresql://user@/mydb",
        Config::new().user("user").host_path("/tmp").dbname("mydb"),
    );

One point of contention is what should the host_path be set to if the host is omitted. The documentation above only talks above standard unix-domain socket directory. The following link suggests that it should be /tmp on a vanilla PostgreSQL install. But that Debian and derivatives use /var/run/postgresql. In NixOS (my scenario) the default unix-domain socket directory is /run/postgresql. :shrug:


Note that although I encountered this error in production it is not a blocker as the workaround is straightforward.

sfackler commented 10 months ago

The Unix socket fallback is not implemented in this library - in libpq it depends on knowledge of the Postgres Unix socket directory which is set at Postgres compilation time and varies between distros.

PuercoPop commented 10 months ago

@sfackler Ok, so should I close the issue? Is it worth it detect that syntax was used and notify the that that syntax is not supported?

sfackler commented 10 months ago

Yeah unfortunately there isn't really anything we can do to make this work like it does for libpq.