Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
Help Center/ GaussDB/ User Guide/ GaussDB Instance Connection/ Using gsql to Connect to an Instance

Using gsql to Connect to an Instance

Updated on 2025-02-17 GMT+08:00

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

    NOTICE:

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

    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

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 the root user 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_***_EULER_64bit-Gsql.tar.gz client tool package to the /tmp/tools directory:
        NOTE:

        This section uses the gsql tool package suitable for the centralized 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, 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 as 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 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 the IP address, click the instance name on the Instances page to go to the Basic Information page of the instance. The IP address can be found in the Private 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 to be connected. The default management database is postgres.

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

      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)

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback