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?
Related
I'm using the POSTGRESQL package to access a database and R times out and crashes every time when connecting ("The previous R session was terminated due to an unexpected crash"). Terminal says 'abrt-cli status' timed out.
I've tried accessing the same db through SSH and it works. Our sysadmin thinks the credentials are correct, and says R seems to be working fine for him (although he's doing completely different work that has nothing to do with accessing postgresql databases). I've tried naming the server rather than using 'localhost', just in case, and that doesn't work either. But even if the credentials were wrong, I feel like I'd get an error rather than a crash.
I realize this might have to do with our local configuration, but I'm at the end of my rope. I'd be extremely grateful for any ideas people have.
# Install the package RPostgres
knitr::opts_chunk$set(echo = TRUE)
library(DBI)
library(RPostgreSQL)
library(tidycensus)
# The user needs to input their password
input_password <- rstudioapi::askForPassword("Database password")
db <- [CENSORED, I PROMISE IT'S CORRECT :)]
host_db <- 'localhost'
db_port <- '5432'
db_user <- 'leviem'
db_password <- input_password
drv <- dbDriver("PostgreSQL")
conn <- dbConnect(drv,
dbname = db,
host = host_db,
port = db_port,
user = db_user,
password = db_password)
# List all the tables available
dbListTables(conn)
# Close the connection
dbDisconnect (conn)
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")
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!
Wanted to try a simple thing.I created empty database in pgadmin. I used R to connect to that database but it`s not working. I searched it up online but could not get it working even though I have all packages installed.
code I used which I got it from a website.
# install.packages("RPostgreSQL")
require("RPostgreSQL")
# create a connection
# save the password that we can "hide" it as best as we can by collapsing it
pw <- {
"new_user_password"
}
# loads the PostgreSQL driver
drv <- dbDriver("PostgreSQL")
# creates a connection to the postgres database
# note that "con" will be used later in each connection to the database
con <- dbConnect(drv, dbname = "postgres",
host = "localhost", port = 5432,
user = "******", password =******)
rm(pw) # removes the password`enter code here`
Am I missing something???
I have this R code and i want to connect to a postgres db using the conf file:
con <- dbConnect(PostgreSQL(), groups='epl')
The postgresql.conf file contains:
#------------------------------------------------------------------------------
# CUSTOMIZED OPTIONS
#------------------------------------------------------------------------------
# Add settings for extensions here
[epl]
host='localhost'
port = 5432
dbname='rlearning'
user='user'
password='pass'
When I run the R code, i get this error:
Error in postgresqlNewConnection(drv, ...) :
unused argument(s) (groups = "epl")
If everything else fails, you can try reading the documentation and following the examples. For help(dbConnect), you find
# create an PostgreSQL instance and create one connection.
drv <- dbDriver("PostgreSQL")
# open the connection using user, passsword, etc., as
con <- dbConnect(drv, dbname = "postgres")
and this is where you add additional user, password, host, arguments.
It will also show you that there is no argument groups explaining the error you get.