psycopg

This is for installing the psycopg2 PostgreSQL adapter for python.

  1. ensure python is set up on your machine
    1. it should come with a stock Apple install, so type python –version in a terminal to be sure
    2. at the time of this writing, these instructions worked with Python 2.7.1
  2. ensure you have a postgresql installation available
  3. download the source for psycopg2 from http://initd.org/psycopg/
  4. decompress the archive
  5. follow along with these commands:
    # change into the psycopg2 directory
    cd psycopg2-2.5.1
     
    # allow postgres binaries to be available
    export PATH=$PATH:/usr/local/pgsql/bin
     
    # build and install psycopg2
    sudo easy_install psycopg2
  6. sample python script to test db connection
    #!/usr/bin/python
     
    import psycopg2
     
    # psycopg features
    print 'Features of the psycopg are:',
    print ('API Level: %s, Thread Safety Level: %s, Parameter Style: %s.\n' % \
             (psycopg2.apilevel,psycopg2.threadsafety,psycopg2.paramstyle))
     
    # create a connection to the db
    try:
        conn=psycopg2.connect("host=localhost dbname=world user=postgres password=mypass")
        print 'Successfully connected to the database.\n'
    except:
        print 'Error: Unable to connect to database!\n'
     
    conn.close()