PostgreSQL Databases: Connect To R and Python

PostgreSQL is a commonly used database language for creating and managing large amounts of data effectively.

Here, you will see how to:

  1. create a PostgreSQL database using the Linux terminal
  2. connect the PostgreSQL database to R using the “RpostgreSQL” library, and to Python using the “psycopg2” library

Creating our PostgreSQL database

In this example, we are going to create a simple database containing a table of dates, cities, and average temperature in degrees (Celsius).

We will name our database weather, and our table cities.

Once we open a Linux terminal, we enter the following:

This creates our weather database. We then enter \password to set a password:

Once prompted, we will enter a password rainbow, or whatever password you prefer

We then enter psql to initiate PostgreSQL and \conninfo to test the connection:

Create Table for Database

Once we have verified our connection, we can now create the table. As mentioned, we will name our table cities, and include the fields date, city_name, and averagetemp_celsius. We will define each variable as DATE, VARCHAR(50), and DECIMAL(7,2) respectively.

Once that’s done, we can then insert the appropriate values into our table:

Connect Python to Database using psycopg2

  1. We import our psycopg2 library, and enter our database credentials:
  1. Now, let’s execute two commands. Firstly, we wish to insert an extra row of data into our database from Python directly:
  1. Once we have done that, we can then display our data within the Python console:
  1. And, we see that our data is displayed in the Python console:
  1. In order to make the changes to the database permanent, we now commit our changes to the cities database:
  1. We have committed the necessary changes and can now close out our connection:

Connect R to database using RpostgreSQL

Firstly, we install the RpostgreSQL package:

Then, we define the database password within R:

We define our driver and set up the connection:

We verify that the “cities” table exists:

Once verified, we read our table into R using dbReadTable:

And there’s our table — we’ve successfully connected our PostgreSQL database to R! You might also find the following tutorial helpful in using PostgreSQL with Ubuntu.

dbSendQuery: Editing database directly from R

Now that we’ve created our database, what if we wish to edit directly from R? For instance, suppose that we have a new row of values that we would like to input. Instead of having to access the PostgreSQL database directly every time, we would like to send a query from R that does this for us.

Let us suppose that we wish to insert the following three values: ‘06/08/2017’, ‘Sao Paulo’, ‘16’.

To do this, we create a variable res that uses a dbSendQuery command to send an INSERT INTO query as follows:

Once we have sent the query, we now see that upon opening the associated data frame in R, our entry for Sao Paulo has been included and our query has been committed!


In this tutorial, you have learned how to:

  1. Create a database and table in PostgreSQL
  2. Connect a Python environment to a database using psycopg2
  3. Commit queries to the database and add additional entries

Further examples of Python and R applications can be found at

Michael Grogan (MGCodesandStats)

Written by

Machine Learning Consultant, Educator, and Speaker