我有一个大型数据集,打算在R软件中进行一些分析。但是我无法正确地将数据导入R。
出现了这个错误:
Error in postgresqlNewConnection(drv, ...) : RS-DBI driver: (could not connect User@local on dbname "Intel"
我已经使用PostgreSQL打开数据并进行了一些处理。如何将现有的PostgreSQL数据导入到R软件中?
我有一个大型数据集,打算在R软件中进行一些分析。但是我无法正确地将数据导入R。
出现了这个错误:
Error in postgresqlNewConnection(drv, ...) : RS-DBI driver: (could not connect User@local on dbname "Intel"
我已经使用PostgreSQL打开数据并进行了一些处理。如何将现有的PostgreSQL数据导入到R软件中?
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, host='localhost', port='5432', dbname='Swiss',
user='postgres', password='123456')
此外,在R中需要安装“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
在能够远程连接到PostgreSQL服务器之前,您必须在其上配置两个内容。以下是如何在Linux下进行配置的说明:
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 = '*'
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
sudo service postgresql stop
sudo service postgresql start
祝你好运!