简体   繁体   English

如何正确设置R以从PostgreSQL查询本地数据帧?

[英]How to properly setup R to query local data frames from PostgreSQL?

I've been using the sqldf package for quite a while using the SQLite drive, which came by default. 我使用sqldf包已经有一段时间了,它使用了默认的SQLite驱动器。

However, now I want to leverage PostgreSQL's window functions. 但是,现在我想利用PostgreSQL的窗口函数。 I've installed RPostgresSQL and I think I'm correctly using the options sqldf's documentation specifies. 我已经安装了RPostgresSQL,并且我认为我正确使用了sqldf文档指定的选项。 But even if I try to do a simple query specifying or not the drive and dbname, an error message is returned: 但是,即使我尝试执行一个简单的查询来指定是否指定驱动器和dbname,也会返回错误消息:

library(RPostgreSQL)
library(sqldf) # the R console returns "sqldf will default to using PostgreSQL"

df = data.frame(color=c("red","blue"),value=c(10,20))

sqldf("select * from df")

sqldf("select * from df",drv="PostgreSQL",dbname=getOption("sqldf.RPostgreSQL.dbname"))

The error message returned is the following: 返回的错误消息如下:

Error in postgresqlNewConnection(drv, ...) : RS-DBI driver: (could not connect postgres@localhost on dbname "test" ) Error in !dbPreExists : invalid argument type postgresqlNewConnection(drv,...)中的错误:RS-DBI驱动程序:(无法在dbname“ test”上连接postgres @ localhost)!dbPreExists中的错误:无效的参数类型

What am I missing? 我想念什么?

Ok I figure out how to make it work. 好的,我弄清楚了如何使其工作。 Following @akrun's comment, one has to create a "test" database, however, this is not enough. 按照@akrun的评论,必须创建一个“测试”数据库,但是,这还不够。 You have to also setup the following options in R before running any query in sqldf (got the solution here ) 您还必须在R中设置以下选项,然后才能在sqldf中运行任何查询(在此处获得解决方案)

options(sqldf.RPostgreSQL.user = "postgres", 
        sqldf.RPostgreSQL.password = "postgres",
        sqldf.RPostgreSQL.dbname = "test",
        sqldf.RPostgreSQL.host = "localhost", 
        sqldf.RPostgreSQL.port = 5432)

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

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