How to run Queries on Redshift Database using AWS CLI

I want to run queries by passing them as string to some supported command of AWS through its CLI.

I can see that the commands specific to AWS Redshift mentioned doesnt have anything which says that it can execute commands remotely

Link : https://docs.aws.amazon.com/cli/latest/reference/redshift/index.html

Need help on this.


You need to use psql. There is no API interface to redshift.

Redshift is based loosely on postgresql however so you can connect to the cluster using the psql command line tool.

https://docs.aws.amazon.com/redshift/latest/mgmt/connecting-from-psql.html


You can use Redshift Data API to execute queries on Redshift using AWS CLI.

aws redshift-data execute-statement 
    --region us-west-2 
    --secret arn:aws:secretsmanager:us-west-2:123456789012:secret:myuser-secret-hKgPWn 
    --cluster-identifier mycluster-test 
    --sql "select * from stl_query limit 1" 
    --database dev

https://aws.amazon.com/about-aws/whats-new/2020/09/announcing-data-api-for-amazon-redshift/ https://docs.aws.amazon.com/redshift/latest/mgmt/data-api.html https://docs.aws.amazon.com/cli/latest/reference/redshift-data/index.html#cli-aws-redshift-data