Skip to content

Instantly share code, notes, and snippets.

@vitqst
Created December 9, 2015 07:26
Show Gist options
  • Select an option

  • Save vitqst/f82634f0f05b8f6fd7d8 to your computer and use it in GitHub Desktop.

Select an option

Save vitqst/f82634f0f05b8f6fd7d8 to your computer and use it in GitHub Desktop.
#connect to postgres
import psycopg2
try:
conn = psycopg2.connect("dbname='databasename' user='user_name' host='localhost' password='pass'")
except:
print ("I am unable to connect to the database")
cur = conn.cursor()
cur.execute("SELECT * FROM table") #query
rows = cur.fetchall() #fetch array list
for row in rows:
print ("email = ", row[4], "\n")
conn.commit()
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment