【问题标题】:Importing files from PostgreSQL to R将文件从 PostgreSQL 导入到 R
【发布时间】:2012-09-19 08:17:51
【问题描述】:

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

我收到此错误:

postgresqlNewConnection(drv, ...) 中的错误:RS-DBI 驱动程序:(无法连接数据库名称“Intel”上的 User@local

我使用 PostgreSQL 打开数据并以某种方式对其进行管理。如何将PostgreSQL中已有的数据导入R软件?

【问题讨论】:

  • 用 PL/R 语言不可能吗? joeconway.com/plr
  • 如果您解释了尝试导入它时会发生什么,将会有所帮助。如果可能,显示特定的错误消息。我已将您在评论中显示的一条错误消息复制到问题中。
  • 老实说,我以前没有听过 PL/R。正如我提到的,我有大量数据应该在 R 中分析,但 R 无法处理。我使用 Navicat 和 Pg-admin 准备数据。为了将准备好的数据导入 R,我想使用“RPostgreSQL 包”,根据描述,我使用了“drv

标签: r postgresql


【解决方案1】:
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, host='localhost', port='5432', dbname='Swiss',
                 user='postgres', password='123456')

此外,应该安装 R 中的“RPostgreSQL”包。

【讨论】:

    【解决方案2】:

    试试 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
    

    【讨论】:

    • 实际上,我以前见过,但我遇到了这个错误“postgresqlNewConnection(drv, ...) 中的错误:RS-DBI 驱动程序:(无法连接数据库名称“Intel”上的 User@local "
    • 我之前没用过PostgreSQL。我不知道它有什么问题。
    【解决方案3】:

    在远程连接之前,您必须在 PostgreSQL 服务器上配置两件事。这是如何在 Linux 下进行配置的说明:

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

    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 停止

    sudo service postgresql 启动

    4。与您的客户连接,现在它应该可以工作了。

    祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-29
      • 2013-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多