[Solved]Sorry too many clients in Python Postgresql code

[cpp]def main():
# start script
# Define connection string to db
conn_string = "dbname='Fenrir' user='Fenrir'"

# print the connection string we will use to connect
print "Connecting to database\n ->%s" %(conn_string)

# establish a connection, if not exception will be raised
conn = psycopg2.connect(conn_string)

# conn.cursor will return a cursor object
cursor = conn.cursor()

print "connected\n"

# Get the most recent exception
exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
# Exit script and print an error message of the details
sys.exit("Database connection failed!\n ->%s" %(exceptionValue))

if __name__ == "__main__":


Connecting to database
->dbname='Fenrir' user='Fenrir'
Database connection failed!
->FATAL: sorry, too many clients already
Is what is posted in the terminal after Fenrir connects about 30 or so times.

I'm using psycopg2
Can some one tell me why this is happening? I searched around and every where says I should just increase the connection size. But why do I need to do that when only one user is supposed to be connecting. I am still a newbie when it comes to python but not to programming so is there something I'm missing causing the main to called so many times? If you guys need anymore information please let me know. For the next 20 mins or so I'm gonna keep looking into this and documenting what I find. This way if anyone else has the same problem they can just come here.Thanks in advance.
2 answers Last reply
More about solved sorry clients python postgresql code
  1. Maybe I'm just really tired right now... On line 24 and 25 there are too many indents. The way the code is setup line 24 and 25 don't execute at the right time which allows the main to be run a number of times. Now to research why this makes such an impact.
  2. You appear to be calling "main()" recursively within the definition of main. I think you meed to delete the last line and then unindent the preceeding "if" statement.
Ask a new question

Read More

Programming Cursor Connection Database Apps