In R, I'm using the RPostgreSQL package to connect to my Postgres database (9.5.1). Here's the code:
library("RPostgreSQL")
con <- dbConnect(dbDriver("PostgreSQL"), dbname="mydb", host="localhost", port=5432, user="myuser")
This does not work, per this message:
Error in postgresqlNewConnection(drv, ...) :
RS-DBI driver: (could not connect myuser#localhost on dbname "mydb"
)
Calls: system.time ... .valueClassTest -> is -> is -> postgresqlNewConnection -> .Call Execution halted
However, if I explicitly put in the host address instead of localhost, it works, i.e.
library("RPostgreSQL")
con <- dbConnect(dbDriver("PostgreSQL"), dbname="mydb", host="15.2.52.1", port=5432, user="myuser")
I realize this is not much to go on, but I'd like to use localhost here. I'm working on Ubuntu 14.
Related
I´m trying to connect to a Postgresql localhost server from R using the following commands:
rm(list=ls())
library(RPostgreSQL)
host="localhost";port=5432;user="postgres";pw="postgres"
dbname="myBase"
drv = RPostgreSQL::PostgreSQL()#dbDriver("PostgreSQL")
con = dbConnect(drv, dbname = dbname,host = host, port = port,user = user, password = pw)
rm(pw)
When I run de code, I get the following:
"Error in postgresqlNewConnection(drv, ...) : RS-DBI driver: I_)"
For some reason I get those strange symbols instead of an error message.
The version of R is 4.1.3, and I'm using 14.2.1 version of Postgresql. No problems when running queries from pgadmin directly.
I've tried uninstalling and re-installing both R and Postgreql and still no work.
Any clues?
I am trying to connect to my localhost Postgres DB within R to further read/write tables. The server is running in the background which I'm monitoring in the background. I'm using the RPostgreSQL library.
pg <- dbDriver(drvName = "PostgreSQL")
con <- dbConnect(drv = pg, dbname = "test",
host = "localhost",
port = 5432,
user = "postgres",
password = "1234")
dbListTables(con)
Just running the dbConnect part gives a strange message reading:
ØJI)
Further running the dbListTables function gives:
Error in h(simpleError(msg, call)) :
error in evaluating the argument 'conn' in selecting a method for function 'dbWriteTable': object 'con' not found
I can't figure out what I am missing.
Edit: I think I'm not using the RPostgreSQL library correctly. Using RPostgres worked perfectly now.
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?
Using RPostgreSQL and getting an exception on dbConnect:
Error in postgresqlNewConnection(drv, ...) :
RS-DBI driver: (could not connect ...)
Calls: dbConnect -> dbConnect -> postgresqlNewConnection -> .Call
Execution halted
Looking for a way to catch the error reported by the database engine. dbGetexception requires a connection object, so its no use when the connection fails.
Is there a way to throw an error that reports what the error message from the DB engine is, so I know why the connection failed?
All that is needed to generate an error, either with an unavailable server, or bad user or DB name is:
library('RPostgreSQL')
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, host= "***", dbname = "***", user="***")
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.