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")
)
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)
I've got a postgres database hosted on Digital Ocean. On a server, I use the below R code without error and am able to connect. However, on my local machine (Mac, M1, 11.2.3) I get an error. Any ideas?
library(DBI)
library(RPostgres)
library(dbplyr)
library(dbx)
con <- dbxConnect(adapter = "postgres",
host = "db-postgresql-XXX.ondigitalocean.com",
port = 11111,
dbname = "XXXX",
user = "XXXX",
password = "XXXXX")
The error I get is:
Error: FATAL: no pg_hba.conf entry for host "XXX", user "XXXX", database "XXXX", SSL off
p.s. I've replaced sensitive stuff in the above with XXX etc.
That is often a sign that you do are being barred from access by Postgres configurations.
You need to add your IP address to the pg_hba.conf file (or your administrator does) and label it as trusted. Once this is done, you will likely not have that problem.
It is a security feature.
The is a problem with the newest version of the RPostgres library. Downgrading worked for me:
install.packages("RPostgres", repos = "https://r-dbi.r-universe.dev")
More info here:
https://github.com/r-dbi/RPostgres/issues/291
I am running into
Error: could not receive data from server: Software caused connection abort (0x00002745/10053)
upon trying to connect to a postgres database using the DBI package in R. Note that i am in a work environment, so subject to a corporate firewall. Can that explain the error or is there something else that could be happening?
Here is the code I'm using
# Connect to trayaway dev
con <- DBI::dbConnect(
RPostgres::Postgres(),
host = host, port = 5432, dbname = "postgres",
user = user, password = password
)
error below:
Error: could not receive data from server: Software caused connection abort (0x00002745/10053)
solution was found- i tried the same code using wiFi and the code works - when hardwired, connection string fails to connect to database - so this is a corporate firewall issue - thank you,
I am having trouble creating an SSL connection using RPostgreSQL to an AWS hosted PostgreSQL database.
Here is what I've tried so far:
Created the PostgreSQL database on AWS.
Set the database parameter "rds.force_ssl" to 1.
Downloaded the AWS public key from https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem
Test the connection from a windows command prompt with psql (it works).
Executed the following in R:
library(RPostgreSQL)
cert <- paste0("C:/Users/johnr/Downloads/", "rds-combined-ca-bundle.pem")
dbname <- paste0("dbname=", "flargnog", " ", "sslrootcert=", cert, " ", "sslmode=verify-full")
host <- "xxxxxx.xxxxx.us-region-2.rds.amazonaws.com"
con <- dbConnect(dbDriver("PostgreSQL"), user="username", host=host, port=5432, dbname=dbname, password="abcd1234!")
I receive an error message after executing the last statement:
Error in postgresqlNewConnection(drv, ...) :
RS-DBI driver: (could not connect username#xxxxxx.xxxxx.us-region-2.rds.amazonaws.com on dbname "flargnog"
If I change the rds.force_ssl setting to 0 (and remove the ssl stuff from dbname) the connection works just fine.
I have looked at other posts on Stackoverflow related to this issue. This and this seem to indicate an SSL connection is not possible due to issues with RPostgreSQL. However, this post indicates that you can.
Any guidance would be appreciated!
You can try to ssh to the rds instance using e.g. putty and port-forward your local port 5432 to the remote port 5432. Once the ssh connection is open in R just connect to localhost:5432...
Here is how to port-forward using putty:
http://www.akadia.com/services/ssh_putty.html
Here is how this works via command-line:
https://gist.github.com/magnetikonline/3d239b82265398568f31
P.S.: Make sure your instance is in a security-group that accepts ssh connections - port 22
I am making the move from RSQLite to RMySQL and I am confused by the user and password fields. FWIW, I'm running Windows 7, R 2.12.2, MySQL 5.5 (all 64 bit), and RMySQL 0.7-5.
I installed RMySQL as prescribed in this previous SO question, and as far as I know it works (i.e., I can load the package with library(RMySQL)). But when I try to run the tutorial from the R data import guide, I get a "failed to connect to database..." error. This is the code from the tutorial from the guide:
library(RMySQL) # will load DBI as well
## open a connection to a MySQL database
con <- dbConnect(dbDriver("MySQL"), user = "root", password = "root", dbname = "pookas")
## list the tables in the database
dbListTables(con)
## load a data frame into the database, deleting any existing copy
data(USArrests)
dbWriteTable(con, "arrests", USArrests, overwrite = TRUE)
dbListTables(con)
## get the whole table
dbReadTable(con, "arrests")
## Select from the loaded table
dbGetQuery(con, paste("select row_names, Murder from arrests",
"where Rape > 30 order by Murder"))
dbRemoveTable(con, "arrests")
dbDisconnect(con)
On the second line I get the following error:
> con <- dbConnect(dbDriver("MySQL"), user = "richard", password = "root", dbname = "pookas")
Error in mysqlNewConnection(drv, ...) :
RS-DBI driver: (Failed to connect to database: Error: Access denied for user 'richard'#'localhost' (using password: NO)
)
I have tried with and without user and password and with admin as user. I have also tried using a dbname that I made before with the command line and with one that doesn't exist.
Any tips? Is there a good reference here? Thanks!
That is most likely a setup issue on the server side. Make sure that networked access is enabled.
Also, a local test with the command-line client is not equivalent as that typically uses sockets. The mysql server logs may be helpful.
First try to connect to MySQL server using MySQL Workbench or command line mysql using the same parameter. If it connects then R should also be able to connect.
Typically this issue comes when MySQL server doesn't allow connections from remote machines.
As people have told you, you can try to connect to the host with other application as mysql workbench. How odd! When I have tried in RStudio to connect to my db with your code without indicate the host in the command I haven't been able to connect.
I have needed to indicate the host ( host = 'localhost' ) in the command.