# Connecting to PostgreSQL using the command line

> Connect to a PostgreSQL database over SSH with the psql command-line client and run common psql commands.

Source: https://helpcenter.ruachost.com/developers/databases/connecting-to-postgresql-using-the-command-line/

> **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).

PostgreSQL provides a powerful interactive terminal called **psql** that allows you to connect to databases, run queries, and manage your data directly from the command line. On **ruachost.com**, you can use this method to quickly access and administer your PostgreSQL databases.

## Why Use the Command Line?

- Fast and direct access to your database.
- Ability to run SQL queries and administrative commands.
- Useful for scripting and automation.
- Essential for troubleshooting and advanced database management.

## Steps to Connect to PostgreSQL Using `psql`

### Step 1: Log in via SSH

- Use SSH to connect to your hosting account:

```bash
ssh username@yourserver.ruachost.com
```

### Step 2: Run the `psql` Command

- At the command line, type:

  ```bash
  psql dbname username
  ```

- Replace `dbname` with the name of your database.
- Replace `username` with your PostgreSQL username.

### Step 3: Enter Password

- At the password prompt, type the database user’s password.
- If correct, the **psql prompt** will appear.

## Common `psql` Commands

- ### View help for psql commands

  ```bash
  \?
  ```

- ### View help for SQL commands

  ```bash
  \h
  ```
- ### Show current database connection info

  ```bash
  \conninfo
  ```

- ### List tables and owners

  ```bash
  \dt
  ```

- ### List all tables, views, and sequences

  ```bash
  \z
  ```

- ### Exit psql

  ```bash
  \q
  ```

> **Important Notes:**
- Always back up your database before running administrative commands.
- Use secure passwords for PostgreSQL accounts.
- You can automate tasks by combining `psql` with shell scripts.
- For remote connections, ensure your PostgreSQL server is configured to accept external connections.
