从PostgreSQL导入文件到R

14

我有一个大型数据集,打算在R软件中进行一些分析。但是我无法正确地将数据导入R。

出现了这个错误:

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

我已经使用PostgreSQL打开数据并进行了一些处理。如何将现有的PostgreSQL数据导入到R软件中?


1
PL/R语言不支持这个吗?http://www.joeconway.com/plr/ - greg
如果您能解释一下导入时发生了什么,会很有帮助。如果可能的话,请展示具体的错误信息。我已经将您在评论中展示的一个错误信息复制到了问题中。 - kgrittn
1
老实说,我以前没有听说过PL/R。正如我所提到的,我有大量数据需要在R中分析,但是R处理不了。我已经使用Navicat和Pg-admin来准备数据。为了将准备好的数据导入R,我想使用“RPostgreSQL包”,根据描述,我使用了“drv <- dbDriver(“PostgreSQL”)”,然后“con <- dbConnect(drv,dbname =“ tempdb”)”,但代码无法运行,并出现以下错误。【Error in postgresqlNewConnection(drv, ...) : RS-DBI driver: (could not connect User@local on dbname "Intel"]。 - A.Amidi
3个回答

25
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, host='localhost', port='5432', dbname='Swiss',
                 user='postgres', password='123456')
此外,在R中需要安装“RPostgreSQL”包。

7
尝试使用R包RPostgreSQLhttp://cran.r-project.org/web/packages/RPostgreSQL/ 。 你可以在http://code.google.com/p/rpostgresql/中学习如何使用它。 例如:
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

1
其实,我之前见过这个错误,但我遇到了这个错误:“postgresqlNewConnection(drv, ...)中的错误: RS-DBI驱动程序:(无法连接User@local到数据库名“Intel”)。” - A.Amidi
我以前没有使用过PostgreSQL。我不知道它的问题在哪里。 - lf.xiao

0

在能够远程连接到PostgreSQL服务器之前,您必须在其上配置两个内容。以下是如何在Linux下进行配置的说明:

1. 查找并配置postgresql.conf以允许TCP服务接受来自任何主机的连接,而不仅仅是localhost

find / -name "postgresql.conf"

在我的Linux操作系统中,该文件位于/etc/postgresql/9.6/main/,因此我在那里进行了修改。添加以下行"listen_addresses = '*'"

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

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

2. 查找并配置pg_hba.conf以允许从任何主机连接客户端

sudo find / -name "pg_hba.conf"

在我的Linux操作系统中,该文件位于/etc/postgresql/9.6/main/,因此我在那里进行修改。按照以下方式添加"host all all 0.0.0.0/0"行:

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. 停止和启动服务器

sudo service postgresql stop

sudo service postgresql start

4. 现在可以连接客户端,应该可以正常工作了。

祝你好运!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接