1

I am trying to import contents of a file locally stored into a column of a row in a table. The type of column is bytea.

Something like:

UPDATE server_info
SET key = lo_import('C:\Users\certificate.p12')
WHERE server_id = 1;

However, it gives below error:

ERROR:  column "key" is of type bytea but expression is of type oid

I tried casting. Used below line for it:

UPDATE server_info
SET key = lo_import('C:\Users\certificate.p12')::bytea
WHERE server_id = 1;

But it gives:

ERROR:  cannot cast type oid to bytea

I am new to using Postgres. Any leads in this matter will be helpful. Thank you

2
  • Which SQL client are you using? Some support that kind of "uploading" files into a bytea column. Commented Sep 19, 2018 at 8:25
  • @a_horse_with_no_name I am using pgAdmin 4 as client Commented Sep 19, 2018 at 9:00

2 Answers 2

1

You won't be able to do that with just SQL.

You'll have to write a program in the language of your choice that reads the files into memory and uses that as parameter to an INSERT.

Sign up to request clarification or add additional context in comments.

1 Comment

Okay. That way I am able to do it i.e. using a program. But wanted to know if it is possible via just SQL.
0

Well, if you want to do it just with an SQL, you could first import it as an oid and then convert it to a bytea. But this a little bit weired.

ALTER TABLE server_info add column key_bytea bytea
UPDATE server_info SET key_bytea = lo_get(key)
ALTER TABLE server_info drop column key
ALTER TABLE server_info rename column key_bytea to key

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.