Connecting from Python | Tarantool

Connecting from Python

Examples on GitHub: sample_db, python

The tutorial shows how to use the tarantool-python library to create a Python script that connects to a remote Tarantool instance, performs CRUD operations, and executes a stored procedure. You can find the full package documentation here: Python client library for Tarantool.


This tutorial shows how to make CRUD requests to a single-instance Tarantool database. To make requests to a sharded Tarantool cluster with the CRUD module, use the tarantool.crud module’s API.

This section describes the configuration of a sample database that allows remote connections:

      password: '123456'
      - permissions: [ read, write ]
        spaces: [ bands ]
      - permissions: [ execute ]
        functions: [ get_bands_older_than ]

              - uri: ''

  file: 'myapp.lua'
  • The configuration contains one instance that listens for incoming requests on the address.
  • sampleuser has privileges to select and modify data in the bands space and execute the get_bands_older_than stored function. This user can be used to connect to the instance remotely.
  • myapp.lua defines the data model and a stored function.

The myapp.lua file looks as follows:

-- Create a space --'bands')

-- Specify field names and types --{
    { name = 'id', type = 'unsigned' },
    { name = 'band_name', type = 'string' },
    { name = 'year', type = 'unsigned' }

-- Create indexes --'primary', { parts = { 'id' } })'band', { parts = { 'band_name' } })'year_band', { parts = { { 'year' }, { 'band_name' } } })

-- Create a stored function --
box.schema.func.create('get_bands_older_than', {
    body = [[
        return{ year }, { iterator = 'LT', limit = 10 })

You can find the full example on GitHub: sample_db.

Before creating and starting a client Python application, you need to run the sample_db application using tt start:

$ tt start sample_db

Now you can create a client Python application that makes requests to this database.

Before you start, make sure you have Python installed on your computer.

  1. Create the hello directory for your application and go to this directory:

    $ mkdir hello
    $ cd hello
  2. Create and activate a Python virtual environment:

    $ python -m venv .venv
    $ source .venv/bin/activate
  3. Install the tarantool module:

    $ pip install tarantool
  4. Inside the hello directory, create the file for application code.

In the file, import the tarantool package:

import tarantool

Add the following code:

# Connect to the database
conn = tarantool.Connection(host='',

This code establishes a connection to a running Tarantool instance on behalf of sampleuser. The conn object can be used to make CRUD requests and execute stored procedures.

Add the following code to insert four tuples into the bands space:

# Insert data
tuples = [(1, 'Roxette', 1986),
          (2, 'Scorpions', 1965),
          (3, 'Ace of Base', 1987),
          (4, 'The Beatles', 1960)]
print("Inserted tuples:")
for tuple in tuples:
    response = conn.insert(space_name='bands', values=tuple)

Connection.insert() is used to insert a tuple to the space.

To get a tuple by the specified primary key value, use

# Select by primary key
response ='bands', key=1)
print('Tuple selected by the primary key value:', response[0])

You can also get a tuple by the value of the specified index using the index argument:

# Select by secondary key
response ='bands', key='The Beatles', index='band')
print('Tuple selected by the secondary key value:', response[0])

Connection.update() can be used to update a tuple identified by the primary key as follows:

# Update
response = conn.update(space_name='bands',
                       op_list=[('=', 'band_name', 'Pink Floyd')])
print('Updated tuple:', response[0])

Connection.upsert() updates an existing tuple or inserts a new one. In the example below, a new tuple is inserted:

# Upsert
            tuple_value=(5, 'The Rolling Stones', 1962),
            op_list=[('=', 'band_name', 'The Doors')])

In this example, Connection.replace() deletes the existing tuple and inserts a new one:

# Replace
response = conn.replace(space_name='bands', values=(1, 'Queen', 1970))
print('Replaced tuple:', response[0])

Connection.delete() in the example below deletes a tuple whose primary key value is 5:

# Delete
response = conn.delete(space_name='bands', key=5)
print('Deleted tuple:', response[0])

To execute a stored procedure, use

# Call
response ='get_bands_older_than', 1966)
print('Stored procedure result:', response[0])

The Connection.close() method can be used to close the connection when it is no longer needed:

# Close connection
print('Connection is closed')


You can find the example with all the requests above on GitHub: python.

To run the resulting application, pass the script name to the python command:

$ python
Inserted tuples:
[1, 'Roxette', 1986]
[2, 'Scorpions', 1965]
[3, 'Ace of Base', 1987]
[4, 'The Beatles', 1960]
Tuple selected by the primary key value: [1, 'Roxette', 1986]
Tuple selected by the secondary key value: [4, 'The Beatles', 1960]
Updated tuple: [2, 'Pink Floyd', 1965]
Replaced tuple: [1, 'Queen', 1970]
Deleted tuple: [5, 'The Rolling Stones', 1962]
Stored procedure result: [[2, 'Pink Floyd', 1965], [4, 'The Beatles', 1960]]
Connection is closed

See the feature comparison table of all Python connectors available.

Found what you were looking for?