How to connect to a cluster in Amazon Redshift using SQLAlchemy?

后端 未结 5 1293
既然无缘
既然无缘 2021-01-12 06:17

In Amazon Redshift\'s Getting Started Guide, it\'s mentioned that you can utilize SQL client tools that are compatible with PostgreSQL to connect to your Amazon Redshift Clu

5条回答
  •  误落风尘
    2021-01-12 06:25

    The following works for me with Databricks on all kinds of SQLs

      import sqlalchemy as SA
      import psycopg2
      host = 'your_host_url'
      username = 'your_user'
      password = 'your_passw'
      port = 5439
      url = "{d}+{driver}://{u}:{p}@{h}:{port}/{db}".\
                format(d="redshift",
                driver='psycopg2',
                u=username,
                p=password,
                h=host,
                port=port,
                db=db)
      engine = SA.create_engine(url)
      cnn = engine.connect()
    
      strSQL = "your_SQL ..."
      try:
          cnn.execute(strSQL)
      except:
          raise
    

提交回复
热议问题