将文件从 PostgreSQL 导入 R
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12490863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Importing files from PostgreSQL to R
提问by A.Amidi
I have a large data-set and I will preform some analysis in R software. While I could not import the data properly to R.
我有一个大数据集,我将在 R 软件中进行一些分析。虽然我无法将数据正确导入 R。
I get this error:
我收到此错误:
Error in postgresqlNewConnection(drv, ...) : RS-DBI driver: (could not connect User@local on dbname "Intel"
I have used PostgreSQL to open data and somehow manage it. How can I import the existing data in the PostgreSQL to the R software?
我使用 PostgreSQL 打开数据并以某种方式对其进行管理。如何将PostgreSQL中已有的数据导入R软件?
回答by A.Amidi
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, host='localhost', port='5432', dbname='Swiss',
user='postgres', password='123456')
Moreover, "RPostgreSQL" package in R should be installed.
此外,应该安装 R 中的“RPostgreSQL”包。
回答by lf.xiao
Try the R package RPostgreSQL http://cran.r-project.org/web/packages/RPostgreSQL/. You can see how to use it in http://code.google.com/p/rpostgresql/. Example:
试试 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
回答by NDB
You have to configure two things on the PostgreSQL server before you are able to connect remotely. This is a instruction how to configure this under Linux:
在能够远程连接之前,您必须在 PostgreSQL 服务器上配置两件事。这是如何在 Linux 下进行配置的说明:
1. Find and configure postgresql.conf to allow the TCP service to accept connections from any host, not only localhost
1.找到并配置postgresql.conf,允许TCP服务接受来自任何主机的连接,而不仅仅是localhost
find / -name "postgresql.conf"
find / -name "postgresql.conf"
In my linux OS the file is locate in /etc/postgresql/9.6/main/, so I modify it there. Add the line "listen_addresses = '*'"as follows:
在我的 linux 操作系统中,该文件位于 /etc/postgresql/9.6/main/ 中,因此我在那里对其进行了修改。添加“listen_addresses = '*'”行,如下所示:
/etc/postgresql/9.6/main/postgresql.conf
/etc/postgresql/9.6/main/postgresql.conf
#listen_addresses = 'localhost' # what IP address(es) to listen on;
# insert the following line
listen_addresses = '*'
2. Find and configure pg_hba.conf to allow to connect with a client from any host
2. 找到并配置 pg_hba.conf 以允许从任何主机连接客户端
sudo find / -name "pg_hba.conf"
sudo find / -name "pg_hba.conf"
In my linux OS the file is locate in /etc/postgresql/9.6/main/, so I modify it there. Add the line "host all all 0.0.0.0/0"as follows:
在我的 linux 操作系统中,该文件位于 /etc/postgresql/9.6/main/ 中,因此我在那里对其进行了修改。添加“host all all 0.0.0.0/0”行如下:
sudo nano /etc/postgresql/9.6/main/pg_hba.conf
须藤纳米 /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. Stop and start the server
3.停止和启动服务器
sudo service postgresql stop
sudo service postgresql start
须藤服务 postgresql 停止
须藤服务 postgresql 启动
4. Connect with you client, now it should work.
4. 与您的客户端连接,现在它应该可以工作了。
GOOD LUCK!
祝你好运!