Skip to content

Instantly share code, notes, and snippets.

@arthuralvim
Created February 24, 2015 17:21
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 arthuralvim/c1b8a94f91fd12d60ff1 to your computer and use it in GitHub Desktop.
Save arthuralvim/c1b8a94f91fd12d60ff1 to your computer and use it in GitHub Desktop.
Testing Postgres Connection
#!/usr/bin/python
import psycopg2
import sys
host = ''
dbname = ''
user = ''
password = ''
port = 5432
conn_string = "host='%s' dbname='%s' user='%s' password='%s' port='%i'"\
% (host, dbname, user, password, port)
print "Connecting to database\n ->%s" % (conn_string)
try:
conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
print "Connected!\n"
except:
exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
sys.exit("Database connection failed!\n ->%s" % (exceptionValue))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment