如何更新Postgres表中的Bytea数据?

时间:2018-09-19 07:59:26

标签: postgresql

我正在尝试将本地存储的文件内容导入表中一行的列中。 列的类型是bytea。

类似的东西:

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

但是,它给出以下错误:

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

我尝试过铸造。用于下面的行:

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

但是它给出了:

ERROR:  cannot cast type oid to bytea

我是不熟悉Postgres的人。在此问题上的任何线索都将有所帮助。谢谢

2 个答案:

答案 0 :(得分: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.

答案 1 :(得分:0)

好吧,如果您只想使用SQL进行操作,则可以先将其作为oid导入,然后将其转换为bytea。但这有点奇怪。

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