


To check if the connection is established, we can run the dbListTables(con)įunction that returns the list of the tables in our database.Īs you can see, no tables are stored in our database, so now it’s time to create one. ''ĭb_port <- '98939' # or any other port specified by the DBAĬon <- dbConnect(RPostgres::Postgres(), dbname = db, host=host_db, port=db_port, user=db_user, password=db_password) With the following steps you can set up the connection to a specific database:ĭb <- 'DATABASE' #provide the name of your db The Postgres package comes with the next command: con<-dbConnect(RPostgres::Postgres()) Establishing basic connection with the database using R Note: we used RPostgres package for establishing the connection. They both use a special DBI package in R that provides a wide range of methods and classes to establishing connection with DBs. The truth is, RPostgreSQL and RPostgres packages have no difference in the way they connect to the PostgreSQL database. The difference in these two ways is that in CRAN the latest stable version of a package is stored while on Github users can find the latest development version. To install package from CRAN, the next basic command is used: install.packages(‘RPostgres’) Then, for installing package, run remotes::install_github("r-dbi/RPostgres") To install the package from Github, first, devtools and remotes packages must be installed with the commands. The RPostgreSQL package is available on the CRAN, a Comprehensive R Archive Network, and is installed with the following command run in the IDE: install.packages('RPostgreSQL')Īs for the RPostgres package, it can be installed in two ways: cloning from Github and installing directly from CRAN.

Both of them provide great functionality for database interactions, the difference is only in the way of installation. Two main packages can be found in the library for connecting PostgreSQL in R environment: RPostgreSQL and RPostgres. Moreover, the package library is constantly growing, as the packages are set up and developed by the community. One of the great things about R language is that it has numerous packages for almost every kind of needs. Usually, R comes with an RStudio IDE, so that will be used while connecting and using PostgreSQL.

Besides, it allows creating and running emulations of the real-world data. R is an open source language for statistical and graphics data analysis providing scientists, statisticians, and academics powerful tools for various manipulations. In this article, we will go through connection and usage of PostgreSQL in R. That is why it is frequently used among all the areas of work where databases are involved. Among the variety of open source relational databases, PostgreSQL is probably one of the most popular due to its functional capacities.
