SQL data into Pandas#
Many companies store data in central databases to ensure efficient data management, security, and accessibility.
Structured Query Language (SQL) is the standard language for interacting with these databases, allowing users to extract and manipulate data. In recent years, Python has become a popular tool for data analysis and manipulation due to its flexibility, ease of use, and extensive libraries. Pandas is a powerful Python library for data manipulation, and it offers seamless integration with SQL databases through SQLAlchemy, a popular SQL toolkit for Python.
By using Python, Pandas, and SQLAlchemy, users can access and analyze data stored in SQL databases and perform complex queries and data transformations. In this article, we will explore how to connect to a SQL database, retrieve data using SQL queries, and analyze the data using Pandas.
How To#
import pandas as pd
import sqlalchemy as sql
conn = sql.create_engine("postgresql://scott:[email protected]/test")
df = pd.read_sql_table("sales", conn)
---------------------------------------------------------------------------
OperationalError Traceback (most recent call last)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/base.py:2339, in Engine._wrap_pool_connect(self, fn, connection)
2338 try:
-> 2339 return fn()
2340 except dialect.dbapi.Error as e:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:304, in Pool.unique_connection(self)
293 """Produce a DBAPI connection that is not referenced by any
294 thread-local context.
295
(...)
302
303 """
--> 304 return _ConnectionFairy._checkout(self)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:778, in _ConnectionFairy._checkout(cls, pool, threadconns, fairy)
777 if not fairy:
--> 778 fairy = _ConnectionRecord.checkout(pool)
780 fairy._pool = pool
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:495, in _ConnectionRecord.checkout(cls, pool)
493 @classmethod
494 def checkout(cls, pool):
--> 495 rec = pool._do_get()
496 try:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/impl.py:140, in QueuePool._do_get(self)
139 with util.safe_reraise():
--> 140 self._dec_overflow()
141 else:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py:68, in safe_reraise.__exit__(self, type_, value, traceback)
67 if not self.warn_only:
---> 68 compat.raise_(
69 exc_value, with_traceback=exc_tb,
70 )
71 else:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/util/compat.py:178, in raise_(***failed resolving arguments***)
177 try:
--> 178 raise exception
179 finally:
180 # credit to
181 # https://cosmicpercolator.com/2016/01/13/exception-leaks-in-python-2-and-3/
182 # as the __traceback__ object creates a cycle
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/impl.py:137, in QueuePool._do_get(self)
136 try:
--> 137 return self._create_connection()
138 except:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:309, in Pool._create_connection(self)
307 """Called by subclasses to create a new ConnectionRecord."""
--> 309 return _ConnectionRecord(self)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:440, in _ConnectionRecord.__init__(self, pool, connect)
439 if connect:
--> 440 self.__connect(first_connect_check=True)
441 self.finalize_callback = deque()
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:661, in _ConnectionRecord.__connect(self, first_connect_check)
660 with util.safe_reraise():
--> 661 pool.logger.debug("Error on connect(): %s", e)
662 else:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py:68, in safe_reraise.__exit__(self, type_, value, traceback)
67 if not self.warn_only:
---> 68 compat.raise_(
69 exc_value, with_traceback=exc_tb,
70 )
71 else:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/util/compat.py:178, in raise_(***failed resolving arguments***)
177 try:
--> 178 raise exception
179 finally:
180 # credit to
181 # https://cosmicpercolator.com/2016/01/13/exception-leaks-in-python-2-and-3/
182 # as the __traceback__ object creates a cycle
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:656, in _ConnectionRecord.__connect(self, first_connect_check)
655 self.starttime = time.time()
--> 656 connection = pool._invoke_creator(self)
657 pool.logger.debug("Created new connection %r", connection)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/strategies.py:114, in DefaultEngineStrategy.create.<locals>.connect(connection_record)
113 return connection
--> 114 return dialect.connect(*cargs, **cparams)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/default.py:493, in DefaultDialect.connect(self, *cargs, **cparams)
491 def connect(self, *cargs, **cparams):
492 # inherits the docstring from interfaces.Dialect.connect
--> 493 return self.dbapi.connect(*cargs, **cparams)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/psycopg2/__init__.py:122, in connect(dsn, connection_factory, cursor_factory, **kwargs)
121 dsn = _ext.make_dsn(dsn, **kwargs)
--> 122 conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
123 if cursor_factory is not None:
OperationalError: connection to server at "localhost" (::1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
The above exception was the direct cause of the following exception:
OperationalError Traceback (most recent call last)
Cell In[4], line 1
----> 1 df = pd.read_sql_table("sales", conn)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/pandas/io/sql.py:242, in read_sql_table(table_name, con, schema, index_col, coerce_float, parse_dates, columns, chunksize)
240 meta = MetaData(con, schema=schema)
241 try:
--> 242 meta.reflect(only=[table_name], views=True)
243 except sqlalchemy.exc.InvalidRequestError:
244 raise ValueError(f"Table {table_name} not found")
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/sql/schema.py:4438, in MetaData.reflect(self, bind, schema, views, only, extend_existing, autoload_replace, resolve_fks, **dialect_kwargs)
4435 if bind is None:
4436 bind = _bind_or_error(self)
-> 4438 with bind.connect() as conn:
4440 reflect_opts = {
4441 "autoload": True,
4442 "autoload_with": conn,
(...)
4446 "_extend_on": set(),
4447 }
4449 reflect_opts.update(dialect_kwargs)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/base.py:2266, in Engine.connect(self, **kwargs)
2251 def connect(self, **kwargs):
2252 """Return a new :class:`_engine.Connection` object.
2253
2254 The :class:`_engine.Connection` object is a facade that uses a DBAPI
(...)
2263
2264 """
-> 2266 return self._connection_cls(self, **kwargs)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/base.py:104, in Connection.__init__(self, engine, connection, close_with_result, _branch_from, _execution_options, _dispatch, _has_events)
99 self.schema_for_object = _branch_from.schema_for_object
100 else:
101 self.__connection = (
102 connection
103 if connection is not None
--> 104 else engine.raw_connection()
105 )
106 self.__transaction = None
107 self.__savepoint_seq = 0
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/base.py:2372, in Engine.raw_connection(self, _connection)
2350 def raw_connection(self, _connection=None):
2351 """Return a "raw" DBAPI connection from the connection pool.
2352
2353 The returned object is a proxied version of the DBAPI
(...)
2370
2371 """
-> 2372 return self._wrap_pool_connect(
2373 self.pool.unique_connection, _connection
2374 )
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/base.py:2342, in Engine._wrap_pool_connect(self, fn, connection)
2340 except dialect.dbapi.Error as e:
2341 if connection is None:
-> 2342 Connection._handle_dbapi_exception_noconnection(
2343 e, dialect, self
2344 )
2345 else:
2346 util.raise_(
2347 sys.exc_info()[1], with_traceback=sys.exc_info()[2]
2348 )
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/base.py:1584, in Connection._handle_dbapi_exception_noconnection(cls, e, dialect, engine)
1582 util.raise_(newraise, with_traceback=exc_info[2], from_=e)
1583 elif should_wrap:
-> 1584 util.raise_(
1585 sqlalchemy_exception, with_traceback=exc_info[2], from_=e
1586 )
1587 else:
1588 util.raise_(exc_info[1], with_traceback=exc_info[2])
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/util/compat.py:178, in raise_(***failed resolving arguments***)
175 exception.__cause__ = replace_context
177 try:
--> 178 raise exception
179 finally:
180 # credit to
181 # https://cosmicpercolator.com/2016/01/13/exception-leaks-in-python-2-and-3/
182 # as the __traceback__ object creates a cycle
183 del exception, replace_context, from_, with_traceback
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/base.py:2339, in Engine._wrap_pool_connect(self, fn, connection)
2337 dialect = self.dialect
2338 try:
-> 2339 return fn()
2340 except dialect.dbapi.Error as e:
2341 if connection is None:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:304, in Pool.unique_connection(self)
292 def unique_connection(self):
293 """Produce a DBAPI connection that is not referenced by any
294 thread-local context.
295
(...)
302
303 """
--> 304 return _ConnectionFairy._checkout(self)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:778, in _ConnectionFairy._checkout(cls, pool, threadconns, fairy)
775 @classmethod
776 def _checkout(cls, pool, threadconns=None, fairy=None):
777 if not fairy:
--> 778 fairy = _ConnectionRecord.checkout(pool)
780 fairy._pool = pool
781 fairy._counter = 0
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:495, in _ConnectionRecord.checkout(cls, pool)
493 @classmethod
494 def checkout(cls, pool):
--> 495 rec = pool._do_get()
496 try:
497 dbapi_connection = rec.get_connection()
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/impl.py:140, in QueuePool._do_get(self)
138 except:
139 with util.safe_reraise():
--> 140 self._dec_overflow()
141 else:
142 return self._do_get()
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py:68, in safe_reraise.__exit__(self, type_, value, traceback)
66 self._exc_info = None # remove potential circular references
67 if not self.warn_only:
---> 68 compat.raise_(
69 exc_value, with_traceback=exc_tb,
70 )
71 else:
72 if not compat.py3k and self._exc_info and self._exc_info[1]:
73 # emulate Py3K's behavior of telling us when an exception
74 # occurs in an exception handler.
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/util/compat.py:178, in raise_(***failed resolving arguments***)
175 exception.__cause__ = replace_context
177 try:
--> 178 raise exception
179 finally:
180 # credit to
181 # https://cosmicpercolator.com/2016/01/13/exception-leaks-in-python-2-and-3/
182 # as the __traceback__ object creates a cycle
183 del exception, replace_context, from_, with_traceback
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/impl.py:137, in QueuePool._do_get(self)
135 if self._inc_overflow():
136 try:
--> 137 return self._create_connection()
138 except:
139 with util.safe_reraise():
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:309, in Pool._create_connection(self)
306 def _create_connection(self):
307 """Called by subclasses to create a new ConnectionRecord."""
--> 309 return _ConnectionRecord(self)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:440, in _ConnectionRecord.__init__(self, pool, connect)
438 self.__pool = pool
439 if connect:
--> 440 self.__connect(first_connect_check=True)
441 self.finalize_callback = deque()
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:661, in _ConnectionRecord.__connect(self, first_connect_check)
659 except Exception as e:
660 with util.safe_reraise():
--> 661 pool.logger.debug("Error on connect(): %s", e)
662 else:
663 if first_connect_check:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py:68, in safe_reraise.__exit__(self, type_, value, traceback)
66 self._exc_info = None # remove potential circular references
67 if not self.warn_only:
---> 68 compat.raise_(
69 exc_value, with_traceback=exc_tb,
70 )
71 else:
72 if not compat.py3k and self._exc_info and self._exc_info[1]:
73 # emulate Py3K's behavior of telling us when an exception
74 # occurs in an exception handler.
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/util/compat.py:178, in raise_(***failed resolving arguments***)
175 exception.__cause__ = replace_context
177 try:
--> 178 raise exception
179 finally:
180 # credit to
181 # https://cosmicpercolator.com/2016/01/13/exception-leaks-in-python-2-and-3/
182 # as the __traceback__ object creates a cycle
183 del exception, replace_context, from_, with_traceback
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:656, in _ConnectionRecord.__connect(self, first_connect_check)
654 try:
655 self.starttime = time.time()
--> 656 connection = pool._invoke_creator(self)
657 pool.logger.debug("Created new connection %r", connection)
658 self.connection = connection
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/strategies.py:114, in DefaultEngineStrategy.create.<locals>.connect(connection_record)
112 if connection is not None:
113 return connection
--> 114 return dialect.connect(*cargs, **cparams)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/default.py:493, in DefaultDialect.connect(self, *cargs, **cparams)
491 def connect(self, *cargs, **cparams):
492 # inherits the docstring from interfaces.Dialect.connect
--> 493 return self.dbapi.connect(*cargs, **cparams)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/psycopg2/__init__.py:122, in connect(dsn, connection_factory, cursor_factory, **kwargs)
119 kwasync['async_'] = kwargs.pop('async_')
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: (psycopg2.OperationalError) connection to server at "localhost" (::1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
(Background on this error at: http://sqlalche.me/e/13/e3q8)
query = "SELECT * FROM sales"
df = pd.read_sql_query(query, conn)
---------------------------------------------------------------------------
OperationalError Traceback (most recent call last)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/base.py:2339, in Engine._wrap_pool_connect(self, fn, connection)
2338 try:
-> 2339 return fn()
2340 except dialect.dbapi.Error as e:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:364, in Pool.connect(self)
363 if not self._use_threadlocal:
--> 364 return _ConnectionFairy._checkout(self)
366 try:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:778, in _ConnectionFairy._checkout(cls, pool, threadconns, fairy)
777 if not fairy:
--> 778 fairy = _ConnectionRecord.checkout(pool)
780 fairy._pool = pool
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:495, in _ConnectionRecord.checkout(cls, pool)
493 @classmethod
494 def checkout(cls, pool):
--> 495 rec = pool._do_get()
496 try:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/impl.py:140, in QueuePool._do_get(self)
139 with util.safe_reraise():
--> 140 self._dec_overflow()
141 else:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py:68, in safe_reraise.__exit__(self, type_, value, traceback)
67 if not self.warn_only:
---> 68 compat.raise_(
69 exc_value, with_traceback=exc_tb,
70 )
71 else:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/util/compat.py:178, in raise_(***failed resolving arguments***)
177 try:
--> 178 raise exception
179 finally:
180 # credit to
181 # https://cosmicpercolator.com/2016/01/13/exception-leaks-in-python-2-and-3/
182 # as the __traceback__ object creates a cycle
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/impl.py:137, in QueuePool._do_get(self)
136 try:
--> 137 return self._create_connection()
138 except:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:309, in Pool._create_connection(self)
307 """Called by subclasses to create a new ConnectionRecord."""
--> 309 return _ConnectionRecord(self)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:440, in _ConnectionRecord.__init__(self, pool, connect)
439 if connect:
--> 440 self.__connect(first_connect_check=True)
441 self.finalize_callback = deque()
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:661, in _ConnectionRecord.__connect(self, first_connect_check)
660 with util.safe_reraise():
--> 661 pool.logger.debug("Error on connect(): %s", e)
662 else:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py:68, in safe_reraise.__exit__(self, type_, value, traceback)
67 if not self.warn_only:
---> 68 compat.raise_(
69 exc_value, with_traceback=exc_tb,
70 )
71 else:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/util/compat.py:178, in raise_(***failed resolving arguments***)
177 try:
--> 178 raise exception
179 finally:
180 # credit to
181 # https://cosmicpercolator.com/2016/01/13/exception-leaks-in-python-2-and-3/
182 # as the __traceback__ object creates a cycle
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:656, in _ConnectionRecord.__connect(self, first_connect_check)
655 self.starttime = time.time()
--> 656 connection = pool._invoke_creator(self)
657 pool.logger.debug("Created new connection %r", connection)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/strategies.py:114, in DefaultEngineStrategy.create.<locals>.connect(connection_record)
113 return connection
--> 114 return dialect.connect(*cargs, **cparams)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/default.py:493, in DefaultDialect.connect(self, *cargs, **cparams)
491 def connect(self, *cargs, **cparams):
492 # inherits the docstring from interfaces.Dialect.connect
--> 493 return self.dbapi.connect(*cargs, **cparams)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/psycopg2/__init__.py:122, in connect(dsn, connection_factory, cursor_factory, **kwargs)
121 dsn = _ext.make_dsn(dsn, **kwargs)
--> 122 conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
123 if cursor_factory is not None:
OperationalError: connection to server at "localhost" (::1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
The above exception was the direct cause of the following exception:
OperationalError Traceback (most recent call last)
Cell In[5], line 2
1 query = "SELECT * FROM sales"
----> 2 df = pd.read_sql_query(query, conn)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/pandas/io/sql.py:326, in read_sql_query(sql, con, index_col, coerce_float, params, parse_dates, chunksize)
271 """
272 Read SQL query into a DataFrame.
273
(...)
323 parameter will be converted to UTC.
324 """
325 pandas_sql = pandasSQL_builder(con)
--> 326 return pandas_sql.read_query(
327 sql,
328 index_col=index_col,
329 params=params,
330 coerce_float=coerce_float,
331 parse_dates=parse_dates,
332 chunksize=chunksize,
333 )
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/pandas/io/sql.py:1218, in SQLDatabase.read_query(self, sql, index_col, coerce_float, parse_dates, params, chunksize)
1176 """Read SQL query into a DataFrame.
1177
1178 Parameters
(...)
1214
1215 """
1216 args = _convert_params(sql, params)
-> 1218 result = self.execute(*args)
1219 columns = result.keys()
1221 if chunksize is not None:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/pandas/io/sql.py:1087, in SQLDatabase.execute(self, *args, **kwargs)
1085 def execute(self, *args, **kwargs):
1086 """Simple passthrough to SQLAlchemy connectable"""
-> 1087 return self.connectable.execute(*args, **kwargs)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/base.py:2237, in Engine.execute(self, statement, *multiparams, **params)
2217 def execute(self, statement, *multiparams, **params):
2218 """Executes the given construct and returns a
2219 :class:`_engine.ResultProxy`.
2220
(...)
2234
2235 """
-> 2237 connection = self._contextual_connect(close_with_result=True)
2238 return connection.execute(statement, *multiparams, **params)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/base.py:2305, in Engine._contextual_connect(self, close_with_result, **kwargs)
2302 def _contextual_connect(self, close_with_result=False, **kwargs):
2303 return self._connection_cls(
2304 self,
-> 2305 self._wrap_pool_connect(self.pool.connect, None),
2306 close_with_result=close_with_result,
2307 **kwargs
2308 )
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/base.py:2342, in Engine._wrap_pool_connect(self, fn, connection)
2340 except dialect.dbapi.Error as e:
2341 if connection is None:
-> 2342 Connection._handle_dbapi_exception_noconnection(
2343 e, dialect, self
2344 )
2345 else:
2346 util.raise_(
2347 sys.exc_info()[1], with_traceback=sys.exc_info()[2]
2348 )
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/base.py:1584, in Connection._handle_dbapi_exception_noconnection(cls, e, dialect, engine)
1582 util.raise_(newraise, with_traceback=exc_info[2], from_=e)
1583 elif should_wrap:
-> 1584 util.raise_(
1585 sqlalchemy_exception, with_traceback=exc_info[2], from_=e
1586 )
1587 else:
1588 util.raise_(exc_info[1], with_traceback=exc_info[2])
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/util/compat.py:178, in raise_(***failed resolving arguments***)
175 exception.__cause__ = replace_context
177 try:
--> 178 raise exception
179 finally:
180 # credit to
181 # https://cosmicpercolator.com/2016/01/13/exception-leaks-in-python-2-and-3/
182 # as the __traceback__ object creates a cycle
183 del exception, replace_context, from_, with_traceback
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/base.py:2339, in Engine._wrap_pool_connect(self, fn, connection)
2337 dialect = self.dialect
2338 try:
-> 2339 return fn()
2340 except dialect.dbapi.Error as e:
2341 if connection is None:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:364, in Pool.connect(self)
356 """Return a DBAPI connection from the pool.
357
358 The connection is instrumented such that when its
(...)
361
362 """
363 if not self._use_threadlocal:
--> 364 return _ConnectionFairy._checkout(self)
366 try:
367 rec = self._threadconns.current()
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:778, in _ConnectionFairy._checkout(cls, pool, threadconns, fairy)
775 @classmethod
776 def _checkout(cls, pool, threadconns=None, fairy=None):
777 if not fairy:
--> 778 fairy = _ConnectionRecord.checkout(pool)
780 fairy._pool = pool
781 fairy._counter = 0
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:495, in _ConnectionRecord.checkout(cls, pool)
493 @classmethod
494 def checkout(cls, pool):
--> 495 rec = pool._do_get()
496 try:
497 dbapi_connection = rec.get_connection()
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/impl.py:140, in QueuePool._do_get(self)
138 except:
139 with util.safe_reraise():
--> 140 self._dec_overflow()
141 else:
142 return self._do_get()
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py:68, in safe_reraise.__exit__(self, type_, value, traceback)
66 self._exc_info = None # remove potential circular references
67 if not self.warn_only:
---> 68 compat.raise_(
69 exc_value, with_traceback=exc_tb,
70 )
71 else:
72 if not compat.py3k and self._exc_info and self._exc_info[1]:
73 # emulate Py3K's behavior of telling us when an exception
74 # occurs in an exception handler.
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/util/compat.py:178, in raise_(***failed resolving arguments***)
175 exception.__cause__ = replace_context
177 try:
--> 178 raise exception
179 finally:
180 # credit to
181 # https://cosmicpercolator.com/2016/01/13/exception-leaks-in-python-2-and-3/
182 # as the __traceback__ object creates a cycle
183 del exception, replace_context, from_, with_traceback
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/impl.py:137, in QueuePool._do_get(self)
135 if self._inc_overflow():
136 try:
--> 137 return self._create_connection()
138 except:
139 with util.safe_reraise():
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:309, in Pool._create_connection(self)
306 def _create_connection(self):
307 """Called by subclasses to create a new ConnectionRecord."""
--> 309 return _ConnectionRecord(self)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:440, in _ConnectionRecord.__init__(self, pool, connect)
438 self.__pool = pool
439 if connect:
--> 440 self.__connect(first_connect_check=True)
441 self.finalize_callback = deque()
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:661, in _ConnectionRecord.__connect(self, first_connect_check)
659 except Exception as e:
660 with util.safe_reraise():
--> 661 pool.logger.debug("Error on connect(): %s", e)
662 else:
663 if first_connect_check:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py:68, in safe_reraise.__exit__(self, type_, value, traceback)
66 self._exc_info = None # remove potential circular references
67 if not self.warn_only:
---> 68 compat.raise_(
69 exc_value, with_traceback=exc_tb,
70 )
71 else:
72 if not compat.py3k and self._exc_info and self._exc_info[1]:
73 # emulate Py3K's behavior of telling us when an exception
74 # occurs in an exception handler.
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/util/compat.py:178, in raise_(***failed resolving arguments***)
175 exception.__cause__ = replace_context
177 try:
--> 178 raise exception
179 finally:
180 # credit to
181 # https://cosmicpercolator.com/2016/01/13/exception-leaks-in-python-2-and-3/
182 # as the __traceback__ object creates a cycle
183 del exception, replace_context, from_, with_traceback
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:656, in _ConnectionRecord.__connect(self, first_connect_check)
654 try:
655 self.starttime = time.time()
--> 656 connection = pool._invoke_creator(self)
657 pool.logger.debug("Created new connection %r", connection)
658 self.connection = connection
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/strategies.py:114, in DefaultEngineStrategy.create.<locals>.connect(connection_record)
112 if connection is not None:
113 return connection
--> 114 return dialect.connect(*cargs, **cparams)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/default.py:493, in DefaultDialect.connect(self, *cargs, **cparams)
491 def connect(self, *cargs, **cparams):
492 # inherits the docstring from interfaces.Dialect.connect
--> 493 return self.dbapi.connect(*cargs, **cparams)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/psycopg2/__init__.py:122, in connect(dsn, connection_factory, cursor_factory, **kwargs)
119 kwasync['async_'] = kwargs.pop('async_')
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: (psycopg2.OperationalError) connection to server at "localhost" (::1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
(Background on this error at: http://sqlalche.me/e/13/e3q8)
query = """SELECT customers, total_spend from sales
WHERE YEAR = 2019
LIMIT 1000
"""
df = pd.read_sql_query(query, conn)
---------------------------------------------------------------------------
OperationalError Traceback (most recent call last)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/base.py:2339, in Engine._wrap_pool_connect(self, fn, connection)
2338 try:
-> 2339 return fn()
2340 except dialect.dbapi.Error as e:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:364, in Pool.connect(self)
363 if not self._use_threadlocal:
--> 364 return _ConnectionFairy._checkout(self)
366 try:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:778, in _ConnectionFairy._checkout(cls, pool, threadconns, fairy)
777 if not fairy:
--> 778 fairy = _ConnectionRecord.checkout(pool)
780 fairy._pool = pool
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:495, in _ConnectionRecord.checkout(cls, pool)
493 @classmethod
494 def checkout(cls, pool):
--> 495 rec = pool._do_get()
496 try:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/impl.py:140, in QueuePool._do_get(self)
139 with util.safe_reraise():
--> 140 self._dec_overflow()
141 else:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py:68, in safe_reraise.__exit__(self, type_, value, traceback)
67 if not self.warn_only:
---> 68 compat.raise_(
69 exc_value, with_traceback=exc_tb,
70 )
71 else:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/util/compat.py:178, in raise_(***failed resolving arguments***)
177 try:
--> 178 raise exception
179 finally:
180 # credit to
181 # https://cosmicpercolator.com/2016/01/13/exception-leaks-in-python-2-and-3/
182 # as the __traceback__ object creates a cycle
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/impl.py:137, in QueuePool._do_get(self)
136 try:
--> 137 return self._create_connection()
138 except:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:309, in Pool._create_connection(self)
307 """Called by subclasses to create a new ConnectionRecord."""
--> 309 return _ConnectionRecord(self)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:440, in _ConnectionRecord.__init__(self, pool, connect)
439 if connect:
--> 440 self.__connect(first_connect_check=True)
441 self.finalize_callback = deque()
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:661, in _ConnectionRecord.__connect(self, first_connect_check)
660 with util.safe_reraise():
--> 661 pool.logger.debug("Error on connect(): %s", e)
662 else:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py:68, in safe_reraise.__exit__(self, type_, value, traceback)
67 if not self.warn_only:
---> 68 compat.raise_(
69 exc_value, with_traceback=exc_tb,
70 )
71 else:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/util/compat.py:178, in raise_(***failed resolving arguments***)
177 try:
--> 178 raise exception
179 finally:
180 # credit to
181 # https://cosmicpercolator.com/2016/01/13/exception-leaks-in-python-2-and-3/
182 # as the __traceback__ object creates a cycle
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:656, in _ConnectionRecord.__connect(self, first_connect_check)
655 self.starttime = time.time()
--> 656 connection = pool._invoke_creator(self)
657 pool.logger.debug("Created new connection %r", connection)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/strategies.py:114, in DefaultEngineStrategy.create.<locals>.connect(connection_record)
113 return connection
--> 114 return dialect.connect(*cargs, **cparams)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/default.py:493, in DefaultDialect.connect(self, *cargs, **cparams)
491 def connect(self, *cargs, **cparams):
492 # inherits the docstring from interfaces.Dialect.connect
--> 493 return self.dbapi.connect(*cargs, **cparams)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/psycopg2/__init__.py:122, in connect(dsn, connection_factory, cursor_factory, **kwargs)
121 dsn = _ext.make_dsn(dsn, **kwargs)
--> 122 conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
123 if cursor_factory is not None:
OperationalError: connection to server at "localhost" (::1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
The above exception was the direct cause of the following exception:
OperationalError Traceback (most recent call last)
Cell In[6], line 5
1 query = """SELECT customers, total_spend from sales
2 WHERE YEAR = 2019
3 LIMIT 1000
4 """
----> 5 df = pd.read_sql_query(query, conn)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/pandas/io/sql.py:326, in read_sql_query(sql, con, index_col, coerce_float, params, parse_dates, chunksize)
271 """
272 Read SQL query into a DataFrame.
273
(...)
323 parameter will be converted to UTC.
324 """
325 pandas_sql = pandasSQL_builder(con)
--> 326 return pandas_sql.read_query(
327 sql,
328 index_col=index_col,
329 params=params,
330 coerce_float=coerce_float,
331 parse_dates=parse_dates,
332 chunksize=chunksize,
333 )
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/pandas/io/sql.py:1218, in SQLDatabase.read_query(self, sql, index_col, coerce_float, parse_dates, params, chunksize)
1176 """Read SQL query into a DataFrame.
1177
1178 Parameters
(...)
1214
1215 """
1216 args = _convert_params(sql, params)
-> 1218 result = self.execute(*args)
1219 columns = result.keys()
1221 if chunksize is not None:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/pandas/io/sql.py:1087, in SQLDatabase.execute(self, *args, **kwargs)
1085 def execute(self, *args, **kwargs):
1086 """Simple passthrough to SQLAlchemy connectable"""
-> 1087 return self.connectable.execute(*args, **kwargs)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/base.py:2237, in Engine.execute(self, statement, *multiparams, **params)
2217 def execute(self, statement, *multiparams, **params):
2218 """Executes the given construct and returns a
2219 :class:`_engine.ResultProxy`.
2220
(...)
2234
2235 """
-> 2237 connection = self._contextual_connect(close_with_result=True)
2238 return connection.execute(statement, *multiparams, **params)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/base.py:2305, in Engine._contextual_connect(self, close_with_result, **kwargs)
2302 def _contextual_connect(self, close_with_result=False, **kwargs):
2303 return self._connection_cls(
2304 self,
-> 2305 self._wrap_pool_connect(self.pool.connect, None),
2306 close_with_result=close_with_result,
2307 **kwargs
2308 )
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/base.py:2342, in Engine._wrap_pool_connect(self, fn, connection)
2340 except dialect.dbapi.Error as e:
2341 if connection is None:
-> 2342 Connection._handle_dbapi_exception_noconnection(
2343 e, dialect, self
2344 )
2345 else:
2346 util.raise_(
2347 sys.exc_info()[1], with_traceback=sys.exc_info()[2]
2348 )
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/base.py:1584, in Connection._handle_dbapi_exception_noconnection(cls, e, dialect, engine)
1582 util.raise_(newraise, with_traceback=exc_info[2], from_=e)
1583 elif should_wrap:
-> 1584 util.raise_(
1585 sqlalchemy_exception, with_traceback=exc_info[2], from_=e
1586 )
1587 else:
1588 util.raise_(exc_info[1], with_traceback=exc_info[2])
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/util/compat.py:178, in raise_(***failed resolving arguments***)
175 exception.__cause__ = replace_context
177 try:
--> 178 raise exception
179 finally:
180 # credit to
181 # https://cosmicpercolator.com/2016/01/13/exception-leaks-in-python-2-and-3/
182 # as the __traceback__ object creates a cycle
183 del exception, replace_context, from_, with_traceback
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/base.py:2339, in Engine._wrap_pool_connect(self, fn, connection)
2337 dialect = self.dialect
2338 try:
-> 2339 return fn()
2340 except dialect.dbapi.Error as e:
2341 if connection is None:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:364, in Pool.connect(self)
356 """Return a DBAPI connection from the pool.
357
358 The connection is instrumented such that when its
(...)
361
362 """
363 if not self._use_threadlocal:
--> 364 return _ConnectionFairy._checkout(self)
366 try:
367 rec = self._threadconns.current()
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:778, in _ConnectionFairy._checkout(cls, pool, threadconns, fairy)
775 @classmethod
776 def _checkout(cls, pool, threadconns=None, fairy=None):
777 if not fairy:
--> 778 fairy = _ConnectionRecord.checkout(pool)
780 fairy._pool = pool
781 fairy._counter = 0
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:495, in _ConnectionRecord.checkout(cls, pool)
493 @classmethod
494 def checkout(cls, pool):
--> 495 rec = pool._do_get()
496 try:
497 dbapi_connection = rec.get_connection()
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/impl.py:140, in QueuePool._do_get(self)
138 except:
139 with util.safe_reraise():
--> 140 self._dec_overflow()
141 else:
142 return self._do_get()
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py:68, in safe_reraise.__exit__(self, type_, value, traceback)
66 self._exc_info = None # remove potential circular references
67 if not self.warn_only:
---> 68 compat.raise_(
69 exc_value, with_traceback=exc_tb,
70 )
71 else:
72 if not compat.py3k and self._exc_info and self._exc_info[1]:
73 # emulate Py3K's behavior of telling us when an exception
74 # occurs in an exception handler.
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/util/compat.py:178, in raise_(***failed resolving arguments***)
175 exception.__cause__ = replace_context
177 try:
--> 178 raise exception
179 finally:
180 # credit to
181 # https://cosmicpercolator.com/2016/01/13/exception-leaks-in-python-2-and-3/
182 # as the __traceback__ object creates a cycle
183 del exception, replace_context, from_, with_traceback
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/impl.py:137, in QueuePool._do_get(self)
135 if self._inc_overflow():
136 try:
--> 137 return self._create_connection()
138 except:
139 with util.safe_reraise():
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:309, in Pool._create_connection(self)
306 def _create_connection(self):
307 """Called by subclasses to create a new ConnectionRecord."""
--> 309 return _ConnectionRecord(self)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:440, in _ConnectionRecord.__init__(self, pool, connect)
438 self.__pool = pool
439 if connect:
--> 440 self.__connect(first_connect_check=True)
441 self.finalize_callback = deque()
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:661, in _ConnectionRecord.__connect(self, first_connect_check)
659 except Exception as e:
660 with util.safe_reraise():
--> 661 pool.logger.debug("Error on connect(): %s", e)
662 else:
663 if first_connect_check:
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py:68, in safe_reraise.__exit__(self, type_, value, traceback)
66 self._exc_info = None # remove potential circular references
67 if not self.warn_only:
---> 68 compat.raise_(
69 exc_value, with_traceback=exc_tb,
70 )
71 else:
72 if not compat.py3k and self._exc_info and self._exc_info[1]:
73 # emulate Py3K's behavior of telling us when an exception
74 # occurs in an exception handler.
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/util/compat.py:178, in raise_(***failed resolving arguments***)
175 exception.__cause__ = replace_context
177 try:
--> 178 raise exception
179 finally:
180 # credit to
181 # https://cosmicpercolator.com/2016/01/13/exception-leaks-in-python-2-and-3/
182 # as the __traceback__ object creates a cycle
183 del exception, replace_context, from_, with_traceback
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/pool/base.py:656, in _ConnectionRecord.__connect(self, first_connect_check)
654 try:
655 self.starttime = time.time()
--> 656 connection = pool._invoke_creator(self)
657 pool.logger.debug("Created new connection %r", connection)
658 self.connection = connection
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/strategies.py:114, in DefaultEngineStrategy.create.<locals>.connect(connection_record)
112 if connection is not None:
113 return connection
--> 114 return dialect.connect(*cargs, **cparams)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/sqlalchemy/engine/default.py:493, in DefaultDialect.connect(self, *cargs, **cparams)
491 def connect(self, *cargs, **cparams):
492 # inherits the docstring from interfaces.Dialect.connect
--> 493 return self.dbapi.connect(*cargs, **cparams)
File /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/psycopg2/__init__.py:122, in connect(dsn, connection_factory, cursor_factory, **kwargs)
119 kwasync['async_'] = kwargs.pop('async_')
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: (psycopg2.OperationalError) connection to server at "localhost" (::1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
(Background on this error at: http://sqlalche.me/e/13/e3q8)
Exercise#
Try out one of the free online SQL playgrounds