For AI agents: the complete documentation index is available at https://docs.ovhcloud.com/pl/llms.txt, the full documentation bundle is available at https://docs.ovhcloud.com/pl/llms-full.txt, and this page is available as Markdown at https://docs.ovhcloud.com/pl/guides/public-cloud/databases/valkey-connect-python.md.

Connect using Python for Public Cloud Databases for Valkey

Pokaż jako Markdown

Connect to your Public Cloud Databases for Valkey using the Python programming language

Objective

Public Cloud Databases allow you to focus on building and deploying cloud applications while OVHcloud takes care of the database infrastructure and maintenance in operational conditions.

This guide explains how to connect to a Valkey service instance with one of the world's most famous programming language: Python.

Requirements

Concept

A Valkey service can be managed through multiple ways. One of the easiest, yet powerful, is to use a Command Line Interface (CLI), as shown in our guide: Connect with CLI.

Another way is to interact directly using programming languages, such as Python.

We will need to set up our Python environment with the valkey-py client, then code in Python to perform a few example actions.

Instructions

Set up your Python environment

To interact with your instance using Python, your development environment needs to be configured with:

  • A compatible version of Python (valkey-py 6.1 requires Python 3.9 or later)
  • valkey-py, the Python client of the Valkey project

Please follow the official valkey-py documentation to get the latest information.

Once your Python environment is set up, run python3 --version in your command line interface (CLI) to display its version, as shown below:

$ python3 --version
Python 3.12.0

In the same console, install valkey-py, then check that it is correctly installed:

$ pip install valkey
$ pip list
Package                Version
---------------------- -------
(...)
valkey                 6.1.1
(...)
Info

redis-py can also connect to your Valkey service, but valkey-py is the client maintained by the Valkey project.

We are now ready to learn how to connect to our instance!

Connect with valkey-py

Get the URI, or the host and port, of your service in the Dashboard tab, Connection information section, as explained in the Configure incoming connections guide.

TLS is mandatory: connections without TLS are refused. The server certificate is signed by a public certification authority, so valkey-py verifies it by default without any CA file. Do not disable this verification.

In your Python environment, let's try a connection. To be sure that we are indeed connected, set a data pair and then check we can get the value of it.

Create a valkey-connect.py file with the following content, and replace <username>, <password>, <host> and <port> with your own values. The valkeys:// scheme enables TLS:

import valkey

r = valkey.from_url("valkeys://<username>:<password>@<host>:<port>")
print(r.ping())
r.set("cache:greeting", "hello")
print(r.get("cache:greeting"))
r.delete("cache:greeting")

Run it:

$ python3 valkey-connect.py
True
b'hello'

You can also pass each parameter separately. With ssl=True, the connection uses TLS, and decode_responses=True returns strings instead of bytes:

import valkey

r = valkey.Valkey(
    host="<host>",
    port=<port>,
    username="<username>",
    password="<password>",
    ssl=True,
    decode_responses=True,
)
print(r.ping())
r.set("cache:greeting", "hello")
print(r.get("cache:greeting"))
r.delete("cache:greeting")
$ python3 valkey-connect.py
True
hello

Congratulations! Everything is working properly.

Tip

Avoid writing your password in your source code: read it from an environment variable (for example os.environ["VALKEY_PASSWORD"]) or from a secret manager.

If you get a NoPermissionError (NOPERM), the permissions of your user do not allow the key or the command used. In our example, the user must be allowed to read and write keys matching cache:*. Review its keys, categories and commands in the Users tab, as explained in the Configure incoming connections guide.

Go further

Valkey capabilities and limitations

valkey-py documentation

Join our Discord: Visit our dedicated Discord channel to ask questions, provide feedback, and interact directly with the team that builds our databases services.

For training or technical assistance implementing our solutions, contact your sales representative or visit our Professional Services page to request a quote and have your project analysed by our experts.

Join our community of users.

Czy ta strona była pomocna?