# Connecting with Python

This guide explains how to connect a Python application to a Hydra database using the `<span class="s2">psycopg2-binary</span>` package. It covers environment setup, configuration, and execution of a simple query to test connectivity.

### **Variables**

To connect to a Hydra database, you only need <span class="s1">**one environment variable**</span> — the connection URI.

<table border="1" id="bkmrk-variable-description" style="border-collapse: collapse; border-color: rgb(0, 0, 0); width: 100%;"><thead><tr><th style="border-color: rgb(0, 0, 0); width: 11.7982%;">**Variable**

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

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

</th></tr></thead><tbody><tr><td style="border-color: rgb(0, 0, 0); width: 11.7982%;">`HYDRA_URI`

</td><td style="border-color: rgb(0, 0, 0); width: 50.4187%;">Full Hydra (PostgreSQL-compatible) connection string from the Elestio service overview

</td><td style="border-color: rgb(0, 0, 0); width: 37.7831%;">Provides all credentials and connection details in a single URI

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

A typical URI format looks like:

```
postgresql://<USER>:<PASSWORD>@<HOST>:<PORT>/<DATABASE>
```

You can find the details needed in the URI from the **Elestio service overview** details. Copy and replace the variables carefully in the URI example provided above.

[![image.png](https://docs.elest.io/uploads/images/gallery/2025-05/scaled-1680-/kHqimage.png)](https://docs.elest.io/uploads/images/gallery/2025-05/kHqimage.png)

### **Prerequisites**

**Install Python**

Check if Python is installed:

```
python --version
```

If not installed, download it from [https://python.org](https://python.org).

**Install** `<strong>psycopg2-binary</strong>`

Install the PostgreSQL driver for Python:

```
pip install psycopg2-binary
```

### **Code**

Once all prerequisites are set up, create a new file named `hydra.py` and add the following code and replace the `HYDRA_URI` with actual link or in environment setup as you wish:

```python
import psycopg2
import os

def get_db_version():
    try:
        # Use the Hydra URI from environment variable
        connection_uri = os.getenv('HYDRA_URI', 'POSTGRESQL_URI')
        db_connection = psycopg2.connect(connection_uri)
        db_cursor = db_connection.cursor()
        db_cursor.execute('SELECT VERSION()')
        db_version = db_cursor.fetchone()[0]
        return db_version

    except Exception as e:
        print(f"Database connection error: {e}")
        return None

    finally:
        if 'db_cursor' in locals():
            db_cursor.close()
        if 'db_connection' in locals():
            db_connection.close()

def display_version():
    version = get_db_version()
    if version:
        print(f"Connected to Hydra: {version}")

if __name__ == "__main__":
    display_version()
```

> 🔐 <span class="s1">**Tip:**</span> Save your URI in an <span class="s2">.env</span> file or set it in your terminal session like this:

```bash
export HYDRA_URI=postgresql://user:password@host:port/database
```

To execute the script, open the terminal or command prompt and navigate to the directory where `hydra.py`. Once in the correct directory, run the script with the command

```bash
python hydra.py
```

If the connection is successful, you’ll see:

```
Connected to Hydra: PostgreSQL 14.13 (Debian 14.13-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
```