I load image in PostgreSQL with next command:
UPDATE "USERS" SET userblob = (pg_read_binary_file('img.png')::bytea) WHERE id>1;
The field type is: bytea
Now DB like this.
Then I tried to write binary blob back to file system. And got very big file that include data like: [56, 57, 53, 48, 52, 101, 52, 55, 48, 100, 48, 97, 49 ...
What I am doing wrong? My code is next:
struct MyData
{
string guid;
string id;
string name;
byte [] userblob;
string fl;
}
auto rs = pgstmt.executeQuery(`SELECT guid::text, id, name, userblob, "FL" FROM "USERS" where "FL" = 10;`);
while (rs.next())
{
md.guid = to!string(rs.getString(1));
md.id = to!string(rs.getString(2));
md.name = to!string(rs.getString(3));
md.userblob = rs.getBytes(4);
md.fl = to!string(rs.getBytes(5));
std.file.write("output.png", cast(byte[])md.userblob);
readln;
mydata ~= md;
}
UPD: I have read docs. PostgreSQL bytea is pain. It's much better to store all text in base64 encoding.
Working example:
UPDATE "USERS" SET userblob = encode(pg_read_binary_file('img.png'), 'base64') WHERE id>1;
There was a problem in bytea encoding/decoding function. Fixed in ddbc v0.2.32