# Connecting with Python

This guide explains how to connect a Python application to a Redis database using the [<span class="s3">redis</span>](https://pypi.org/project/redis/) library. It walks through the required setup, configuration, and execution of a simple Redis command.

## **Variables**

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

<table border="1" id="bkmrk-variable-description" style="width: 90.8333%; height: 119.188px; border-collapse: collapse; border-width: 1px; border-color: rgb(0, 0, 0);"><thead><tr style="height: 29.7969px;"><th style="width: 12.4672%; height: 29.7969px; border-color: rgb(0, 0, 0);">**Variable**

</th><th style="width: 42.3885%; height: 29.7969px; border-color: rgb(0, 0, 0);">**Description**

</th><th style="width: 45.2756%; height: 29.7969px; border-color: rgb(0, 0, 0);">**Purpose**

</th></tr></thead><tbody><tr style="height: 29.7969px;"><td style="width: 12.4672%; height: 29.7969px; border-color: rgb(0, 0, 0);">`HOST`

</td><td style="width: 42.3885%; height: 29.7969px; border-color: rgb(0, 0, 0);">Redis hostname (from Elestio service overview)

</td><td style="width: 45.2756%; height: 29.7969px; border-color: rgb(0, 0, 0);">Address of the Redis server.

</td></tr><tr style="height: 29.7969px;"><td style="width: 12.4672%; height: 29.7969px; border-color: rgb(0, 0, 0);">`PORT`

</td><td style="width: 42.3885%; height: 29.7969px; border-color: rgb(0, 0, 0);">Redis port (from Elestio service overview)

</td><td style="width: 45.2756%; height: 29.7969px; border-color: rgb(0, 0, 0);">Port used to connect to Redis. The default is <span class="s1">6379</span>.

</td></tr><tr style="height: 29.7969px;"><td style="width: 12.4672%; height: 29.7969px; border-color: rgb(0, 0, 0);">`PASSWORD`

</td><td style="width: 42.3885%; height: 29.7969px; border-color: rgb(0, 0, 0);">Redis password (from Elestio service overview)

</td><td style="width: 45.2756%; height: 29.7969px; border-color: rgb(0, 0, 0);">Authentication credential for the Redis connection.

</td></tr></tbody></table>

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-05-19 at 12.23.05 PM.jpg](https://docs.elest.io/uploads/images/gallery/2025-05/scaled-1680-/screenshot-2025-05-19-at-12-23-05-pm.jpg)](https://docs.elest.io/uploads/images/gallery/2025-05/screenshot-2025-05-19-at-12-23-05-pm.jpg)

## **Prerequisites**

**Install Python and pip**

- Check if Python is installed by running:

```
python3 --version
```

- If not installed, download and install it from [python.org](https://www.python.org).
- Check pip (Python package installer):

```
pip --version
```

**Install the redis Package**

Install the official <span class="s2">redis</span> library using pip:

```
pip install redis
```

## **Code**

Create a file named `<span class="s2">redis.py</span>` and paste the following code:

```python
import redis

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

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

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

To execute the script, open the terminal or command prompt and navigate to the directory where `<span class="s1">redis.py</span>` 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 Redis  
Retrieved value: Hello Redis
```