

 Amazon Redshift 將不再支援從修補程式 198 開始建立新的 Python UDFs。現有 Python UDF 將繼續正常運作至 2026 年 6 月 30 日。如需詳細資訊，請參閱[部落格文章](https://aws.amazon.com/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/)。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 Amazon Redshift RSQL 連線至叢集
<a name="rsql-query-tool-starting-tool-connection"></a>

透過 Amazon Redshift，您可以使用 RSQL 連線到叢集並與其互動。這是一種命令列工具，提供了安全的方法來查詢資料、建立資料庫物件及管理 Amazon Redshift 叢集。下列各節將引導您完成在使用和不使用資料來源名稱 (DSN) 搭配 RSQL 的情況下，建立與叢集的連線的步驟。

## 在不使用 DSN 的情況下進行連線
<a name="rsql-query-tool-starting-tool-connection-dsn-less-example"></a>

1. 在 Amazon Redshift 主控台上，選擇要連線到的叢集，並記下端點、資料庫和連接埠。

1. 在命令提示字元中，使用命令列參數指定連線資訊。

   ```
   rsql -h <endpoint> -U <username> -d <databasename> -p <port>
   ```

    這裡會套用下列項目：
   +  *<endpoint>* 是您在前一步驟中記錄的 **Endpoint (端點)**。
   +  *<username>* 是有許可而可以連線至叢集的使用者名稱。
   +  *<databasename>* 是您在前一步驟中記錄的 **Database Name (資料庫名稱)**。
   +  *<port>* 是您在上一個步驟中記錄的**連接埠**。*<port>* 是選用參數。

   範例如下。

   ```
   rsql -h testcluster.example.amazonaws.com -U user1 -d dev -p 5439
   ```

1.  在密碼提示中，輸入 *<username>* 使用者的密碼。

   成功連線的回應看起來如下。

   ```
   % rsql -h testcluster.example.com -d dev -U user1 -p 5349
   Password for user user1:
   DSN-less Connected
   DBMS Name: Amazon Redshift
   Driver Name: Amazon Redshift ODBC Driver
   Driver Version: 1.4.27.1000
   Rsql Version: 1.0.1
   Redshift Version: 1.0.29306
   Type "help" for help.
   
   (testcluster) user1@dev=#
   ```

用來連線的命令在 Linux、Mac OS 和 Windows 上具有相同的參數。

## 在使用 DSN 的情況下進行連線
<a name="rsql-query-tool-starting-tool-connection-dsn-example"></a>

您可以使用 DSN 將 RSQL 連線至 Amazon Redshift，以簡化連線屬性的組織。本主題包含 ODBC 驅動器安裝的指示，以及 DSN 屬性的說明。

### 使用具有密碼的 DSN 連線
<a name="rsql-query-tool-starting-tool-connection-dsn-example-password"></a>

以下示範使用密碼的 DSN 連線組態範例。Mac OSX 的預設 `<path to driver>` 是 `/opt/amazon/redshift/lib/libamazonredshiftodbc.dylib`，Linux 的則是 `/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so`。

```
[testuser]
Driver=/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so
SSLMode=verify-ca
Min_TLS=1.2
boolsaschar=0
Host=<server endpoint>
Port=<database port>
Database=<dbname>
UID=<username>
PWD=<password>
sslmode=prefer
```

以下輸出來自成功的連線。

```
% rsql -D testuser
DSN Connected
DBMS Name: Amazon Redshift
Driver Name: Amazon Redshift ODBC Driver
Driver Version: 1.4.27.1000
Rsql Version: 1.0.1
Redshift Version: 1.0.29306
Type "help" for help.

(testcluster) user1@dev=#
```

### 使用單一登入 DSN
<a name="rsql-query-tool-starting-tool-connection-dsn"></a>

您可以設定 DSN 以進行單一登入身分驗證。以下示範使用 Okta 單一登入的 DSN 連線組態範例。

```
[testokta]
Driver=<path to driver>
SSLMode=verify-ca
Min_TLS=1.2
boolsaschar=0
Host=<server endpoint>
clusterid=<cluster id>
region=<region name>
Database=<dbname>
locale=en-US
iam=1
plugin_name=<plugin name>
uid=<okta username>
pwd=<okta password>
idp_host=<idp endpoint>
app_id=<app id>
app_name=<app name>
preferred_role=<role arn>
```

成功連線的範例輸出。

```
% rsql -D testokta 
DSN Connected
DBMS Name: Amazon Redshift
Driver Name: Amazon Redshift ODBC Driver
Driver Version: 1.4.27.1000
Rsql Version: 1.0.1
Redshift Version: 1.0.29306
Type "help" for help.

(testcluster) user1@dev=#
```

以下範例示範使用 Azure 單一登入的 DSN 連線組態範例。

```
[testazure]
Driver=<path to driver>
SSLMode=verify-ca
Min_TLS=1.2
boolsaschar=0
Host=<server endpoint>
Port=<cluster port>
clusterid=<cluster id>
region=<region name>
Database=<dbname>
locale=en-us
iam=1
plugin_name=<plugin name>
uid=<azure username>
pwd=<azure password>
idp_tenant=<Azure idp tenant uuid>
client_id=<Azure idp client uuid>
client_secret=<Azure idp client secret>
```

### 搭配 IAM 設定檔使用 DSN 連線
<a name="rsql-query-tool-starting-tool-connection-dsn-iam"></a>

您可以使用所設定的 IAM 設定檔連線到 Amazon Redshift。IAM 設定檔必須具有呼叫 `GetClusterCredentials` 的權限。以下範例示範要使用的 DSN 屬性。只有在 `Host` 不是 Amazon 提供的端點 (例如 `examplecluster.abc123xyz789.us-west-2.redshift.amazonaws.com`) 時，才需要 `ClusterID` 和 `Region` 參數。

```
[testiam]
Driver=Default
Host=testcluster.example.com
Database=dev
DbUser=testuser
ClusterID=rsqltestcluster
Region=us-east-1
IAM=1
Profile=default
```

`Profile` 金鑰的值是您從 CLI AWS 登入資料中選擇的具名設定檔。本範例示範名為 `default` 之設定檔的憑證。

```
$ cat .aws/credentials
[default]
aws_access_key_id = ASIAIOSFODNN7EXAMPLE 
aws_secret_access_key = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
```

以下示範連線回應。

```
$ rsql -D testiam
DSN Connected
DBMS Name: Amazon Redshift
Driver Name: Amazon Redshift ODBC Driver
Driver Version: 1.4.27.1000
Rsql Version: 1.0.1
Redshift Version: 1.0.29306
Type "help" for help.

(testcluster) testuser@dev=>
```

### 搭配執行個體設定檔使用 DSN 連線
<a name="rsql-query-tool-starting-tool-connection-dsn-instance"></a>

您可以使用 Amazon EC2 執行個體設定檔連線到 Amazon Redshift。執行個體設定檔必須具有呼叫 `GetClusterCredentials` 的權限。如需要使用的 DSN 屬性，請參閱以下範例。只有在 `Host` 不是 Amazon 提供的端點 (例如 `examplecluster.abc123xyz789.us-west-2.redshift.amazonaws.com`) 時，才需要 `ClusterID` 和 `Region` 參數。

```
[testinstanceprofile]
Driver=Default
Host=testcluster.example.com
Database=dev
DbUser=testuser
ClusterID=rsqltestcluster
Region=us-east-1
IAM=1
Instanceprofile=1
```

以下示範連線回應。

```
$ rsql -D testinstanceprofile
DSN Connected
DBMS Name: Amazon Redshift
Driver Name: Amazon Redshift ODBC Driver
Driver Version: 1.4.27.1000
Rsql Version: 1.0.1
Redshift Version: 1.0.29306
Type "help" for help.

(testcluster) testuser@dev=>
```

### 搭配使用 DSN 連線與預設憑證提供者鏈結
<a name="rsql-query-tool-starting-tool-connection-dsn-provider-chain"></a>

若要使用預設登入資料提供者鏈結進行連線，請僅指定 IAM 屬性，Amazon Redshift RSQL 將嘗試按照適用於 Java 的 AWS SDK 中的[使用登入資料中所述的順序取得 AWS 登入](https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html)資料。鏈結中至少要有一個提供者必須具有 `GetClusterCredentials` 許可。這對於從 ECS 容器進行連線 (舉例來說) 非常有用。

```
[iamcredentials]
Driver=Default
Host=testcluster.example.com
Database=dev
DbUser=testuser
ClusterID=rsqltestcluster
Region=us-east-1
IAM=1
```