# Connecting with psql

This guide explains how to connect to a **PostgreSQL** database using the **`psql`** command-line tool. It walks through the necessary setup, connection process, and execution of a simple SQL query.

### **Variables**

To connect to a PostgreSQL database, you only need one environment variable — the **connection URI**. This URI contains all the necessary information like username, password, host, port, and database name.

<div class="overflow-x-auto contain-inline-size" id="bkmrk-variable-description"><table border="1" data-end="1005" data-start="750" style="width: 100%;"><thead data-end="786" data-start="750"><tr data-end="786" data-start="750"><th data-end="761" data-start="750" style="width: 15.4918%;">Variable</th><th data-end="775" data-start="761" style="width: 40.4081%;">Description</th><th data-end="786" data-start="775" style="width: 44.1001%;">Purpose</th></tr></thead><tbody data-end="1005" data-start="824"><tr data-end="1005" data-start="824"><td style="width: 15.4918%;">**POSTGRESQL\_URI**</td><td style="width: 40.4081%;">Full PostgreSQL connection string (from the Elestio service overview page)</td><td style="width: 44.1001%;">Provides all necessary credentials and endpoint details in a single URI format.</td></tr></tbody></table>

</div>The URI will look like this:

```bash
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-03/scaled-1680-/ZvEimage.png)

### **Prerequisites**

While following this tutorial, you will need to have **`psql`** already installed; if not head over to [https://www.postgresql.org/download/](https://www.postgresql.org/download/) and download it first.

### **Connecting to PostgreSQL**

Open your terminal and run the following command to connect to your PostgreSQL database using the full connection URI:

```bash
psql POSTGRESQL_URI
```

<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary" id="bkmrk--1"><div class="sticky top-9"><div class="absolute bottom-0 right-0 flex h-9 items-center pr-2"></div></div></div>If the connection is successful, you’ll see output similar to this. Here it will show you the database you tried to connect to, which in this case is Elestio:

```
psql (17.4, server 16.8 (Debian 16.8-1.pgdg120+1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off, ALPN: none)
Type "help" for help.

Elestio=#
```

To ensure you're connected correctly, run this command inside the `psql` prompt:

```bash
SELECT version();
```

You should receive output like the following:

```
                                                       version                                                       
---------------------------------------------------------------------------------------------------------------------
 PostgreSQL 16.8 (Debian 16.8-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
(1 row)
```

<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary" id="bkmrk--2"><div class="overflow-y-auto p-4" dir="ltr"><div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary"><div class="sticky top-9"><div class="absolute bottom-0 right-0 flex h-9 items-center pr-2"><div class="flex items-center rounded bg-token-sidebar-surface-primary px-2 font-sans text-xs text-token-text-secondary dark:bg-token-main-surface-secondary">  
</div></div></div></div>  
</div></div>