Help Center> GaussDB> Getting Started> Connecting to a DB Instance Using a Database Client> Using gsql to Connect to an Instance from a Linux Server
Updated on 2024-05-31 GMT+08:00

Using gsql to Connect to an Instance from a Linux Server

This section describes how to use the gsql client to connect to a GaussDB instance you have bought on the GaussDB 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 ECS and install gsql on it.

  1. Log in to the management console and check whether there is an available ECS.
    • If there is, go to 3.
    • If there is not, go to 2.
    Figure 1 ECS instances
  2. Buy an ECS 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 Connection 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 Login 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.
      • If Destination is not 0.0.0.0/0 and Protocol & Port is not All 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

      • If Source is not 0.0.0.0/0 and Protocol & Port is not All 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

Obtaining the Driver Package

Download particular packages listed in Table 1 based on the version of your instance.

To prevent a software package from being tampered with during transmission or storage, download the corresponding verification package and perform the following steps to verify the software package:

  1. Upload the software package and verification package to the same directory on a Linux VM.
  2. Run the following command to verify the integrity of the software package:

    cat GaussDB_driver.zip.sha256 | sha256sum --check

    If OK is displayed in the command output, the verification is successful.

    GaussDB_driver.zip: OK

Connecting to a Database

  • Non-SSL connection
    1. Log in as user root to the ECS 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 Obtaining the Driver Package, and upload it to the /tmp/tools directory of the created ECS.
      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_V***R***C**_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_V***R***C**_EULER_64bit-Gsql.tar.g /tmp/tools
      5. Run the following commands to decompress the package:
        cd /tmp/tools
        tar -zxvf GaussDB-Kernel_V***R***C**_EULER_64bit-Gsql.tar.g
      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 the database is installed, a database is generated. 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.

  • 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 DB Information 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 ECS or save it to the device to be connected to the GaussDB instance.

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

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

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

      Table 2 Parameters

      Parameter

      Description

      <host>

      IP address of the DB instance. To obtain this parameter, go to the Basic Information page of the DB instance. If the DB instance is accessed through an ECS, the IP address can be found in the Private IP Address field of the Connection Information 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 Connection Information area.

      <database>

      Name of the database (postgres by default).

      <user>

      Username of the GaussDB 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 ECS:

      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:
    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)

Helpful Links

For more information about gsql commands, see Tool Reference.