Search code examples
pythonamazon-web-servicessqlalchemyamazon-redshiftpsycopg2

How to connect Amazon Redshift to python


This is my python code and I want to connect my Amazon Redshift database to Python, but it is showing error in host.

Can anyone tell me the correct syntax? Am I passing all the parameters correctly?

con=psycopg2.connect("dbname = pg_table_def, host=redshifttest-icp.cooqucvshoum.us-west-2.redshift.amazonaws.com, port= 5439, user=me, password= secret")

This is the error:

OperationalError: could not translate host name "redshift://redshifttest-xyz.cooqucvshoum.us-west-2.redshift.amazonaws.com," to address: Unknown host


Solution

  • It appears that you wish to run Amazon Redshift queries from Python code.

    The parameters you would want to use are:

    • dbname: This is the name of the database you entered in the Database name field when the cluster was created.
    • user: This is you entered in the Master user name field when the cluster was created.
    • password: This is you entered in the Master user password field when the cluster was created.
    • host: This is the Endpoint provided in the Redshift management console (without the port at the end): redshifttest-xyz.cooqucvshoum.us-west-2.redshift.amazonaws.com
    • port: 5439

    For example:

    con=psycopg2.connect("dbname=sales host=redshifttest-xyz.cooqucvshoum.us-west-2.redshift.amazonaws.com port=5439 user=master password=secret")