I'm trying to store a small file into a postgres db using the node-postgres module. I understand that I should use the bytea data type to do this. The problem I'm having is when I do some thing like:
fs.readFile path, (err, data) ->
client.query 'UPDATE file_table SET file = $1 WHERE key = $2', [data, key], (e, result) ->
....
The contents of the file column in the db is: \x and nothing is stored. If I change the data buffer to hex i.e. data.toString('hex') the file is stored but all formatting is lost when I read the file back out.
What is the correct way of storing a file into postgres using the node-postgres module?
psql
. See if it's correct there. That will tell you if the problem is with inserting the data correctly, or with reading it back out. You also need to mention your Pg version; the defaultbytea
format changed fromescape
tohex
in 9.0. – Craig Ringer Oct 29 '12 at 23:52node-postgres
version are you using? It looks like it supports bytea as of about a year ago (github.com/brianc/node-postgres/pull/38) so you should be able to just pass a buffer. – Craig Ringer Oct 29 '12 at 23:58