Before you manage data in a database, you must connect to the database. This topic describes how to connect to a database by using the psql tool.
Prerequisites
An AnalyticDB for PostgreSQL instance is created, and a privileged account is created for the instance. For more information, see Create a privileged account.
An IP address whitelist is configured for the instance. For more information, see Configure an IP address whitelist.
The internal or public IP address of the AnalyticDB for PostgreSQL instance is obtained.
If your client is deployed on an Elastic Compute Service (ECS) instance that resides in the same region and uses the same network type as the AnalyticDB for PostgreSQL instance, you can use the internal endpoint to connect to the AnalyticDB for PostgreSQL instance. Log on to the AnalyticDB for PostgreSQL console. In the Database Connection Information section of the Basic Information page, view the internal endpoint of the instance.
If your client is deployed on an ECS instance that resides in a different region or uses a different network type from your AnalyticDB for PostgreSQL instance or a system outside Alibaba Cloud, you must apply for a public endpoint and then use the public endpoint to connect to the AnalyticDB for PostgreSQL instance. For more information about how to apply for a public endpoint, see Manage public endpoints.
Install the psql tool
In this example, the psql tool is installed on an Elastic Compute Service (ECS) instance. The ECS instance runs on 64-bit CentOS 7.9.
Connect to the ECS instance. For more information, see Quick start for Linux instances.
Download the psql tool package.
wget https://docs-aliyun.cn-hangzhou.oss.aliyun-inc.com/assets/attach/181125/cn_zh/1598426198114/adbpg_client_package.el7.x86_64.tar.gz
The preceding command applies only to AnalyticDB for PostgreSQL V6.0 instances. The operating system of the client must be RHEL 7 or CentOS 7. If you want to use another version of psql, see the "psql" section of the Use client tools to connect to an instance topic.
Decompress the psql tool package.
tar -xzvf adbpg_client_package.el7.x86_64.tar.gz
Switch to the directory where the psql tool is located.
cd adbpg_client_package/bin
Connect to a database
Run the following command to connect to a database:
./psql -h <Endpoint of the AnalyticDB for PostgreSQL instance> -p 5432 -d <Name of the database to be connected> -U <Database account of the AnalyticDB for PostgreSQL instance>
Sample command:
./psql -h gp-bp13zq652yy4p****-master.gpdb.rds.aliyuncs.com -p 5432 -d postgres -U testuser
Press the Enter key and enter the password. If postgres=>
is displayed, the connection to the database is successful.