psycopg2 for insertion of binary data to PostgreSQL database

D'Arcy J.M. Cain darcy at
Sat Aug 21 23:49:04 CEST 2010

On Sat, 21 Aug 2010 22:58:00 +0200
Julia Jacobson <julia.jacobson at> wrote:
> For the insertion of pictures into my PostgreSQL database [with table 
> foo created by SQL command "CREATE TABLE foo (bmp BYTEA)], I've written 
> the following script:
> #!/usr/bin/python
> import psycopg2
> try:
>      conn = psycopg2.connect("dbname='postgres' user='postgres' 
> host='localhost' password='data'");
> except:
>      print "I am unable to connect to the database"

First, bare excepts are a bad idea.  Figure out what exception you
expect to catch and catch that one.

In this case, start by removing the try/except altogether and see what
the traceback says.  That may explain your problem right off the bat.
If it doesn't then repost with the traceback.

D'Arcy J.M. Cain <darcy at>         |  Democracy is three wolves                |  and a sheep voting on
+1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.

More information about the Python-list mailing list