Skip to content

Instantly share code, notes, and snippets.

@alexdebrie
Created December 9, 2024 15:26
Show Gist options
  • Save alexdebrie/4dcad46d603e865a92f1d097b08f2b41 to your computer and use it in GitHub Desktop.
Save alexdebrie/4dcad46d603e865a92f1d097b08f2b41 to your computer and use it in GitHub Desktop.
PSQL to DSQL
function dsql {
local auth_token
auth_token=$(aws dsql generate-db-connect-admin-auth-token \
--region us-east-1 \
--expires-in 3600 \
--hostname CLUSTER_ENDPOINT_URL) || { echo "Failed to generate auth token" >&2; return 1; }
if [ -z "$auth_token" ]; then
echo "Auth token is empty" >&2
return 1
fi
PGPASSWORD="$auth_token" PGSSLMODE=require psql \
--dbname postgres \
--username admin \
--host CLUSTER_ENDPOINT_URL
}
@alexdebrie
Copy link
Author

Throw this in your ~/.zshrc (or relevant shell config file) to easily connect to your DSQL instance with PSQL. It will grab an authentication token and use that to connect.

Be sure to replace 'CLUSTER_ENDPOINT_URL' with your actual endpoint URL.

If you need a certain AWS profile, set AWS_PROFILE= before aws dsql ... on line 3.

Adopted from the SQL client connection docs page here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment