Handle Large Objects (PostgreSQL)

I have a problem with uploading a binary file on rails and saving it
directly in the database. It’t doesn’t work correctly.

I use large objects. The table entry looks as follows:

id serial NOT NULL,
description varchar(100),
name varchar(100) NOT NULL,
content_type varchar(100),
data oid NOT NULL,
CONSTRAINT pk_pictureid PRIMARY KEY (id)

I have edited


and added the line:

 def lo_import(file)
    @connection.lo_import(file)  #calls lo_import from postgres c 


under the class PostgreSQLAdapter.

To import a picture into the database I add the following lines ito the
picture model:

lo = self.connection().lo_import(picture_field.local_path())
self.data = lo.oid()

Now, if I try to upload an Image an error occurs:

PGError in Upload controllerController#save

could not open large object 16555

I don’t know where the problem lies. Is there someone who has experience
rails, large objects and postgres? Hint’s are welcome.

My system properties are:

OS: OSX Tiger
Ruby: 1.8.4
Postgres: 8.1.4
Postgres Driver: postgres-0.7.1 (C Driver) - works correctly
Rails: 1.1.6

i dont think its a good idea to save pictures (for a gallery?) in the
for obvious reasons (just think about performance)
in my gallery implementation i used acts_as_file_column (i think that
the name), but that plugin is not developed anymore, acts_as_attachment
shall replace it. these plugins save the file on the disk and use a
in the model to store the filename in (or some other technique).
this way you can even integrate RMagick or some other sort of
creating multiple thumbnails in various sizes and stuff.

so my tip is: dont use blobs to store “attachments”


2006/10/4, Joel S. [email protected]:

description varchar(100),

picture model:
I don’t know where the problem lies. Is there someone who has experience

Posted via http://www.ruby-forum.com/.

Michael S. [email protected]

www.siebert-wd.de - Gedanken lesen

thank’s for hint’s

I need the pictures for blog. That will not be many.



It works. I have forgotten to start a transaction.

lo = self.connection().lo_import(picture_field.local_path())


Like you I am trying out the examples in the Agile Web D. with
Rails book. Your notes help me understand how to save a blob in a
PostgreSQL table.

I have no clue how to retrieve the blob and show the image on a page.
Can you help? Thanks.

On Oct 5, 4:48 am, Joel S. [email protected]