我正试图插入postgres数据库。这是查询
INSERT INTO public.tblcast(
castname, castimage)
VALUES ('Henry Cavill',bytea('E:\Cast\henry.jpg'));但它显示出一个错误
ERROR: invalid input syntax for type bytea
LINE 3: VALUES ('Henry Cavill',bytea('E:\Cast\henry.jpg'));
^
SQL state: 22P02
Character: 81ERROR: invalid input syntax for type bytea列castimage具有数据类型bytea。
发布于 2019-12-05 15:50:35
编辑:我已经有一段时间没有回答这个问题了,但我想我错了,虽然OP似乎对答案很满意,但我认为它需要一些评论。感谢丹尼尔·韦里特!
lo_import
使用lo_import,您可以使用绝对路径导入文件,但它返回一个类型为OID的对象,因此您需要更改列数据类型--这也是stud3nt在另一个答案中提出的。
INSERT INTO public.tblcast(castname, castimage)
VALUES ('Henry Cavill',lo_import('server_path_to_file'));如果您没有权限在导入之前将文件放在服务器中(就像我们大多数人一样),您可以从您的控制台使用带有\lo_import的psql工具:
echo "\lo_import '/client_path_to_file' \\\ INSERT INTO public.tblcast VALUES ('Henry Cavil', :LASTOID)" | psql yourdblo_export
psql yourdb -c "SELECT lo_export(castimage, 'path_to_export_file') FROM tblcast;"pg_read_file (不太灵活):
此generic file access function提供了在服务器中读取文件的可能性。但是,它仅限于数据目录路径。如果您想知道它在系统中的位置,请尝试以下命令:
SHOW data_directory以下是如何使用它的一种方法--正如迈克普尔的回答所示:
INSERT INTO public.tblcast(castname, castimage)
VALUES ('Henry Cavill',pg_read_file('path to file')::bytea);发布于 2019-12-05 15:45:41
使用pg_read_file
INSERT INTO public.tblcast(
castname, castimage)
VALUES ('Henry Cavill',pg_read_file('E:\Cast\henry.jpg')::bytea);发布于 2019-12-05 15:42:10
使用lo_import('E:\Cast\henry.jpg')而不是bytea('E:\Cast\henry.jpg')
https://stackoverflow.com/questions/59198586
复制相似问题