Updated on 2024-09-13 GMT+08:00
Using gsql to Connect to an Instance

Using gsql to Connect to an Instance

This section describes how to use the gsql client to connect to a instance you have bought on the management console.

Buying an ECS

If you want to connect to a database using the command-line interface (CLI), like gsql, you need to create an and install gsql on it.

  1. Log in to the management console and check whether there is an available .
    • If there is, go to 3.
    • If there is not, go to 2.
    Figure 1 ECS instances
  2. Buy an that runs EulerOS.

    For details about how to buy a Linux ECS, see Purchasing an ECS in Elastic Cloud Server Getting Started.

  3. On the ECS Information page of the target ECS, view the region and VPC of the ECS.
    Figure 2 ECS basic information

    The ECS must run EulerOS. gsql supports the following versions:

    For x86 servers: EulerOS V2.0SP5 and Kylin V10 SP2

    For Kunpeng servers: EulerOS V2.0SP8 and Kylin V10 SP1

  4. On the Basic Information page of your GaussDB instance, view the region and VPC of the instance.
    Figure 3 Basic information about a GaussDB instance

  5. Check whether the ECS and GaussDB instance are in the same region and VPC.

Querying the IP Address and Port Number of the Instance to Be Connected

  1. Log in to the management console.
  2. Click in the upper left corner and select a region and project.
  3. Click in the upper left corner of the page and choose Databases > GaussDB.
  4. On the Instances page, click the name of the target instance to go to the Basic Information page.
  5. In the Node List area and Network Information area, view the IP address and port number.
    • If the ECS and GaussDB instance are in the same VPC, obtain the private IP address and database port number.
    • If the ECS and GaussDB instance are in different VPCs, obtain the EIP and database port number.

Testing Connectivity

  1. Log in to the ECS. For details, see Logging In to a Linux ECS Using VNC in Elastic Cloud Server User Guide.
  2. On the ECS, check whether it can connect to the target GaussDB instance using the IP address and port number obtained in Querying the IP Address and Port Number of the Instance to Be Connected.

    telnet IP address Port number

    Example:

    telnet 192.168.0.16 8000

    If the message "command not found" is displayed, install a Telnet client that matches the OS of the ECS.

    • If the ECS can connect to the DB instance, no further action is required.
    • If the communication fails, check the security group rules.
      • On the Outbound Rules page of the ECS, add the IP address and port of the GaussDB instance to the outbound rules.
        • If the ECS and GaussDB instance are in the same VPC, add the private IP address and port of the GaussDB instance to the outbound rules.
        • If the ECS and GaussDB instance are in different VPCs, add the EIP address and port of the GaussDB instance to the outbound rules.
        Figure 4 ECS security group

      • On the Inbound Rules page of the GaussDB instance, add the IP address and port of the ECS to the inbound rules.
        • If the ECS and GaussDB instance are in the same VPC, add the private IP address and port of the ECS to the inbound rules.
        • If the ECS and GaussDB instance are in different VPCs, add the EIP address and port of the ECS to the inbound rules.
        For details, see Configuring Security Group Rules.
        Figure 5 GaussDB security group

Connecting to a Database

  • Non-SSL connection
    1. Log in as user root to the you have created.
    2. Upload the client tool package and configure gsql environment variables.
      1. Run the following command to create the /tmp/tools directory for storing the client tool package:
        mkdir /tmp/tools
      2. Download the GaussDB_driver.zip driver package of the required version by referring to #EN-US_TOPIC_0000001790772625/section45811975239, and upload it to the /tmp/tools directory of the created .
      3. Run the following commands to decompress the GaussDB_driver.zip driver package:
        cd /tmp/tools
        unzip GaussDB_driver.zip
      4. Run the following commands to copy the decompressed GaussDB-Kernel_***_EULER_64bit-Gsql.tar.gz client tool package to the /tmp/tools directory:

        This section uses the gsql tool package suitable for the primary/standby instances running on Euler2.5_x86_64 as an example. The relative path of the tool package varies depending on where you decompressed it.

        cd /tmp/tools/GaussDB_driver/Centralized/Euler2.5_X86_64/
        cp GaussDB-Kernel_***_EULER_64bit-Gsql.tar.gz /tmp/tools
      5. Run the following commands to decompress the package:
        cd /tmp/tools
        tar -zxvf GaussDB-Kernel_***_EULER_64bit-Gsql.tar.gz
      6. Configure environment variables.

        Run the following command to open the ~/.bashrc file:

        vim ~/.bashrc

        Press G to move the cursor to the last line and press i to enter Insert mode, and type the following information. Then, press Esc to exit Insert mode, and run :wq to save the settings and exit.

        export PATH=/tmp/tools/bin:$PATH
        export LD_LIBRARY_PATH=/tmp/tools/lib:$LD_LIBRARY_PATH
        Run the following command to make the environment variables take effect permanently:
        source ~/.bashrc
    3. Enter the password when prompted to connect to the database.

      After an instance is created, a postgres database is generated by default. Database postgres is used an example.

      gsql -d postgres -h 10.0.0.0 -U root -p 8000
      Password for user root:

      postgres is the name of the database you want to connect. 10.0.0.0 is the IP address of the instance obtained in Querying the IP Address of the Instance to Be Connected. root is the username for logging in to the database. 8000 is the database port obtained in Querying the Port Number of the Instance to Be Connected.

      For more information about gsql commands, see Tool Reference.

  • SSL connection
    1. Log in to the management console.
    2. Click in the upper left corner and select a region and project.
    3. Click in the upper left corner of the page and choose Databases > GaussDB.
    4. On the Instances page, click the name of the target instance. In the Configuration area on the Basic Information page, click next to the SSL field to download the root certificate or certificate bundle.
    5. Upload the root certificate to the or save it to the device to be connected to the instance.

      Import the root certificate to the Linux . For details, see How Can I Import the Root Certificate to a Windows or Linux OS?

    6. Connect to a instance.
      A Linux is used in this example. Run the following command to set environment variables on the :
      export PGSSLMODE=<sslmode>
      export PGSSLROOTCERT=<ca-file-directory>

      gsql -h <host> -p <port> -d <database> -U <user>

      Table 1 Parameters

      Parameter

      Description

      <host>

      IP address of the DB instance. To obtain this parameter, go to the Basic Information page of the DB instance. The IP address can be found in the IP Address column of the Node List area.

      <port>

      Database port in use. The default value is 8000. To obtain this parameter, go to the Basic Information page of the DB instance. The port number can be found in the Database Port field in the Network Information area.

      <database>

      Name of the database (postgres by default).

      <user>

      Username of the database account. The default administrator is root.

      <ca-file-directory>

      Path of the CA certificate for SSL connection.

      <sslmode>

      SSL connection mode. Set it to verify-ca to use a CA to check whether the service is trusted.

      For example, to connect to a postgres database through an SSL connection as user root, run the following commands on the :

      export PGSSLMODE="verify-ca"
      export PGSSLROOTCERT="/home/Ruby/ca.pem"

      gsql -d postgres -h 10.0.0.0 -U root -p 8000

      Password for user root:

      For more information about gsql commands, see Tool Reference.

    7. Check the command output after you log in to the database. If information similar to the following is displayed, the SSL connection has been established.
      SSL connection (cipher: DHE-RSA-AES256-GCM-SHA384, bits: 256)