我有一个大型数据集,我将在R软件中进行一些分析。 虽然我无法将数据正确导入R。
我收到此错误:
Error in postgresqlNewConnection(drv, ...) : RS-DBI driver: (could not connect User@local on dbname "Intel"
我使用PostgreSQL打开数据并以某种方式管理它。如何将PostgreSQL中的现有数据导入R软件?
答案 0 :(得分:24)
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, host='localhost', port='5432', dbname='Swiss',
user='postgres', password='123456')
此外,应安装R中的“RPostgreSQL”包。
答案 1 :(得分:7)
尝试R包RPostgreSQL http://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
答案 2 :(得分:0)
在能够远程连接之前,必须在PostgreSQL服务器上配置两件事。这是一个如何在Linux下配置它的说明:
find / -name&#34; postgresql.conf&#34;
在我的linux操作系统中,该文件位于/etc/postgresql/9.6/main/,所以我在那里修改它。添加以下行&#34; listen_addresses =&#39; *&#39;&#34; :
/etc/postgresql/9.6/main/postgresql.conf
#listen_addresses = 'localhost' # what IP address(es) to listen on;
# insert the following line
listen_addresses = '*'
sudo find / -name&#34; pg_hba.conf&#34;
在我的linux操作系统中,该文件位于/etc/postgresql/9.6/main/,所以我在那里修改它。添加&#34;主机全部0.0.0.0/0&#34; ,如下所示:
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
sudo service postgresql stop
sudo service postgresql start
好运!