Unable to connect to postgres Cluster from python-Shell Script

0

Hello All,

I have created simple python-shell script as mentioned below code is running fine from my local system and I am also able to connect to cluster from my local system.

But when try running python-shell script as glue then getting following error:

import sys
import psycopg2


rds_host  = "hostname"
name = "aaaaaaa"
password = "XXXXXXX"
db_name = "bbb"
conn = psycopg2.connect(host=rds_host, user=name, password=password, dbname=db_name)

with conn.cursor() as cur:
   query="CALL test_vals()"
   cur.execute(query)
   conn.commit()
   cur.close()

cloudwatch error log

    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection timed out
	Is the server running on host "hostname" (XX.XXX.XX.XX) and accepting
	TCP/IP connections on port 5432?


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/tmp/runscript.py", line 215, in <module>

I have not added any Connections in job properties .

Please help .

asked a year ago437 views
1 Answer
1

Hi , the right answer depends on the configuration of the PostgreSQL database. is it hosted in a subnet (AWS VPC or on Prem) which is accessible via the internet? Do you have a firewall or a security group configured limiting access to a some CIDR ranges?

Without connection (not even a Network connection) the Glue Job would run outside a VPC so it may not have the right route to your cluster or it could be blocked by the Security Group/Firewall.

So, you need to use Connection (even a network connection ) it makes the glue job run in your VPC and thus easily define the right routing or setting for the Security Group. Please review the documentation here.

hope this helps

AWS
EXPERT
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions