Skip to content

Instantly share code, notes, and snippets.

@yglodt
Created March 14, 2018 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yglodt/409ea74b3e7da42554789623d61fbefe to your computer and use it in GitHub Desktop.
Save yglodt/409ea74b3e7da42554789623d61fbefe to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import psycopg2
import sys
con = None
conn_string = "host='localhost' dbname='mydb' user='myuser' password='mypass'"
try:
con = psycopg2.connect(conn_string)
cur = con.cursor()
#cur.execute('SELECT version()')
#ver = cur.fetchone()
#print ver
cur.execute('SELECT id, name from address order by name')
rows = cur.fetchall()
for row in rows:
print row[0], " ", row[1]
except psycopg2.DatabaseError, e:
print 'Error %s' % e
sys.exit(1)
finally:
if con:
con.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment