Skip to content
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

src_postgres doesn't accept service argument #2292

Closed
timabe opened this issue Dec 6, 2016 · 1 comment
Closed

src_postgres doesn't accept service argument #2292

timabe opened this issue Dec 6, 2016 · 1 comment

Comments

@timabe
Copy link

timabe commented Dec 6, 2016

I connect to a lot of different postgres databases, so I find it useful to use the pg_service.conf file.

src_postgres doesn't accept service as one of the ... arguments. I looked into it and it appears it's because the driver is created using RPostgreSQL::PostgreSQL(), whereas if it was using RPostgres::Postgres() instead it would accept the service argument. Here's the line I'm referring to.

I don't know much about how solid the RPostgres package is, so maybe the fix isn't as simple as changing the function call for the driver but perhaps we can discuss here.

@krlmlr
Copy link
Member

krlmlr commented Dec 8, 2016

You should be able to create your own private version of src_postgres(). Unfortunately, it will probably take some time until RPostgres hits CRAN; dplyr might switch to using RPostgres then.

library(DBI)
library(dplyr)

"%||%" <- function(x, y) if(is.null(x)) y else x

db_disconnector <- function(con, name, quiet = FALSE) {
  reg.finalizer(environment(), function(...) {
    if (!quiet) {
      message("Auto-disconnecting ", name, " connection ",
              "(", paste(con@Id, collapse = ", "), ")")
    }
    dbDisconnect(con)
  })
  environment()
}

src_postgres2 <- function(dbname = NULL, host = NULL, port = NULL, user = NULL,
                         password = NULL, ...) {
  if (!requireNamespace("RPostgres", quietly = TRUE)) {
    stop("RPostgres package required to connect to postgres db", call. = FALSE)
  }

  user <- user %||% ""

  con <- dbConnect(RPostgres::Postgres(), host = host %||% "", dbname = dbname %||% "",
                   user = user, password = password %||% "", port = port %||% "", ...)
  info <- dbGetInfo(con)

  src_sql("postgres", con,
          info = info, disco = db_disconnector(con, "postgres"))
}

src_postgres2()

@timabe timabe closed this as completed Dec 8, 2016
@lock lock bot locked as resolved and limited conversation to collaborators Jun 8, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants