

# 使用 PostgreSQL 交互式终端（psql）访问 Aurora DSQL
<a name="accessing-psql"></a>

## 使用 AWS CloudShell 通过 PostgreSQL 交互式终端（psql）访问 Aurora DSQL
<a name="accessing-sql-clients-psql"></a>

按照以下过程操作，使用 AWS CloudShell 通过 PostgreSQL 交互式终端访问 Aurora DSQL。有关更多信息，请参阅[什么是 AWS CloudShell](https://docs.aws.amazon.com/cloudshell/latest/userguide/welcome.html)。

**使用 AWS CloudShell 进行连接**

1. 登录 [Aurora DSQL 控制台](https://console.aws.amazon.com/dsql)。

1. 选择要在 CloudShell 中打开的集群。如果您尚未创建集群，请按照[步骤 1：创建 Aurora DSQL 单区域集群](getting-started.md#getting-started-create-cluster)或[创建多区域集群](getting-started.md#getting-started-multi-region)中的步骤操作。

1. 选择**使用查询编辑器进行连接**，然后选择**使用 CloudShell 进行连接**。

1. 选择是要以管理员身份还是要使用[自定义数据库角色](https://docs.aws.amazon.com/aurora-dsql/latest/userguide/authentication-authorization.html#authentication-authorization-iam-role-connect)进行连接。

1. 选择**在 CloudShell 中启动**，然后在以下 CloudShell 对话框中选择**运行**。

## 使用本地 CLI 通过 PostgreSQL 交互式终端（psql）访问 Aurora DSQL
<a name="accessing-sql-clients-psql-local"></a>

使用 `psql`（一款基于终端的 PostgreSQL 前端实用程序）可通过交互方式输入查询，将查询发送到 PostgreSQL，并查看查询结果。

**注意**  
要缩短查询响应时间，请使用 PostgreSQL 版本 17 客户端。如果您在不同的环境中使用 CLI，请务必手动设置 Python 版本 3.8\$1 和 psql 版本 14\$1。

从 [PostgreSQL Downloads](https://www.postgresql.org/download/) 页面下载操作系统的安装程序。有关 `psql` 的更多信息，请参阅 *PostgreSQL* 网站上的 [PostgreSQL 客户端应用程序](https://www.postgresql.org/docs/current/app-psql.htm)。

如果您已经安装了 AWS CLI，请使用以下示例连接到集群。

```
# Aurora DSQL requires a valid IAM token as the password when connecting.  
# Aurora DSQL provides tools for this and here we're using Python.  
export PGPASSWORD=$(aws dsql generate-db-connect-admin-auth-token \
  --region us-east-1 \
  --expires-in 3600 \
  --hostname your_cluster_endpoint)  
  
# Aurora DSQL requires SSL and will reject your connection without it.  
export PGSSLMODE=require  
  
# Connect with psql, which automatically uses the values set in PGPASSWORD and PGSSLMODE.  
# Quiet mode suppresses unnecessary warnings and chatty responses but still outputs errors.  
psql --quiet \
  --username admin \
  --dbname postgres \
  --host your_cluster_endpoint
```