Home > Mobile >  How to connect to Postgres using DBI in R?
How to connect to Postgres using DBI in R?

Time:10-16

Trying to connect to Postgres using DBI for first time to use dbplyr.

Issue: I am trying to use dbplyr with my exiting odbc connection but dbplyr doesn't seems to work with that so trying to create a new connection using DBI but this dbi connection is giving an error. So I am looking to fix dbi connection.

  1. When I try to replicate my existing connection using DBI then connection doesn't work:

libs

library(odbc)
library(RODBC)

library(DBI)
library(dplyr)
library(dbplyr)
# from: https://stackoverflow.com/questions/59413904/reading-data-from-a-sql-server-in-rstudio-dplyr
# from: /help/library/DBI/html/dbConnect.html
# from: https://github.com/r-dbi/odbc#odbc

dbicon <-  DBI::dbConnect( 
               odbc::odbc(),
               driver = "PostgreSQL Unicode", 
               database = "Postgres_xyz_db", 
               host = "some_xyz.amazonaws.com", 
               port = "5432", 
               uid = "user",
               pwd = "users_password")

# connect with table
tbl_qry <- tbl(dbicon, "mydb_demo.demo_table")

tbl_qry %>% head()

Error with dbConnect step:

Error: nanodbc/nanodbc.cpp:1021: 00000: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
 
In addition: Warning message:
In for (i in seq_len(n)) { : closing unused RODBC handle 2

PS: I am running this on localhost:8787

  1. My Existing odbc / RODBC connection which is working for the same db.

libs

library(odbc)
library(RODBC)

library(dplyr)
library(dbplyr)

Existing Working connection:

## #################################################################
##                         Connection Para
## #################################################################

driver.name <- "PostgreSQL Unicode"
db.name <- "Postgres_xyz_db"
host.name <- "some_xyz.amazonaws.com" 
port <-"5432"
user.name <-"user"
pwd <- "users_password"

## #################################################################
##                      connect to a database
## #################################################################

con.text <- paste("DRIVER=",driver.name,
                  ";Database=",db.name,
                  ";Server=",host.name,
                  ";Port=",port,
                  ";PROTOCOL=TCPIP",
                  ";UID=", user.name,
                  ";PWD=",pwd,sep="")

con1 <- odbcDriverConnect(con.text)

But dbplyr doesn't work with this connection

# connect with table
tbl_qry <- tbl(con1, "mydb_demo.demo_table")

tbl_qry %>% head() 

(I am not really a techy or db admin or devops guy so pls excuse if it looks kind of filled with basic mistakes).

CodePudding user response:

odbc connections prefer server= over host=, so your connection attempt should likely be

dbicon <-  DBI::dbConnect( 
               odbc::odbc(),
               driver = "PostgreSQL Unicode", 
               database = "Postgres_xyz_db", 
               server = "some_xyz.amazonaws.com", 
               port = "5432", 
               uid = "user",
               pwd = "users_password")
  • Related