Sign in with
Sign up | Sign in
Your question

[Solved]Sorry too many clients in Python Postgresql code

Last response: in Applications
Share
August 19, 2011 6:20:46 AM

  1. def main():
  2. # start script
  3. # Define connection string to db
  4. conn_string = "dbname='Fenrir' user='Fenrir'"
  5.  
  6. # print the connection string we will use to connect
  7. print "Connecting to database\n ->%s" %(conn_string)
  8.  
  9. try:
  10. # establish a connection, if not exception will be raised
  11. conn = psycopg2.connect(conn_string)
  12.  
  13. # conn.cursor will return a cursor object
  14. cursor = conn.cursor()
  15.  
  16. print "connected\n"
  17.  
  18. except:
  19. # Get the most recent exception
  20. exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
  21. # Exit script and print an error message of the details
  22. sys.exit("Database connection failed!\n ->%s" %(exceptionValue))
  23.  
  24. if __name__ == "__main__":
  25. sys.exit(main())
  26.  
  27. 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.
August 19, 2011 6:40:12 AM

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.
a b L Programming
August 19, 2011 6:43:54 AM

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.
!