Skip to content

Instantly share code, notes, and snippets.

@dulichan
Created December 5, 2012 18:55
Show Gist options
  • Save dulichan/4218430 to your computer and use it in GitHub Desktop.
Save dulichan/4218430 to your computer and use it in GitHub Desktop.
Python connection to postgres database
import psycopg2
import sys
connection = None
try :
connection = psycopg2.connect(database='postgres', user='test', host='localhost')
cur = connection.cursor()
cur.execute('SELECT version()')
ver = cur.fetchone()
print ver
except psycopg2.DatabaseError, e:
print "Error "+str(e)
sys.exit(1)
finally:
if connection:
connection.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment