-
Notifications
You must be signed in to change notification settings - Fork 228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Read Postgres settings from environment variables #2541
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
065cb8e
PEcAn.DB: new function to get connection parameters from environment …
infotroph f2f3d8e
use get_postgres_envvars in tests
infotroph c8b6fb5
check for PGHOST at db setup time (used by eg add.models.sh)
infotroph 665f5cc
add PR number to changelog
infotroph File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#' Look up Postgres connection parameters from environment variables | ||
#' | ||
#' 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. | ||
#' | ||
#' 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). | ||
#' | ||
#' @param ... defaults for parameters not found in the environment, | ||
#' in `name = value` form | ||
#' @return list of connection parameters suitable for passing on to `db.open` | ||
#' | ||
#' @examples | ||
#' host <- Sys.getenv("PGHOST") # to restore environment after demo | ||
#' | ||
#' Sys.unsetenv("PGHOST") | ||
#' get_postgres_envvars()$host # NULL | ||
#' get_postgres_envvars(host = "default", port = 5432)$host # "default" | ||
|
||
#' # defaults are ignored for a variable that exists | ||
#' Sys.setenv(PGHOST = "localhost") | ||
#' get_postgres_envvars()$host # "localhost" | ||
#' get_postgres_envvars(host = "postgres")$host # still "localhost" | ||
#' | ||
#' # To override a set variable, edit the returned list before using it | ||
#' con_parms <- get_postgres_envvars() | ||
#' con_parms$host # "localhost" | ||
#' con_parms$host <- "postgres" | ||
#' # db.open(con_parms) | ||
#' | ||
#' Sys.setenv(PGHOST = host) | ||
#' @export | ||
get_postgres_envvars <- function(...) { | ||
pg_vars <- list( | ||
host = "PGHOST", | ||
hostaddr = "PGHOSTADDR", | ||
port = "PGPORT", | ||
dbname = "PGDATABASE", | ||
user = "PGUSER", | ||
password = "PGPASSWORD", | ||
passfile = "PGPASSFILE", | ||
service = "PGSERVICE", | ||
options = "PGOPTIONS", | ||
application_name = "PGAPPNAME", | ||
ssl_mode = "PGSSLMODE", | ||
requiressl = "PGREQUIRESSL", | ||
sslcompression = "PGSSLCOMPRESSION", | ||
sslcert = "PGSSLCERT", | ||
sslkey = "PGSSLKEY", | ||
sslrootcert = "PGSSLROOTCERT", | ||
sslcrl = "PGSSLCRL", | ||
requirepeer = "PGREQUIREPEER", | ||
krbsrvname = "PGKRBSRVNAME", | ||
gsslib = "PGGSSLIB", | ||
connect_timeout = "PGCONNECT_TIMEOUT", | ||
client_encoding = "PGCLIENTENCODING", | ||
target_session_attrs = "PGTARGETSESSIONATTRS") | ||
|
||
vals <- Sys.getenv(pg_vars) | ||
names(vals) <- names(pg_vars) | ||
vals <- vals[vals != ""] | ||
|
||
defaults <- list(...) | ||
defaults <- defaults[!(names(defaults) %in% names(vals))] | ||
|
||
append(vals, defaults) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it make sense to have most of these coded in get_postgres_envvars as defaults?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wondered that too, and could be persuaded either way. At the moment I leaning toward thinking it's less magic without, because then what you get back is strictly what you put in modulo whatever PG* vars are set. Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
roll a 🎲 and see what happens. I can be convinced of either solution, hence I approved the pull request.