# Connecting with PHP

This guide explains how to connect a PHP application to a Hydra database using the <span class="s1">**PDO extension**</span>. It covers setting up prerequisites, configuring the connection URI, and running a test SQL query.

### **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="width: 75.9524%; height: 76.3907px; border-collapse: collapse; border-color: rgb(0, 0, 0);"><thead><tr style="height: 29.7969px;"><th style="width: 16.1678%; height: 29.7969px; border-color: rgb(0, 0, 0);">**Variable**

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

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

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

</td><td style="width: 42.5457%; height: 46.5938px; border-color: rgb(0, 0, 0);">Full Hydra connection string from Elestio

</td><td style="width: 41.2878%; height: 46.5938px; border-color: rgb(0, 0, 0);">Encodes all connection info in one URI

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

A typical URI looks like this:

```
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-/eTcimage.png)](https://docs.elest.io/uploads/images/gallery/2025-05/eTcimage.png)

### **Prerequisites**

**Install PHP**

Check if PHP is installed:

```
php -v
```

If not, download and install PHP from: [https://www.php.net/downloads.php](https://www.php.net/downloads.php)

### **Code**

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

```php
<?php
$db_url = getenv("HYDRA_URI") ?: "postgresql://user:password@host:port/database";
$db_parts = parse_url($db_url);
$db_name = ltrim($db_parts['path'], '/');
$dsn = "pgsql:host={$db_parts['host']};port={$db_parts['port']};dbname={$db_name}";

try {
    $pdo = new PDO($dsn, $db_parts['user'], $db_parts['pass']);
    $version = $pdo->query("SELECT VERSION()")->fetchColumn();
    echo "Connected to Hydra: " . $version . PHP_EOL;
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage() . PHP_EOL;
}
```

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

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

Navigate to the directory containing `<span class="s2">hydra.php</span>` and run:

```
php hydra.php
```

If successful, you’ll see output like:

```
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
```