For AI agents: the complete documentation index is available at https://docs.us.ovhcloud.com/en/llms.txt, the full documentation bundle is available at https://docs.us.ovhcloud.com/en/llms-full.txt, and this page is available as Markdown at https://docs.us.ovhcloud.com/en/public-cloud/databases/postgresql-connect-cli.md.
  • English

PostgreSQL® - Connect with CLI

View as Markdown

Learn how to connect to a PostgreSQL® database instance with a Command Line Interface (CLI).

Cloud Databases allow you to focus on building and deploying cloud applications while OVHcloud takes care of the database infrastructure and maintenance in operational conditions.

 

Requirements

 

Concept

A PostgreSQL® instance can be managed in multiple ways. One of the easiest yet powerful ways is to use a Command Line Interface (CLI), also known as a Console or Terminal.

To interact correctly with the PostgreSQL® instance, we first need to install something called a PostgreSQL® client to connect and control the PostgreSQL® service remotely. It's a Client-Server interaction.

 

Instructions

Installing the PostgreSQL® client

Info

Please note that PostgreSQL®-Client is natively integrated into the PostgreSQL® software package.

You first need to install software to interact with your PostgreSQL® instance remotely. This official software can be installed on various client machines such as your own computer, a VPS, or a virtual machine. The only rule is to reach the public network (Internet) and have sufficient rights to install it.

To do this, and depending on your configuration, you may need to follow official PostgreSQL® documentation to install the PostgreSQL® client (referred to as psql).

Follow these steps after selecting Windows, macOS, or Linux as the operating system.

As explained, the postgresql-client is often included by default.

Example with Linux/Debian:

PostgreSQL cli - step 1

We will now follow official PostgreSQL® documentation to perform our first connection.

In your CLI, type psql --version. The result should look like this:

$ psql --version
psql (PostgreSQL) 13.4 (Ubuntu 13.4-1))

It means that psql is correctly installed and working properly. If you do not see something like this result, please go back to the previous step and reinstall the PostgreSQL® client.

 

Connect to your PostgreSQL® instance

We will follow the official PostgreSQL® documentation.

To perform a connection, simply type psql followed by the Service URI copied before:

$ psql "postgres://<username>:<password>@<hostname>:<port>/defaultdb?sslmode=require"

Don't forget that you need to modify the username, password, hostname, and port. In our example, it will look like this:

$ psql "postgre://avnadmin:Mysup3rs3cur3p4ssw0rd@postgresql-ab123456-cd7891011.database.cloud.ovh.us:20184/defaultdb?sslmode=require"

Once connected correctly, you should see something similar to the following:

psql (13.4 (Ubuntu 13.4-1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.

defaultdb=>
Tip

Congratulations! You are now fully able to interact with your PostgreSQL® instance!

 

PostgreSQL® client usage

Once connected, you can manage your PostgreSQL® instance with built-in PostgreSQL® client methods.

Please follow the official PostgreSQL® documentation.

To verify:

  • \h CREATE DATABASE will display help on a specific command (here for CREATE DATABASE);
  • \l+ will list all the databases;
  • select * from pg_user; will display information about existing users.

In our example, it will look like this:

defaultdb=> \h CREATE DATABASE
Command:     CREATE DATABASE
Description: create a new database
Syntax:
CREATE DATABASE name
    [ [ WITH ] [ OWNER [=] user_name ]
           [ TEMPLATE [=] template ]
           [ ENCODING [=] encoding ]
           [ LOCALE [=] locale ]
           [ LC_COLLATE [=] lc_collate ]
           [ LC_CTYPE [=] lc_ctype ]
           [ TABLESPACE [=] tablespace_name ]
           [ ALLOW_CONNECTIONS [=] allowconn ]
           [ CONNECTION LIMIT [=] connlimit ]
           [ IS_TEMPLATE [=] istemplate ] ]

URL: https://www.postgresql.org/docs/13/sql-createdatabase.html
defaultdb=> \l+
                                                                List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   |  Size   | Tablespace |            Description
-----------+----------+----------+-------------+-------------+-----------------------+---------+------------+------------------------------------
 _aiven    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |                       | 8269 kB | pg_default |
 defaultdb | avnadmin | UTF8     | en_US.UTF-8 | en_US.UTF-8 |                       | 12 MB   | pg_default |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +| 8253 kB | pg_default | unmodifiable empty database
           |          |          |             |             | postgres=CTc/postgres |         |            |
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +| 8229 kB | pg_default | default template for new databases
           |          |          |             |             | postgres=CTc/postgres |         |            |
(4 rows)
defaultdb=> select * from pg_user;
  usename  | usesysid | usecreatedb | usesuper | userepl | usebypassrls |  passwd  | valuntil | useconfig
-----------+----------+-------------+----------+---------+--------------+----------+----------+-----------
 postgres  |       10 | t           | t        | t       | t            | ******** |          |
 _aiven    |    16399 | f           | t        | t       | f            | ******** |          |
 avnadmin  |    16400 | t           | f        | t       | t            | ******** |          |
(3 rows)

 

Go further

Browse all the Databases guides.

Browse the rest of the OVHcloud US documentation for guides on our other products and services.

Learn more about Managed Databases on the OVHcloud US website.

OVHcloud Managed Databases and Analytics:

  • Grafana® is a registered trademark of Grafana Labs and is used with the permission of Grafana Labs. OVH SAS and its subsidiaries are not affiliated with or endorsed by Grafana Labs.
  • Kafka® is a registered trademark of The Apache Software Foundation and has been licensed for use by OVHcloud, who has no affiliation with and is not endorsed by The Apache Software Foundation.
  • MongoDB® is a registered trademark of MongoDB, Inc.
  • MySQL® is a registered trademark of Oracle and/or its affiliates.
  • PostgreSQL® is a registered trademark of the PostgreSQL Community Association of Canada.