Importing files from PostgreSQL to R

前端 未结 3 640
日久生厌
日久生厌 2020-12-25 15:18

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.

I get this error:

Error in postgr         


        
相关标签:
3条回答
  • 2020-12-25 16:05

    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:

    1. Find and configure postgresql.conf to allow the TCP service to accept connections from any host, not only localhost

    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:

    /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

    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:

    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. Stop and start the server

    sudo service postgresql stop

    sudo service postgresql start

    4. Connect with you client, now it should work.

    GOOD LUCK!

    0 讨论(0)
  • 2020-12-25 16:08

    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:

    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
    
    0 讨论(0)
  • 2020-12-25 16:22
    drv <- dbDriver("PostgreSQL")
    con <- dbConnect(drv, host='localhost', port='5432', dbname='Swiss',
                     user='postgres', password='123456')
    

    Moreover, "RPostgreSQL" package in R should be installed.

    0 讨论(0)
提交回复
热议问题