studio-lab-examples
studio-lab-examples copied to clipboard
Connecting to SQL DB over psycopg2 throws timeout
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:
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
---------------------------------------------------------------------------
OperationalError Traceback (most recent call last)
/tmp/ipykernel_76/3517768673.py in <cell line: 1>()
----> 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
Thanks for this detailed description. We will investigate.
I can confirm on my end that I have same timeout issue when trying to connect to a postgre DB with pscypog library