Basic connection to PostgreSQL with psycopg : PostgreSQL : Database PYTHON TUTORIALS


PYTHON TUTORIALS » Database » PostgreSQL »

 

Basic connection to PostgreSQL with psycopg


import psycopg

def getdsn(db = None, user = None, passwd = None, host = None):
    if user == None:
        import os, pwd
        user = pwd.getpwuid(os.getuid())[0]
    if db == None:
        db = user

    dsn = 'dbname=%s user=%s' % (db, user)
    if passwd != None:
        dsn += ' password=' + passwd
    if host != None:
        dsn += ' host=' + host
    return dsn

dsn = getdsn()
print "Connecting to %s" % dsn
dbh = psycopg.connect(dsn)
print "Connection successful."
dbh.close()



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON TUTORIALS

 Navioo Database
» PostgreSQL