How to connect to Postgres using DBI in R? - r

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.
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
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).

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

How to establish a connection to a remote Redash database using R?

I want to use R, and specifically the {DBI} package, to access a remote Redash DB. Typically, DBI requires to establish a connection first. Such as:
# taken from: https://r4ds.hadley.nz/databases.html#connecting-to-a-database
con <- DBI::dbConnect(
RMariaDB::MariaDB(),
username = "foo"
)
## or
con <- DBI::dbConnect(
RPostgres::Postgres(),
hostname = "databases.mycompany.com",
port = 1234
)
Similarly, there's a package that supports accessing AWS:
library(DBI)
library(noctua)
con <- dbConnect(noctua::athena(),
aws_access_key_id='YOUR_ACCESS_KEY_ID',
aws_secret_access_key='YOUR_SECRET_ACCESS_KEY',
s3_staging_dir='s3://path/to/query/bucket/',
region_name='eu-west-1')
My question: is there a similar (or any!) way to access a Redash DB from R?

ODBC Connection to ORACLE in R

I am using both the DBI and ODBC package in "R" in order to make a connection to an ORACLE database.
Here is the connection code I am using:
library(DBI)
library(odbc)
con <- DBI::dbConnect(odbc::odbc(),
Driver = "ORACLE",
Host = "orasada.ca",
SVC = "STG",
UID = "username",
PWD = "password",
Port = 1521)
Everything looks fine to me, but I keep getting this error:
Error: nanodbc/nanodbc.cpp:983: IM002: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Any ideas on what I can change to make a successful connection?
Thanks.

How do you use Rstudio to connect to a Google Cloud PostGress Database?

I'm trying to use RStudio to connect to the a Google Cloud postgres database. My R code is below.
From the documentation I'm relatively sure I have the correct setup.
# Load the DBI library
library(DBI)
library(RPostgres)
# Helper for getting new connection to Cloud SQL
getSqlConnection <- function(){
con <- dbConnect(
RPostgres::Postgres(),
dbname = 'my_dbname',
host ='my_host',
port = 5432,
user ='username',
password = 'my_password' )
return(con)
}
conn <- getSqlConnection()
res <- dbListTables(conn)
print(res)
When I run the code above I'm getting the error
Error: FATAL: unsupported frontend protocol 1234.5679: server supports 2.0 to 3.0
I was hoping someone might have some advice on the error or how to setup a Rstudio connection to a Google Cloud postgres DB?
Thanks in advance!

MongoDB Connection in Rstudio with odbc

I'm trying to connect to a mongoDB database using library(odbc) in R. First I installed the driver from here and then I have used the following method:
con <- dbConnect(odbc::odbc(),
Driver = "MongoDB ODBC 1.3.0 Unicode Driver",
Server = "xxxxx",
AuthMechanism = "SCRAM-SHA-1",
Port = 27017,
Database = "test",
UID = "utest",
PWD = "ptest"
)
However the following error will happen:
Error: nanodbc/nanodbc.cpp:983: 08S01: [MySQL][ODBC 1.3(w) Driver]Lost
connection to MySQL server at 'waiting for initial communication
packet', system error: 10060
I would appreciate any help.Thanks

Connecting to Impala from R without Impala JDBC driver

For an undisclosed reason, my Impala does not have a JDBC driver installed. This is making the connection from R to Impala challenging.
I am able to connect (and query) to Impala shell via Putty. E.g.,
impala-shell --ssl -i some_name
Using the Putty connection mechanism/credentials, can this be performed from RStudio and bring in the SELECT results into a dataframe?
This worked in my Oracle BDA cluster.
library(dsreq)
print("Connecting to Impala...")
impaladb <- impalaConnection(pool='general')
dbResultsTempTbl <- dbGetQuery(impaladb, paste0("SELECT * FROM mytable") )
print("results")
print(dbResultsTempTbl)
You can use the ODBC driver to connect to impalaDB
library(ODBC)
drv <- odbc::odbc()
con <- DBI::dbConnect(drv = drv, driver = "Cloudera ODBC Driver for Impala",
host = "your hostname", port = 21050, Schema = "your schema")

Resources