14

I have a large data-set and I will preform some analysis in R software. While I could not import the data properly to R.

I get this error:

Error in postgresqlNewConnection(drv, ...) : RS-DBI driver: (could not connect User@local on dbname "Intel"

I have used PostgreSQL to open data and somehow manage it. How can I import the existing data in the PostgreSQL to the R software?

kgrittn
  • 18,113
  • 3
  • 39
  • 47
A.Amidi
  • 2,502
  • 5
  • 25
  • 37
  • 1
    Isn't that possible with the PL/R language ? http://www.joeconway.com/plr/ – greg Sep 19 '12 at 08:26
  • It would help if you explained what happens when you try to import it. If possible show specific error messages. I've copied the one error message you showed in a comment into the question. – kgrittn Sep 19 '12 at 11:45
  • 1
    Honestly, I have not hear PL/R before. As I mentioned I have large amount of data that should be analyzed in R but R could not handle it. I have used Navicat and Pg-admin to prepare data. In order to import prepared data to R, I want to use "RPostgreSQL package" and based on descriptions, I used "drv <- dbDriver("PostgreSQL")" and then "con <- dbConnect(drv, dbname="tempdb")" code did not run and following error arose. [Error in postgresqlNewConnection(drv, ...) : RS-DBI driver: (could not connect User@local on dbname "Intel"]. – A.Amidi Sep 19 '12 at 12:43

3 Answers3

25
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, host='localhost', port='5432', dbname='Swiss',
                 user='postgres', password='123456')

Moreover, "RPostgreSQL" package in R should be installed.

A.Amidi
  • 2,502
  • 5
  • 25
  • 37
7

Try the R package RPostgreSQL http://cran.r-project.org/web/packages/RPostgreSQL/ . You can see how to use it in http://code.google.com/p/rpostgresql/ . Example:

library(RPostgreSQL)
drv <- dbDriver("PostgreSQL")   ## loads the PostgreSQL driver
con <- dbConnect(drv, dbname="R_Project")   ## Open a connection 
rs <- dbSendQuery(con, "select * from R_Users")   ## Submits a statement
fetch(rs,n=-1)   ## fetch all elements from the result set
dbGetQuery(con, "select * from R_packages")   ## Submit and execute the query
dbDisconnect(con)   ## Closes the connection
dbUnloadDriver(drv)   # Frees all the resources on the driver
lf.xiao
  • 380
  • 1
  • 3
  • 1
    Actually, I had seen that before but i face by this error "Error in postgresqlNewConnection(drv, ...) : RS-DBI driver: (could not connect User@local on dbname "Intel"" – A.Amidi Sep 19 '12 at 09:36
  • I have not used PostgreSQL before. I don't know what is the problem with it. – lf.xiao Sep 19 '12 at 12:52
0

You have to configure two things on the PostgreSQL server before you are able to connect remotely. This is a instruction how to configure this under Linux:

1. Find and configure postgresql.conf to allow the TCP service to accept connections from any host, not only localhost

find / -name "postgresql.conf"

In my linux OS the file is locate in /etc/postgresql/9.6/main/, so I modify it there. Add the line "listen_addresses = '*'" as follows:

/etc/postgresql/9.6/main/postgresql.conf

#listen_addresses = 'localhost'         # what IP address(es) to listen on;
# insert the following line
listen_addresses = '*'

2. Find and configure pg_hba.conf to allow to connect with a client from any host

sudo find / -name "pg_hba.conf"

In my linux OS the file is locate in /etc/postgresql/9.6/main/, so I modify it there. Add the line "host all all 0.0.0.0/0" as follows:

sudo nano /etc/postgresql/9.6/main/pg_hba.conf

# Put your actual configuration here
# ----------------------------------
#
# If you want to allow non-local connections, you need to add more
# "host" records.  In that case you will also need to make PostgreSQL
# listen on a non-local interface via the listen_addresses
# configuration parameter, or via the -i or -h command line switches.
#
# insert the following line
host all all 0.0.0.0/0 trust

3. Stop and start the server

sudo service postgresql stop

sudo service postgresql start

4. Connect with you client, now it should work.

GOOD LUCK!

Community
  • 1
  • 1
NDB
  • 618
  • 1
  • 7
  • 16