# Setting up a remote PostgreSQL connection

> Connect to your PostgreSQL database from your own computer through an SSH tunnel or a direct remote connection.

Source: https://helpcenter.ruachost.com/developers/databases/setting-up-a-remote-postgresql-connection/

> **PostgreSQL is available on Managed VPS only:**
Ruachost shared hosting supports MySQL (MariaDB) databases, not PostgreSQL. To use PostgreSQL, choose a [Managed VPS](https://ruachost.com/vps).

On **ruachost.com**, you can connect to your PostgreSQL databases remotely from your local computer. This allows you to manage data using client applications such as **psql**, **pgAdmin**, or programming libraries.

## ???? Important Prerequisite

Before you can establish a remote connection, you must **open a** [support ticket](https://dashboard.ruachost.com/submitticket.php?step=2&deptid=2) Provide:

- The name of the PostgreSQL database you want to access remotely.
- The database username.
- The IP address from which you will connect.

Ruachost support will then enable remote access for your database.

## Methods to Connect Remotely

### Method 1: Secure SSH Tunnel (Recommended)

An SSH tunnel encrypts traffic between your local computer and the PostgreSQL server.

#### On Windows (using PuTTY):

1. Download and open **PuTTY**.
2. In **Category → Connection → SSH → Tunnels**, set:

   - **Source port:** `5432` (or another local port if 5432 is already in use).
   - **Destination:** `localhost:5432`.
   - Select **Local** and **Auto**.
   - Click **Add**.
3. In **Session**, enter your server’s domain or IP, and set **Port** to `22` (or the custom SSH port, e.g., `7822`).
4. Save and open the session.
5. Log in with your Ruachost username and password.
6. Once connected, the tunnel is active. You can now use `psql` or pgAdmin locally with host `localhost` and port `5432`.

#### On macOS/Linux:

Run in terminal:

```bash
ssh -p 7822 username@example.com -L 5432:localhost:5432
```

- Replace `7822` with your SSH port if different.
- Replace `username` and `example.com` with your Ruachost credentials.

### Method 2: Direct Connection (Less Secure)

You can connect directly to the PostgreSQL server if remote access is enabled.

1. Open your PostgreSQL client (e.g., pgAdmin, psql).
2. Enter the following connection details:

   - **Host:** `yourserver.ruachost.com`
   - **Port:** `5432`
   - **Database name:** `dbname`
   - **Username:** `dbuser`
   - **Password:** `dbpassword`

## Example: Using `psql`

```bash
psql -U dbuser -h yourserver.ruachost.com -p 5432 dbname
```

> **Note:**
- If you run PostgreSQL locally, use a different port (e.g., `5433`) to avoid conflicts.
- Always prefer SSH tunnels for secure connections.
- Ensure firewalls allow outgoing connections on port `5432`.
- Use strong passwords for database accounts.
