R- Connecting local postgresql to R - r

I have postgres installed locally in my ubuntu. The dbname is postgres and the user name is also postgres. When I'm trying to connect this database to my R script, I'm getting an error saying-
Error in postgresqlNewConnection(drv, ...) :
RS-DBI driver: (could not connect postgres#localhost on dbname "postgres"
Here is the command that I'm running-
con <- dbConnect(drv, dbname = "postgres",
host = "localhost", port = 5432,
user = "postgres", password=pw)
I checked that this is the right command to run to make this connection and I have also used this to connect to an external server before. But somehow I'm not able to connect to my local postgres instance.
What do you think I'm missing out?

Related

Connecting multiple server PostgreSQL R

I needed to put 3 shards of a database on three different servers. So I created 3 servers in pgAdmin(s1,s2,s3), then I put each server one shard. Then, I tried to connect one of the servers in R; however, I couldn't make the connection. I always get an error:
Error in postgresqlNewConnection(drv, ...) : RS-DBI driver: (could not connect postgres#172.17.0.1:5432 on dbname "postgres": could not connect to server: Operation timed out Is the server running on host "172.17.0.1" and accepting TCP/IP connections on port 5432?
My code is:
#install.packages("RPostgreSQL")
require("RPostgreSQL")
library(DBI)
# create a connection
# save the password that we can "hide" it as best as we can by collapsing it
pw <- {
"postgres"
}
# loads the PostgreSQL driver
drv <- dbDriver("PostgreSQL")
# creates a connection to the postgres database
con <- dbConnect(
drv,
dbname = "postgres",
host = "172.17.0.1",
port = 5432,
user = "postgres",
password = pw
)
rm(pw) # removes the password
pgAdmin snap
Did I write something wrong?
if this is using container make sure to forward the port 5432 on 0.0.0.0 i.e the container is listening on the port 5432.
Also you've gotta check this setting if you are not doing the connection locally ONLY>, in the postgresql.conf file:
# - Connection Settings -
#listen_addresses = 'localhost' >>>> This should be = '*' instead of localhost
Save the conf and restart the service. Hope this helps!

PostgreSQL dbConnect to shiny app in ec2 instance

I have an ec2 instance set up with my shiny app and my postgresql database, I want to get the shiny-app to read from the database
If I type psql and \conninfo while ssh-ed into my instance I get
You are connected to database "ubuntu" as user "ubuntu" via socket in "/var/run/postgresql" at port "5432".
When I use R in the ec2 command line and type the following, I can read from my database no problem!
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, dbname = "ubuntu", host = "/var/run/postgresql", port = 5432, user = "ubuntu", password = pw)
However, when I put these same lines in my shiny app.R file I get
Error in postgresqlNewConnection(drv, ...) :
RS-DBI driver: (could not connect ubuntu#/var/run/postgresql:5432 on dbname "ubuntu": FATAL: Peer authentication failed for user "ubuntu")
I've tried so many different values for host like
host = "localhost"
host = "my ec2 public ip address"
host = "127.0.0.1"
for example and nothing has been working.
my security group for this ec2 instance has an inboud connection to port 5432.
could this be it: why is one file green and the other pink? the green one is the one that works (local) and the pink one is on my instance
Finally figured it out.. this is the same problem as Getting error: Peer authentication failed for user "postgres", when trying to get pgsql working with rails
except that I was getting a different error for the same underlying problem.
the answer that worked for me is the second one:
1.
nano /etc/postgresql/9.x/main/pg_hba.conf
change peer in this line
local all postgres peer
to
local all postgres trust
Restart the server
sudo service postgresql restart
Login into psql and set your password
psql -U postgres
ALTER USER postgres with password 'your-pass';
Finally change the pg_hba.conf from
local all postgres trust
to
local all postgres md5
and that finally worked

RStudio on Windows PostgreSQL SSL Connection with RPostgreSQL

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

Connect to a PostgreSQL database in a Server from a local machine R

When you connect to PostgreSQL thorough RPostgreSQL for example I do the following.
m <- dbDriver("PostgreSQL")
con <- dbConnect(m, host = "localhost", user= "postgres", password="admin",
dbname = "postgres")
But if you want to connect to a postgreSQL in a Server, I'm not sure how to do it. I'd do the following:
m <- dbDriver("PostgreSQL")
con <- dbConnect(m, host = "10.100.100.10", port = "5432", user= "postgres",
password="admin", dbname = "postgres")
and this gives me this error:
Error in postgresqlNewConnection(drv, ...) :
RS-DBI driver: (could not connect postgres#10.100.100.10 on dbname "postgres"
You (or whoever admins the server) needs to allow access from another machine over tcp/ip -- this is turned off by default. See for example
this previous SO question
this blog post
this PostgreSQL documentation entry

Connect with rpostgresql

I have build up a postgresql 9.2 database. My table looks like that:
CREATE DATABASE "EURUSD_M1"
WITH OWNER = fadmin
ENCODING = 'UTF8'
TABLESPACE = pg_default
LC_COLLATE = 'German_Germany.1252'
LC_CTYPE = 'German_Germany.1252'
CONNECTION LIMIT = -1;
However when trying to connect thorugh the rpostgresql driver I get:
> drv <- dbDriver("PostgreSQL") ## loads the PostgreSQL driver
> con <- dbConnect(drv, port='5432', dbname='EURUSD_M1',
+ user='fadmin') ## Open a connection
Error in postgresqlNewConnection(drv, ...) :
RS-DBI driver: (could not connect fadmin#local on dbname "EURUSD_M1"
)
btw to specify host = 'localhost' does not change anything! Also fadmin is a superuser in my db!
Here are further connection information:
Any ideas what I am doing wrong?
I appreciate your answers!
RPostgreSQL, just like the psql command-line tool, connect via tcp/ip network interface even when you have the server on the same machine.
For this to work, you must enable network access; see the various PostreSQL HOWTOs and guides. You probably have to edit / alter a file call pg_hba.conf or similar.

Resources