aws / aws/studio-lab-examples

Connecting to SQL DB over psycopg2 throws timeout

Open
#132 2 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Jupyter Notebook
Stars
772
Forks
229
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**
Connecting to SQL DB over `psycopg2` throws timeout.

This is however
- possible on the full product (SageMaker Studio),
- and also possible on the competitor Google Colab.

**To Reproduce**
Code to reproduce:
```py
import psycopg2

# Credentials
creds = {
"user" : 'root',
"password" : '*********',
"host" : '*******', # IP of server
"port" : "****", # port of DB
"database" : "**********",
"connect_timeout" : 1
}

# Query
query = f"""SELECT * FROM "table"""
print(query)

# Connect
connection = psycopg2.connect(**creds)

# Timeout
"""
ERROR: connection to server at "*******", port **** failed: timeout expired
"""
```

**Expected behavior**
The notebook should be able to at least create a `psycopg2.connection` object succesfully, to be able to retrieve data from external SQL databases.

This could point to a **security lock on the ports** (as also SageMaker Studio has limitations in this regard), but SageMaker Studio Lab **does not allow to check nor change this rules**.

**Desktop (please complete the following information):**
- SageMaker Studio Lab,
- running JupyterLab 3.4.3

**Additional context**
***Full traceback***
```py
---------------------------------------------------------------------------
OperationalError Traceback (most recent call last)
/tmp/ipykernel_76/3517768673.py in ()
----> 1 connection = psycopg2.connect(**creds)

~/.conda/envs/default/lib/python3.9/site-packages/psycopg2/__init__.py in connect(dsn, connection_factory, cursor_factory, **kwargs)
120
121 dsn = _ext.make_dsn(dsn, **kwargs)
--> 122 conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
123 if cursor_factory is not None:
124 conn.cursor_factory = cursor_factory

OperationalError: connection to server at "***********", port **** failed: timeout expired
```

Contributor guide

Open the contributing guide

Research direction

Start by running the provided psycopg2.connect reproduction in SageMaker Studio Lab and compare it with the stated SageMaker Studio and Google Colab behavior. No repository file or test is named; the issue is done only when outbound SQL database connectivity is confirmed as supported and working, or the platform limitation and workaround are documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
jupyter, python
Domain
cloud, databases, networking
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.