Guest User

Untitled

a guest
Dec 10th, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. """
  2. Package psycopg2 must be installed through conda for this to work
  3.  
  4. conda install -c anaconda psycopg2
  5. """
  6. import psycopg2
  7. import json
  8.  
  9.  
  10. # Get credentials from kubernetes. The credentials were setup as a dictionary
  11. credentials = None
  12. with open('/var/run/secrets/user_credentials/postgres_credentials') as f:
  13. credentials = json.load(f)
  14.  
  15. # Check and make sure the credentials were pulled correctly
  16. if credentials:
  17. # Connect to the database
  18. conn = None
  19. try:
  20. conn = psycopg2.connect(
  21. dbname=credentials.get('db_name'),
  22. host=credentials.get('host_name'),
  23. user=credentials.get('username'),
  24. password=credentials.get('password')
  25. )
  26. except:
  27. print("I am unable to connect to the database")
  28.  
  29. # Get a cursor and execute select statement
  30. cur = conn.cursor()
  31. cur.execute("""SELECT * from playground""")
  32. rows = cur.fetchall()
  33.  
  34. # Print out the results
  35. for row in rows:
  36. print(row)
  37.  
  38. # Close the connection when finished
  39. conn.close()
Add Comment
Please, Sign In to add comment