Amazon Redshift will no longer support the use of Python UDFs after June 30, 2026.
We will start enforcing it in phases. For more information on the details of Python end of life
and migration options, see the
blog post
Attaching to Amazon Redshift
To run queries on Redshift from DuckDB, you first attach a Redshift
target with the ATTACH statement. Attaching registers the target in your
DuckDB session and gives it a short name, called a
handle (for example, rs). You then pass the handle to
redshift_query to run Redshift SQL on that target. An attach opens the
Redshift session for the DuckDB connection that runs it, so a wrong
target, database, credentials, network, or missing permission fails the attach. Other
DuckDB connections open their own session at their first
redshift_query.
A target is a Redshift Serverless workgroup, an Amazon Redshift provisioned cluster, or Amazon Redshift Serverless with express configuration. You name the target either by its endpoint in the path, or by options on an empty path.
Name exactly one credential source on each attach, with SECRET
, nameCREDENTIAL_PROVIDER
aws_default_chain, or CREDENTIAL_PROVIDER profile with
PROFILE '. The extension refuses an attach
that names no credential source, or more than one.name'
ATTACH statements written for DuckDB's aws
extension are also accepted, for compatibility. Write new statements in the forms shown
on this page.
An attach first checks the options, how they combine, and the shape of the path, and
refuses what is wrong at once, naming the handle. With SECRET, it also reads
the secret, and refuses one that doesn't exist, isn't an aws or
s3 secret, or holds no key ID or secret key. A missing AWS Region, when
neither REGION, the path, nor the secret gives one, is refused then
too.
The attach then opens the Redshift session for the DuckDB connection
that runs it. At that point it checks the AWS Region, the database, and a workgroup or
cluster name given as an option, along with the network, the credentials, and the
permissions. An error from a failed attach starts with Cannot attach
. Other DuckDB connections open
their own session at their first handleredshift_query.
Attaching with TYPE redshift
An attach gives a path, or an empty path with options. In this and the following examples, replace the endpoint, account ID, workgroup, cluster, and database with your own. The following example attaches a Redshift Serverless workgroup, by endpoint and by options:
-- By endpoint; the database is in the path. ATTACH 'redshift://my-workgroup.111122223333.us-east-1.redshift-serverless.amazonaws.com/dev' AS rs (TYPE redshift, SECRET aws_main); -- By options; REGION can also come from the aws secret. ATTACH '' AS rs_opts (TYPE redshift, SECRET aws_main, WORKGROUP 'my-workgroup', ACCOUNT_ID '111122223333', REGION 'us-east-1', DATABASE 'dev');
A workgroup URI connects to the port it gives, or to 5439 if it gives none. A
labelled workgroup endpoint,
redshift://,
attaches the same way, or by its long-form ARN,
workgroup.label.region.redshift-serverless.amazonaws.comarn:aws:redshift-serverless:.
Any other ARN is refused, such as one that names a workgroup or namespace by its ID,
or a cluster ARN.region:account-id:workgroup/label/workgroup
The following example attaches a provisioned cluster, by endpoint and by identifier. By endpoint, the handle connects to that host and port. By identifier, the extension finds the cluster's endpoint for you:
-- By endpoint: connect to exactly this host and port. ATTACH 'redshift://my-cluster.abc123def456.us-east-1.redshift.amazonaws.com:5439/dev' AS fin (TYPE redshift, SECRET aws_main); -- By identifier: the extension finds the cluster's endpoint for you. ATTACH '' AS fin_opts (TYPE redshift, SECRET aws_main, CLUSTER 'my-cluster', REGION 'us-east-1', DATABASE 'dev');
There are no HOST or PORT options, except a
HOST or PORT that repeats what the path gives. To pin a cluster's
host and port, give its endpoint as a redshift:// URI. A paused cluster
doesn't resume when you connect, so resume it first.
To attach Amazon Redshift Serverless with express configuration, give an empty path and no
workgroup, cluster, endpoint, or DATABASE. The Region comes from the
REGION option or from the secret. The following example attaches the express
configuration with the default credential chain:
ATTACH '' AS rs_express (TYPE redshift, CREDENTIAL_PROVIDER aws_default_chain, REGION 'us-east-1');
The workgroup and cluster examples name a secret. To attach without a secret, give
CREDENTIAL_PROVIDER aws_default_chain or CREDENTIAL_PROVIDER
profile instead:
-- The default credential chain. ATTACH 'redshift://my-workgroup.111122223333.us-east-1.redshift-serverless.amazonaws.com/dev' AS rs_chain (TYPE redshift, CREDENTIAL_PROVIDER aws_default_chain); -- A named profile. ATTACH 'redshift://my-workgroup.111122223333.us-east-1.redshift-serverless.amazonaws.com/dev' AS rs_profile (TYPE redshift, CREDENTIAL_PROVIDER profile, PROFILE 'analytics');
A profile in the AWS config file that uses credential_process works
with either credential source. With CREDENTIAL_PROVIDER profile and
PROFILE, a failed credential process fails the connection. A profile
selected through AWS_PROFILE under CREDENTIAL_PROVIDER
aws_default_chain falls through to the chain's other sources when its process
fails, so the connection can succeed as another identity, such as the host's
instance or container role. A profile that assumes a role works only when you name it
with PROFILE. Under aws_default_chain, the chain doesn't
assume the role, so the connection fails with a no usable AWS credentials
message or signs in as the host's own identity. Confirm the database user with
SELECT current_user.
The rules for an attach are as follows.
-
Name one credential source. Give
SECRET,CREDENTIAL_PROVIDER aws_default_chain, orCREDENTIAL_PROVIDER profilewithPROFILEon every attach, and exactly one of them. ASECRETmust name anawsors3secret. -
Name the handle with
AS. The empty path needsAS, because DuckDB would otherwise name the handlememory, which the extension refuses. GiveASwith a path too. -
Use one source for the target. With a path, the extension refuses
WORKGROUP,ACCOUNT_ID, andCLUSTER, and refusesDATABASEwhen the path names a database. -
Quote option values. Every value is a string. Write
ACCOUNT_ID '111122223333', with quotes. -
Keep credentials out of the path. The extension refuses a URI with user information. Credentials come only from the credential source the attach names.
-
The AWS Region. With a
redshift://endpoint or a long-form ARN, the AWS Region comes from the path, and aREGIONoption must agree with it. With the empty path, it comes fromREGION, or else from the secret. Only AWS Regions in theawspartition are supported, and a newly launched AWS Region needs a new release of the extension. -
External access must be on. While
enable_external_accessisfalse, you can't attach a new handle. Handles attached before keep working.
Checking, connecting, and detaching handles
To check your handles, query duckdb_databases():
SELECT database_name, path, type FROM duckdb_databases() WHERE type = 'redshift' ORDER BY 1;
Instead of calling redshift_query for each statement, you can connect to a
handle and type Redshift SQL straight to it. CONNECT
starts this mode, and
handleDISCONNECT returns to DuckDB. Each statement you type
while connected runs as one redshift_query call on that handle, so the
rules in Running queries with redshift_query apply.
The following SQL example connects to the handle, runs two statements, and disconnects:
CONNECT rs; SELECT current_database(), current_user; SHOW search_path; DISCONNECT;
Although connected, DuckDB parses only DISCONNECT, and
everything else goes to Redshift as text. A credential you type while connected, in
a CREATE SECRET, a SET, or an ATTACH, is sent
to Redshift, where it can enter the query history, so disconnect before you type
one. If the handle's connection can't open, each statement tries to open it again
and fails, and the DuckDB CLI shows #ERROR; run
DISCONNECT, fix the cause, then connect again. Only CONNECT
is supported, not a connection string.
handleCONNECT inside a DuckDB transaction is refused, and a
statement with bound parameters is refused while connected, so a client that binds
parameters, such as a JDBC PreparedStatement, uses
redshift_query with params instead.
A handle is not a browsable catalog. The extension refuses a reference such as
SELECT * FROM rs.schema.table. Query the target through the handle with
redshift_query. The same refusal applies to INSERT,
UPDATE, DELETE, CREATE SCHEMA, and CREATE
TABLE ... AS through the handle's catalog.
DETACH detaches the handle and
discards its temporary tables and open Redshift transactions. The following example
detaches the handle:handle
DETACH rs;