get_postgres_envvars.Rd
Retrieves database connection parameters stored in any of the environment variables known by Postgres, using defaults from `...` for parameters not set in the environment. In a standard PEcAn installation only a few of these parameters will ever be set, but we check all of them anyway in case you need to do anything unusual.
get_postgres_envvars(...)
list of connection parameters suitable for passing on to `db.open`
The list of environment variables we check is taken from the [Postgres 12 manual](https://postgresql.org/docs/12/libpq-envars.html), but it should apply to older Postgres versions as well. Note that this function only looks for environment variables that control connection parameters; it does not retrieve any of the variables related to per-session behavior (e.g. PGTZ, PGSYSCONFDIR).
host <- Sys.getenv("PGHOST") # to restore environment after demo
Sys.unsetenv("PGHOST")
get_postgres_envvars()$host # NULL
#> NULL
get_postgres_envvars(host = "default", port = 5432)$host # "default"
#> [1] "default"
# defaults are ignored for a variable that exists
Sys.setenv(PGHOST = "localhost")
get_postgres_envvars()$host # "localhost"
#> [1] "localhost"
get_postgres_envvars(host = "postgres")$host # still "localhost"
#> [1] "localhost"
# To override a set variable, edit the returned list before using it
con_parms <- get_postgres_envvars()
con_parms$host # "localhost"
#> [1] "localhost"
con_parms$host <- "postgres"
# db.open(con_parms)
Sys.setenv(PGHOST = host)