0040. Connect to MonetDB from Python

Installation of Pymonetdb Module on Ubuntu 24.04

Python comes preinstalled on most of the Linux distributions. We can check version of our python with a command:

python3 --version             

For connection to MonetDB, python is using Pymonetdb module. Ubuntu's default Python (in /usr/bin/python3) is owned and maintained by APT. Ubuntu doesn't want users to use python installers, like "pip", because doing so could corrupt packages that Ubuntu's package manager relies on. This is done to avoid conflicts to packages installed by OS package manager.

For installation of the python packages that are not available in the Ubuntu's repository, we should use virtual environments. We will see bellow that Pymonetdb is provided by Ubuntu, so we don't have to use virtual environment.

For that reason, we will not install Pymonetdb using "pip" as we would normally do, but we will use apt package manager. In this way we can only install packages that are available in the Ubuntu's repository. First I will update the index of the available Ubuntu's repository packages, and I will search for Pymonetdb module.

sudo apt update
apt search pymonetdb

Modern Ubuntu only has Pymonetdb module that is meant to be used with the Python3.

We will install this module from the Ubuntu repository:

sudo apt install python3-pymonetdb

Pymonetdb module is now installed.

Installation of Pymonetdb Module in a Legacy Way

On the older versions of Ubuntu ( older than 23.04 ) we could install Pymonetdb with the "pip" installer. We already have python installed:

python3 --version           

Pip is a console program used for installing python modules. So, first we need to install pip, if we don't have it.

sudo apt install python3-pip                      

After installing pip, we will use it to install pymonetdb module:

pip install pymonetdb  

Pymonetdb module is installed.

Starting a Database

Before we try to connect to MonetDB, we must start our database:
monetdbd start /home/fffovde/DBfarm1
monetdb start voc

Installing of Spyder IDE on Ubuntu

Now we can try to connect to MonetDB from python. For that, I will type python commands into Spyder IDE. We have to first install Spyder IDE on Ubuntu.

sudo apt install spyder                  

We can then start Spyder from the graphical interface (1). This is how spyder looks like (2):

Spyder is a free and open source scientific environment for Python.

Python Script to Connect to MonetDB

Inside of Spyder IDE, I will add this script. This script will first create connection object. Using that connection object, we will create cursor object. Then we can use cursor object to execute our query.

import pymonetdb
connection = pymonetdb.connect(username="voc", password="voc", hostname="localhost", database="voc")
cursor = connection.cursor()
cursor.execute('SELECT * FROM voc.total')
[print( row ) for row in cursor.fetchall() ]

Result of our query will be list of tuples (like [(a,b,c),(1,2,3)] ), where each tuple is one row of a table. We will use list comprehension to print those rows one by one. At the end, Spyder console (1) will show us result.

Pymonetdb Help

If you want to learn more about pymonetdb, you can go to official documentation on this address:

https://pymonetdb.readthedocs.io/en/latest/index.html

Leave a Comment

Your email address will not be published. Required fields are marked *