1

I created an RDS instance (Postgres, free tier, in default VPC) and selected public access. Now, I am trying to connect to it in Python like this:

import psycopg2 as ps
# define credentials 
credentials = {'POSTGRES_ADDRESS' : '', # change to your endpoint
               'POSTGRES_PORT' : '', # change to your port
               'POSTGRES_USERNAME' : '', # change to your username
               'POSTGRES_PASSWORD' : '', # change to your password
               'POSTGRES_DBNAME' : ''} # change to your db name
# create connection and cursor    
conn = ps.connect(host=credentials['POSTGRES_ADDRESS'],
                  database=credentials['POSTGRES_DBNAME'],
                  user=credentials['POSTGRES_USERNAME'],
                  password=credentials['POSTGRES_PASSWORD'],
                  port=credentials['POSTGRES_PORT'])
cur = conn.cursor()

Here is the security group inbound: enter image description here

enter image description here

enter image description here

However, this times out. What am I doing wrong? I believe I put in all the credentials above, so maybe it is a problem with the AWS side of things? Please let me know if you need any more info.

Thanks!

  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/218086/discussion-on-question-by-ngi-cant-connect-to-public-rds-instance-in-python). – Samuel Liew Jul 18 '20 at 12:59

2 Answers2

4

You will need to allow inbound access on the Security Group:

  • Port 5432
  • Source: Your IP address (for good security)

It is best to create a new Security Group with these settings then modify the database to use this new security group. That way, it will not impact other systems, and future changes will not impact this security group.

See also: Change security group on AWS RDS Database Instance

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
  • I see, I followed the instruction from @Marcin 's screenshot, which should be similar to this setup. I gave it a shot again and it unfortunately didn't work. I didn't touch the RDS instance after editing the default security group. Do I need to do that? Thanks for your help, John!! –  Jul 18 '20 at 08:58
2

Based on the comments.

The issue turned out to be due to missing default database in the RDS.

Recreating the RDS with the default database solved the problem.

Marcin
  • 215,873
  • 14
  • 235
  • 294