Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. import psycopg2
  2. from psycopg2 import pool
  3. try:
  4. postgreSQL_pool = psycopg2.pool.SimpleConnectionPool(1, 20,user = "postgres",
  5. password = "pass@#29",
  6. host = "127.0.0.1",
  7. port = "5432",
  8. database = "postgres_db")
  9. if(postgreSQL_pool):
  10. print("Connection pool created successfully")
  11. # Use getconn() method to Get Connection from connection pool
  12. ps_connection = postgreSQL_pool.getconn()
  13. if(ps_connection):
  14. print("successfully recived connection from connection pool ")
  15. ps_cursor = ps_connection.cursor()
  16. ps_cursor.execute("select * from mobile")
  17. mobile_records = ps_cursor.fetchall()
  18. print ("Displaying rows from mobile table")
  19. for row in mobile_records:
  20. print (row)
  21. ps_cursor.close()
  22. #Use this method to release the connection object and send back ti connection pool
  23. postgreSQL_pool.putconn(ps_connection)
  24. print("Put away a PostgreSQL connection")
  25. except (Exception, psycopg2.DatabaseError) as error :
  26. print ("Error while connecting to PostgreSQL", error)
  27. finally:
  28. #closing database connection.
  29. # use closeall method to close all the active connection if you want to turn of the application
  30. if (postgreSQL_pool):
  31. postgreSQL_pool.closeall
  32. print("PostgreSQL connection pool is closed")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement