dbConnect to Postgres gives strange error not allowing me to connect - r

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.

Related

Can't connect to Postgresql server from R

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?

Trouble accessing oracle database using R (odbc package)

A colleague at work is having trouble using the odbc package function. I am trying to find help.
He is using an oracle database using R instead of running our traditional SAS programs, but he has not been successful. We are trying to find out what is causing the error messages below. Can someone help?
Attempt 1:
#Get the Oracle JDBC driver
jdbcDriver =JDBC("oracle.jdbc.OracleDriver",
classPath="C:/instantclient_19_10/ojdbc8.jar")
Create connection string to the Database we want
connect.string <-
glue("jdbc:oracle:thin:#//{host}:{port}/{sid}",
host = "stdbprd01.states.bls.gov",
port = 1521,
sid = "lausonep")
print(connect.string)
#Establish connection to your database
con <- dbConnect(jdbcDriver,
connect.string,
user = "username",
password = rstudioapi::askForPassword("Database password"))
Error in .jcall(drv#jdrv, "Ljava/sql/Connection;", "connect", as.character(url)[1], :
java.sql.SQLRecoverableException: Listener refused the connection with the following error:
ORA-12514, TNS:listener does not currently know of service requested in connect descriptor
Attempt 2:
library(odbc)
con <- DBI::dbConnect(odbc::odbc(),
driver="Oracle in OraClient12Home1",
database="lausprd",
uid="aakre_n",
pwd="!QAZ1qaz#WSX2wsx",
host="stdbprd01.states.bls.gov",
port=1521
)
Error: nanodbc/nanodbc.cpp:1021: IM006: [Oracle][ODBC][ORA]-12560: TNS:protocol adapter error
[Microsoft][ODBC Driver Manager] Driver’s SQLSetConnectAttr failed

RODBC odbcDriverConnect() Connection Error in shiny R but not in Rstudio

I'm trying to use
odbcDriverConnect("driver={SQL
Server};server=IP;database=DBNAME;uid=username;pwd=password")
to connecting sql server database from shiny R,
but its result is -1.
I run the same code in R studio and get results normally, what's the problem?
For testing more I tried this one:
con <- dbConnect(odbc::odbc(),
Driver = "SQL Server",
Server = "IP",
Database = "dbname",
UID = "username",
PWD ="password",
Port = Port)
But again it runs in R studio but not in shiny and gets this error:
nanodbc/nanodbc.cpp:950: IM002: [unixODBC][Driver Manager]Data source name
not found, and no default driver specified

R- Connecting local postgresql to 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?

Connect to PostgreSQL using R (in windows)

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.

Resources