1

I am trying to run the aws redshift code inorder to connect the database using python. I am trying to run the given sample code given in the aws redshift documentation. Still I am getting some issue while running the code.

Code :

import numpy 
import redshift_connector
import socket
socket.getaddrinfo('localhost', 8080)

conn = redshift_connector.connect(
     host='examplecluster.abc123xyz789.us-west-1.redshift.amazonaws.com',
     database='dev',
     user='awsuser',
     password='my_password'
  )

cursor: redshift_connector.Cursor = conn.cursor()
cursor.execute("create Temp table book(bookname varchar,author‎ varchar)")
cursor.executemany("insert into book (bookname, author‎) values (%s, %s)",
                    [
                        ('One Hundred Years of Solitude', 'Gabriel García Márquez'),
                        ('A Brief History of Time', 'Stephen Hawking')
                    ]
                  )
cursor.execute("select * from book")

result: numpy.ndarray = cursor.fetch_numpy_array()
print(result)
cursor.execute("drop table book")
conn.commit()
conn.close()

Error :

enter image description here

starlord
  • 135
  • 9
  • Please provide a full error traceback. On which line you get an error? – 4d61726b Oct 25 '21 at 13:08
  • 2
    Welcome to Stack Overflow. [Please don't post screenshots of text](https://meta.stackoverflow.com/a/285557/4800344). They can't be searched or copied, or even consumed by users of adaptive technologies like screen readers. Instead, paste the code as text directly into your question. If you select it and click the `{}` button or press Ctrl+K, the code block will be indented by four spaces, which will cause it to be rendered as code. – Ermiya Eskandary Oct 25 '21 at 13:31
  • 1
    Why are you using `socket.getaddrinfo()`? – John Rotenstein Oct 25 '21 at 21:18
  • @JohnRotenstein I was searching this issue in stack overflow and came across this solution. However, it didn't work.. – starlord Oct 26 '21 at 04:22
  • @ErmiyaEskandary Sure I will take care of that next time. But I was unable to explore more on this since I was clueless about it. I'm exploring this connector for the first time. – starlord Oct 26 '21 at 04:24
  • @Mark I have updated the content. Please check it now. – starlord Oct 26 '21 at 04:25
  • Since the error is being generated by `socket.getaddrinfo()` and you/we can't see why that line is in the program, try removing it! – John Rotenstein Oct 26 '21 at 04:39
  • @JohnRotenstein I did but still I'm getting this same error. What could the issue? – starlord Oct 26 '21 at 09:10
  • It looks like your code is based on [Performing a basic connector test with NumPy integration - Amazon Redshift](https://docs.aws.amazon.com/redshift/latest/mgmt/python-basic-test-example.html) and [python - "getaddrinfo failed", what does that mean? - Stack Overflow](https://stackoverflow.com/questions/7334199/getaddrinfo-failed-what-does-that-mean). The second one merely _demonstrates_ the error, it does not show a way to _fix_ the error. The error is possibly because the DNS Name of the cluster in the `host` field could not be resolved to an IP address. Replace it with your own cluster URL. – John Rotenstein Oct 26 '21 at 09:55
  • @JohnRotenstein agreed but how to set up own cluster in the system for this program ? – starlord Oct 27 '21 at 11:44
  • That code is using an Amazon Redshift database. You would therefore need your own Amazon Redshift database (charges apply). You can launch one from the Redshift management console. It will then display the Endpoint for your database. – John Rotenstein Oct 27 '21 at 21:45

0 Answers0