Updated on 2025-04-29 GMT+08:00

Using CLI to Connect to a FlexusRDS for PostgreSQL Instance

Scenarios

You can connect to your DB instance using the psql command-line interface (CLI) from a FlexusX instance with the PostgreSQL client installed.

Connecting to a FlexusRDS for PostgreSQL Instance Through a Client (Version 15 and Earlier Versions)

  1. Log in to the FlexusX instance in the same region as your FlexusRDS DB instance.
  2. Install the PostgreSQL client.

    The PostgreSQL community provides client installation methods for different OSs. You can download and install the client using the installation tool of the OS. This installation method is simple but has requirements on the ECS OS. It is only available to the OSs supported by the PostgreSQL community.

    In this example, Linux CentOS 7 is used. Use the default installation tool of the OS to install the client (version 15 or earlier).

    Figure 1 Obtaining the installation tool

    Run the installation commands:

    sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
    sudo yum install -y postgresql15-server

    Check whether the installation is successful.

    psql -V
    Figure 2 Successful installation
  3. Run the following command on the FlexusX instance to connect to the FlexusRDS for PostgreSQL DB instance:

    psql --no-readline -U<user>-h<host>-p<port>-d<datastore>-W

    Example:

    psql --no-readline -U root -h 192.168.0.44 -p 5432 -d postgres -W
    Table 1 Parameter description

    Parameter

    Description

    <host>

    NOTE:

    If the FlexusX instance and the FlexusRDS for PostgreSQL DB instance are in the same region and VPC, you are advised to connect to the DB instance through a private network for higher security.

    If a DB instance cannot be accessed through a private network, connect to it through a public network.

    <port>

    5432

    <user>

    root

    <datastore>

    postgres

  4. When the following information is displayed, enter the password of user root:
    Enter password:

Connecting to a FlexusRDS for PostgreSQL Instance Using Source Code (No Restrictions on the Version)

  1. Log in to the FlexusX instance in the same region as your FlexusRDS DB instance.
  2. Install the PostgreSQL client.

    Installation from source code: This installation method has no restrictions on the RDS for PostgreSQL instance version and ECS OS.

    The following uses an ECS using the Huawei Cloud EulerOS 2.0 image as an example to describe how to install a PostgreSQL 16.4 client.

    Figure 3 Checking the ECS image
    1. To use SSL connection, download OpenSSL on the ECS in advance.
      sudo yum install -y openssl-devel
    2. Obtain the code download link, run wget to download the installation package to the ECS, or download the installation package to the local PC and then upload it to the ECS.
      wget https://ftp.postgresql.org/pub/source/v16.4/postgresql-16.4.tar.gz
    3. Decompress the installation package.
      tar xf postgresql-16.4.tar.gz
    4. Compile and install the client.
      cd postgresql-16.4
      ./configure --without-icu --without-readline --without-zlib --with-openssl
      make -j 8 && make install

      If --prefix is not specified, the default path is /usr/local/pgsql. The client can be installed in the simplest way.

      Figure 4 Compilation and installation
    5. Add the following code to the /etc/profile file to configure environment variables:
      export PATH=/usr/local/pgsql/bin:$PATH
      export LD_LIBRARY_PATH=/usr/local/pgsql/lib:$LD_LIBRARY_PATH
      source /etc/profile
    6. Test whether the psql is available.
      psql -V
      Figure 5 Testing psql
  3. Run the following command on the FlexusX instance to connect to the FlexusRDS for PostgreSQL DB instance:

    psql --no-readline -U<user>-h<host>-p<port>-d<datastore>-W

    Example:

    psql --no-readline -U root -h 192.168.0.44 -p 5432 -d postgres -W
    Table 2 Parameter description

    Parameter

    Description

    <host>

    NOTE:

    If the FlexusX instance and the FlexusRDS for PostgreSQL DB instance are in the same region and VPC, you are advised to connect to the DB instance through a private network for higher security.

    If a DB instance cannot be accessed through a private network, connect to it through a public network.

    <port>

    5432

    <user>

    root

    <datastore>

    postgres

  4. When the following information is displayed, enter the password of user root:
    Enter password: