1

I am running into an issue when attempting to connect to postgresDB. I have a Database URL with port # that I use to create a connection. I am using sqlalchemy python library to connect to postgres db.

My database URL is of the following format from the docs:

from sqlalchemy import create_engine
from sqlalchemy import inspect

# pg8000
engine = create_engine('postgresql+pg8000://postgres:***@hostname:5432/db_name') 

postgres_engine = create_engine(connection_string)

Traceback Error / Exception below:

InterfaceError: Can't create a connection to host xxxxxxx and port xxx (timeout is None and source_address is None).

The above exception was the direct cause of the following exception:

InterfaceError                            Traceback (most recent call last)
<ipython-input-10-18d2b71bc4ec> in <module>
----> 1 postgres_engine.table_names()

~/opt/anaconda3/lib/python3.7/site-packages/sqlalchemy/engine/base.py in table_names(self, schema, connection)
   2254         """
   2255 
-> 2256         with self._optional_conn_ctx_manager(connection) as conn:
   2257             return self.dialect.get_table_names(conn, schema)
   2258 

~/opt/anaconda3/lib/python3.7/contextlib.py in __enter__(self)
    110         del self.args, self.kwds, self.func
    111         try:
--> 112             return next(self.gen)
    113         except StopIteration:
    114             raise RuntimeError("generator didn't yield") from None

~/opt/anaconda3/lib/python3.7/site-packages/sqlalchemy/engine/base.py in _optional_conn_ctx_manager(self, connection)
   2038     def _optional_conn_ctx_manager(self, connection=None):
   2039         if connection is None:
-> 2040             with self._contextual_connect() as conn:
   2041                 yield conn
   2042         else:

~/opt/anaconda3/lib/python3.7/site-packages/sqlalchemy/engine/base.py in _contextual_connect(self, close_with_result, **kwargs)
   2240         return self._connection_cls(
   2241             self,
-> 2242             self._wrap_pool_connect(self.pool.connect, None),
   2243             close_with_result=close_with_result,
   2244             **kwargs

~/opt/anaconda3/lib/python3.7/site-packages/sqlalchemy/engine/base.py in _wrap_pool_connect(self, fn, connection)
   2278             if connection is None:
   2279                 Connection._handle_dbapi_exception_noconnection(
-> 2280                     e, dialect, self
   2281                 )
   2282             else:

~/opt/anaconda3/lib/python3.7/site-packages/sqlalchemy/engine/base.py in _handle_dbapi_exception_noconnection(cls, e, dialect, engine)
   1545             util.raise_from_cause(newraise, exc_info)
   1546         elif should_wrap:
-> 1547             util.raise_from_cause(sqlalchemy_exception, exc_info)
   1548         else:
   1549             util.reraise(*exc_info)

~/opt/anaconda3/lib/python3.7/site-packages/sqlalchemy/util/compat.py in raise_from_cause(exception, exc_info)
    396     exc_type, exc_value, exc_tb = exc_info
    397     cause = exc_value if exc_value is not exception else None
--> 398     reraise(type(exception), exception, tb=exc_tb, cause=cause)
    399 
    400 


InterfaceError: (pg8000.exceptions.InterfaceError) .
(Background on this error at: http://sqlalche.me/e/rvf5)
kms
  • 1,810
  • 1
  • 41
  • 92
  • Is postgres installed and running? Because I get this when it is not. – 5eb Dec 17 '20 at 11:54
  • Yep. I checked the docker container and also confirmed that the port is open. – kms Dec 17 '20 at 14:20
  • I don't see a port number specified in the connection `URL` so it would default to `5432`. Are you sure the server is listening on port `5432`? – Adrian Klaver Dec 17 '20 at 14:48
  • Yes, I am sure the port 5432 is open. – kms Dec 17 '20 at 16:57
  • Try adding the port number to the URL. Is there a firewall or some other network block between the Python client and the Docker container? The URL you show has the sample connection information from the docs, is the real thing connecting to correct host? Can you connect using `psql`? – Adrian Klaver Dec 17 '20 at 18:02
  • I updated the url. I cannot seem to connect using psql as well. I think I have got a hostname issue. – kms Dec 18 '20 at 07:15
  • Check this [article](https://stackoverflow.com/questions/38466190/cant-connect-to-postgresql-on-port-5432) out. Should solve your problem – SKhendle Apr 07 '22 at 03:17

0 Answers0