The Ruby Way I recently needed to read/write binary data from/to Postgres, but via Ruby. Here's how I did it using the Pg library. Although not strictly Postgres-specific, I thought I'd include this Ruby-centric answer for reference. Postgres DB Setup require 'pg' DB = PG::Connection.new(host: 'localhost', dbname:'test') DB.exec "CREATE TABLE mytable (testcol BYTEA)" BINARY = 1 Insert Binary Data sql = "INSERT INTO mytable (testcol) VALUES ($1)" param = {value: binary_data, format: BINARY} DB.exec_params(sql, [param]) {|res| res.cmd_tuples == 1 } Select Binary Data sql = "SELECT testcol FROM mytable LIMIT 1" DB.exec_params(sql, [], BINARY) {|res| res.getvalue(0,0) }