Re: [Non-DoD Source] Re: Inserting .png file into bytea column

From: "Ferrell, Denise D CTR NSWCDD, H11" <denise(dot)ferrell(dot)ctr(at)navy(dot)mil>
To: Craig James <cjames(at)emolecules(dot)com>, Achilleas Mantzios <achill(at)matrix(dot)gatewaynet(dot)com>
Cc: "pgsql-admin(at)postgresql(dot)org" <pgsql-admin(at)postgresql(dot)org>
Subject: Re: [Non-DoD Source] Re: Inserting .png file into bytea column
Date: 2015-11-19 15:20:59
Message-ID: 2BC18916DE4BF141AC1EFC55CE2CAE7D111B8D0B@NAEANRFKXM01V.nadsusea.nads.navy.mil
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

Thank you!

-----Original Message-----
From: Craig James [mailto:cjames(at)emolecules(dot)com]
Sent: Thursday, November 19, 2015 10:09 AM
To: Achilleas Mantzios
Cc: Ferrell, Denise D CTR NSWCDD, H11; pgsql-admin(at)postgresql(dot)org
Subject: [Non-DoD Source] Re: [ADMIN] Inserting .png file into bytea column

On Thu, Nov 19, 2015 at 6:19 AM, Achilleas Mantzios <achill(at)matrix(dot)gatewaynet(dot)com> wrote:

On 19/11/2015 16:07, Ferrell, Denise D CTR NSWCDD, H11 wrote:

Good Morning All,

I am using PostgreSQL 9.3 on Linux Rehat...

I'm trying to insert an image (.png format) into a table of flags. I've tried the following but keep getting errors.

CREATE TABLE FLAGS (country_code text, flag bytea);

INSERT INTO flags VALUES ('AD', pg_read_file('/home/flags')::bytea);

Get the following error:
ERROR: absolute path not allowed
*********ERROR*************
ERROR: absolute path not allowed
SQL State: 42501

Any assistance would be greatly appreciated.

http://www.postgresql.org/docs/9.2/static/functions-admin.html#FUNCTIONS-ADMIN-GENFILE

use pg_read_binary_file(filename text [, offset bigint, length bigint]) as documented

The documentation you linked to explains the problem:

The functions shown in Table 9-66 provide native access to files on the machine hosting the server. Only files within the database cluster directory and the log_directory can be accessed. Use a relative path for files in the cluster directory, and a path matching the log_directoryconfiguration setting for log files. Use of these functions is restricted to superusers.

You can't access files on the client side, and on the server side you can only access files that are internal to Postgres (e.g. inside /data/postgres, or whatever path your Postgres uses for the database). And only super-users can use this function at all because it allows access to sensitive server files.

The easiest way to do this on the client side is to read the file into a client-side variable and then use a prepared statement. In Perl, something like this:

use DBI;
my $dbh = $dbi->connect(...);

my $photo;
open(PHOTO, "/home/flags");
binmode PHOTO;
while (<PHOTO>) {
$photo .= $_;
}
my $sth = $dbh->prepare("insert into flags(country_code, flag) values(?, ?)");
$sth->execute('AD', $photo);

Craig

Denise Ferrell




--
Achilleas Mantzios
IT DEV Lead
IT DEPT
Dynacom Tankers Mgmt

--

---------------------------------
Craig A. James

Chief Technology Officer

eMolecules, Inc.

---------------------------------

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Dorian Machado 2015-11-19 18:35:35 Re: Inserting .png file into bytea column
Previous Message Jan-Peter Seifert 2015-11-19 15:18:02 Re: postgresql.conf