psycopg
This is for installing the psycopg2 PostgreSQL adapter for python.
- ensure python is set up on your machine
- it should come with a stock Apple install, so type
python –version
in a terminal to be sure - at the time of this writing, these instructions worked with
Python 2.7.1
- ensure you have a postgresql installation available
- download the source for psycopg2 from http://initd.org/psycopg/
- decompress the archive
- 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
- 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()