Skip to content

Instantly share code, notes, and snippets.

@allenaven
Created June 28, 2017 09:01
Show Gist options
  • Save allenaven/cb7221e8b537f00709cf85a098141b3c to your computer and use it in GitHub Desktop.
Save allenaven/cb7221e8b537f00709cf85a098141b3c to your computer and use it in GitHub Desktop.
Example R dplyr connection to a Postgres database, since I have to look it up every time I code it...
## Connect to a local Postgres database running in a Docker container
## although the Docker part isn't of any importance here.
## Working with dplyr 0.7, DBI 0.7, dbplyr 1.1.0
library(DBI)
library(dplyr)
library(dbplyr)
con <- DBI::dbConnect(RPostgreSQL::PostgreSQL(), host = 'localhost', port = 8990,
dbname = <name of db>, user = <user>, password = <pass>)
DBI::dbListTables(con)
deployments <- tbl(con, <name of table>)
dbExistsTable(con, <name of table>)
t <- dbGetQuery(con, <name of table>)
dbDisconnect(con)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment