Reputation: 1527
I have an issue with Postgresql psycopg2. I receive the error :
error in executing with exception: connection pool exhausted
My code :
from psycopg2 import pool
import pandas.io.sql as sqlio
import pandas as pd
db = pool.ThreadedConnectionPool(5, 100,host=POSTGRES['host'],
database=POSTGRES['database'],user=POSTGRES['username'],
password=POSTGRES['password'],port=POSTGRES['port'])
try:
sql = "select * from role"
data = sqlio.read_sql_query(sql, db.getconn())
return data.to_json(orient='records')
except Exception as e:
print "error in executing with exception: ", e
return pd.DataFrame({'empty' : []})
and this request should return only 5 rows, but i get this error.
Do you have any idea why i get this error ?
My Postgresql Database (medium instance) is deployed on a public cloud.
Thank you in advance
Upvotes: 7
Views: 11107
Reputation: 16184
it would appear you need to return the connection back to the pool at some point, see:
http://initd.org/psycopg/docs/pool.html#psycopg2.pool.AbstractConnectionPool.putconn
i.e. something like:
sql = "select * from role"
try:
conn = db.getconn()
try:
data = sqlio.read_sql_query(sql, conn)
finally:
pool.putconn(conn)
return data.to_json(orient='records')
except Exception as e:
print "error in executing with exception: ", e
return pd.DataFrame({'empty' : []})
Upvotes: 13