Skip to main content

Connecting with Python

This guide explains how to connect a Python application to a KeyDB database using the redis library. It walks through the required setup, configuration, and execution of a simple KeyDB command.

Variables

To connect to KeyDB, the following parameters are needed. You can find these values in the Elestio KeyDB service overview.

Variable

Description

Purpose

HOST

KeyDB hostname (from Elestio service overview)

Address of the KeyDB server.

PORT

KeyDB port (from Elestio service overview)

Port used to connect to KeyDB. The default is 6379.

PASSWORD

KeyDB password (from Elestio service overview)

Authentication credential for the KeyDB connection.

These values can usually be found in the Elestio service overview details as shown in the image below, make sure to take a copy of these details and add it to the code moving ahead.

Screenshot 2025-06-26 at 1.13.23 PM.jpgPrerequisites

Install Python and pip

  • Check if Python is installed by running:

python3 --version
  • If not installed, download and install it from python.org.

  • Check pip (Python package installer):

pip --version

Install the redis Package

Install the official redis library using pip:

pip install redis

Code

Create a file named keydb.py and paste the following code:

import redis

config = {
    "host": "HOST",
    "port": PORT,  # Example: 6379
    "password": "PASSWORD",
    "decode_responses": True
}

try:
    client = redis.Redis(**config)
    client.set("testKey", "Hello KeyDB")
    value = client.get("testKey")
    print("Connected to KeyDB")
    print("Retrieved value:", value)

except redis.RedisError as err:
    print("KeyDB connection or operation failed:", err)

To execute the script, open the terminal or command prompt and navigate to the directory where keydb.py is located. Once in the correct directory, run the script with the command:

python3 redis.py

If everything is set up correctly, the output will be:

Connected to KeyDB  
Retrieved value: Hello KeyDB