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???
Related
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?
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)
I'm writing a R package for the first time. Its called aactr and is hosted on my GitHub: https://github.com/jasonbaik94/aactr
Right now, the package only has one function, aact_connect:
aact_connect <- function(user, password) {
drv <- DBI::dbDriver('PostgreSQL')
con <- DBI::dbConnect(drv,
dbname="aact",
host="aact-db.ctti-clinicaltrials.org",
port=5432,
user=user,
password=password)
}
My concern is that users of my package wouldn't like to input their username and password in their R script for privacy reasons.
What would be a nice workaround to ensure user privacy?
One thought I had: When the user types aact_connect(), a window pops up where the user can input username and password and press Enter, after which the connection would be made. Also, for those without a username or password, I'd put a parameter, init_connection = TRUE, after which this sign up page would load: https://aact.ctti-clinicaltrials.org/users/sign_up
Any others suggestions are greatly welcome!
There are many ways to securely handle database credentials in scripts. See few examples below:
Use configuration files such as yaml saves securely on user's machines for R to read into needed variables. See #Spacedman's answer.
config.yaml
db:
host : localhost
port : 5432
name : mypgdb
user : pg_useR
pwd : ***
R
library(yaml)
config = yaml.load_file("/path/to/config.yml")
dbConnect(drv, host = config$db$host, port = config$db$port,
dbname = config$db$name,
user = config$db$user, password = config$db$pwd)
Use environmental variables that are linked to user or system profiles:
db_creds <- Sys.getenv(c("DB_HOST", "DB_PORT", "DB_NAME", "DB_USER", "DB_PWD"))
con <- DBI::dbConnect(drv,
dbname = db_creds[['DB_NAME']],
host = db_creds[['DB_HOST']],
port = db_creds[['DB_PORT,']],
user = db_creds[['DB_USER']],
password = db_creds[['DB_PWD']])
Use DSNs that maintains connection parameters but requires an ODBC connection which can be run with the generalized odbc package (part of same DBI family as RPostgreSQL). Postgres maintains up-to-date odbc drivers for most operating systems. See R-bloggers post.
odbc.ini
[myPG_DSN]
Driver = PostgreSQL Unicode
Database = mypg_db
Servername = localhost
UserName = pg_useR
Password = ***
R
db <- dbConnect(odbc::odbc(), "myPG_DSN")
I'm running a Postgres-9.4 server that I would like to require SSL for. When I connect to the Postgres server from my laptop with either pgadmin or windows odbc connection, it works with SSL. However when I try to connect with R using SSL it fails.
library(RPostgreSQL)
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv,
user = "postgres",
password = mypasswd,
dbname = "dbname=postgres sslmode=prefer",
host = "192.168.1.179")
If I set my pg_hba.conf to allow non-ssl connections then this will work. When I set it to only allow SSL connections then this will fail. Unfortunately dbConnect doesn't have a verbose option so I don't get anything more than could not connect postgres#192.168.1.179 on dbname "postgres"
I found this question which seems to suggest I'm doing the right thing but, no go.
Edit:
I did some more digging and found this discussion which suggests that this won't work on windows due to various library/dll issues. That discussion is a few years old at this point so perhaps it has been resolved. I can confirm that doing the above from linux does work.
My understanding of the problem is that RPostgreSQL uses a client that doesn't support SSL.
I switched to the package RPostgres and got it to work. I first installed RPostgres with install.packages('RPostgres') then used this package to connect with sslmode="require".
library('RPostgres') #Instead of library('RPostgreSQL')
con <- dbConnect(RPostgres::Postgres(),
user = "postgres",
password = mypasswd,
dbname = "postgres",
host = "192.168.1.179",
sslmode = "require")
)
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.