sfackler / rust-postgres

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

Option to read config from environment variables? #654

Open vpzomtrrfrt opened 4 years ago

vpzomtrrfrt commented 4 years ago

As documented here: https://www.postgresql.org/docs/current/libpq-envars.html

libpq can read connection information from environment variables, could this be an option in tokio-postgres as well?

benesch commented 4 years ago

Not the maintainer, but I was personally unsure about whether this should be part of the [tokio-]postgres crate or not. I did make it possible to read the environment variables yourself in the meantime, though! (#535)

Here's some example code you can copy/paste if you like:

https://github.com/MaterializeInc/materialize/blob/60ef4511c45867dea4e4091812cabb0b36c86271/src/symbiosis/src/lib.rs#L57-L84

sfackler commented 4 years ago

While I wouldn't want this to happen by default, it seems potentially plausible to have a method on the Config type to do this - Config::new().load_from_env() or whatever.

bikeshedder commented 3 years ago

It would be great if tokio-postgres did support the config crate. In deadpool-postgres I have create my own Config struct which is a 1:1 copy of the tokio_postgres::Config but supports serde and the config crate. See https://github.com/bikeshedder/deadpool/blob/master/postgres/src/config.rs#L219

@vpzomtrrfrt If you are using tokio and need a pool anyways you could use deadpool-postgres which supports the config crate. You could even use just the Config struct from deadpool-postgres and call the get_pg_config method, but that would add a lot of unneeded dependencies to your code.

@sfackler Would you like to adopt this code to rust-postgres? I'd be more than happy to get rid of my own config wrapper and provide a PR for that instead.