简体   繁体   English

无法通过Putty连接R和PostgreSQL

[英]Cant connect R and PostgreSQL throught Putty

I have a problem with integration PostgreSQL and R. I always unload outputs from SQL queries into .txt file and then I download em into R using read.table() function. 我在集成PostgreSQL和R时遇到问题。我总是将SQL查询的输出卸载到.txt文件中,然后使用read.table()函数将它们下载到R中。 But now I need to get outputs from my queries directly in R. What I know about my SQL? 但是现在我需要直接在R中从查询中获取输出。我对SQL有什么了解? It's PostgreSQL , I use PuttY to connect to db Also I know such information from PuttY about my connection as 这是PostgreSQL ,我使用PuttY连接到db。

  1. Host name(or IP adress) 主机名(或IP地址)
  2. Port 港口
  3. saved session ='dbcenter' 保存的会话='dbcenter'
  4. ConnectionType = SSH ConnectionType = SSH
  5. key -- its file with .ppk extension 密钥-其文件扩展名为.ppk
  6. passphrase for this key 密钥密码

Also, before writing queries, I choose in opened window 另外,在编写查询之前,我在打开的窗口中选择

  1. region 区域
  2. databases 资料库

This is full information i know about putty and i have no idea how to write queries directly in R script. 这是我所了解的有关腻子的全部信息,我也不知道如何直接在R脚本中编写查询。 I tried RPostgreSQL package, no success. 我尝试了RPostgreSQL软件包,但没有成功。

Can somebody help me? 有人可以帮我吗?

Firstly, make sure you can connect to the remote DB without R, for example, using pgadmin3 or psql (see this link1 and link2 for more idea about pgadmin/psql/remote/port forwarding). 首先,请确保您可以不使用R而连接到远程数据库,例如,使用pgadmin3或psql(有关pgadmin / psql / remote / port转发的更多信息,请参阅此link1link2 )。

Then, try along these lines: 然后,按照以下方式尝试:

dbname <- "myname"; dbuser <- "myname"; dbpass <- "IWillNotTell";
dbhost <- "remotehostname"; dbport <- 5432;

If the remote host is withing the same local network (ie, your local host is myhost.host.edu), then, the following should work as well: 如果远程主机与同一本地网络连接(即,您的本地主机为myhost.host.edu),则以下内容也应正常工作:

dbhost <- "remote"

library(RPostgreSQL)
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, host=dbhost, port=dbport, dbname=dbname,
                 user=dbuser, password=dbpass)
query = 'SELECT * FROM TABLENAME'
dbGetQuery(con, query)
sqldata<-dbGetQuery(con, query)
summary(sqldata)

sqldata will be a dataframe sqldata将是一个数据框

The important thing is firewall and access to the DB. 重要的是防火墙和对数据库的访问。 I suggest, using pgadmin3 to make sure you can connect to the server. 我建议使用pgadmin3来确保您可以连接到服务器。

If the local firewall does not allow connection to the postgresql server port then you'll need to use ssh tunneling - the connection can get pretty slow depending on how remote the remote host is. 如果本地防火墙不允许连接到postgresql服务器端口,那么您将需要使用ssh隧道-连接的速度可能会变慢,具体取决于远程主机的远程程度。 For example: 例如:

assuming the sever allows listens to only "localhost" (this is wrt to the server), then: 假设服务器允许只侦听“ localhost”(这是服务器的密码),则:

ssh -L 22222:localhost:5432 myname@remotehostname ssh -L 22222:localhost:5432 myname @ remotehostname

will forward the 22222 port on your client to the remote's 5432 port. 会将客户端上的22222端口转发到遥控器的5432端口。 Again, use pgadmin3 at this point to make that you can connect. 同样,在这一点上使用pgadmin3使您可以连接。

Then set 然后设置

dbhost <- "localhost"; dbhost <-“本地主机”; dbport <- "22222" dbport <-“ 22222”

and use the same dbConnect call. 并使用相同的dbConnect调用。 Once the dbConnect call is successfull all sql results will be in R local 一旦dbConnect调用成功,所有的SQL结果将在R local中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM