<p><strong>编辑</p>
<p>我已经有一段时间没有回答这个问题了,但我想我弄错了,虽然提问者似乎对答案很满意,但我相信它需要一些评论。感谢丹尼尔·维瑞特!你知道吗</p>
<p><strong>低成本导入</strong></p>
<p>使用<a href="https://www.postgresql.org/docs/current/lo-funcs.html" rel="nofollow noreferrer">^{<cd1>}</a>可以使用绝对路径导入文件,但它会返回一个类型为<a href="https://www.postgresql.org/docs/12/datatype-oid.html" rel="nofollow noreferrer">^{<cd2>}</a>的对象,因此需要更改列数据类型,这也是stud3nt在另一个答案中提出的。你知道吗</p>
<pre><code>INSERT INTO public.tblcast(castname, castimage)
VALUES ('Henry Cavill',lo_import('server_path_to_file'));
</code></pre>
<p>如果您没有在导入之前将文件放入服务器的特权(像我们大多数人一样),您可以从控制台使用带有<a href="https://www.postgresql.org/docs/9.2/app-psql.html" rel="nofollow noreferrer">^{<cd4>}</a>的<code>\lo_import</code>工具:</p>
<pre><code>echo "\lo_import '/client_path_to_file' \\\ INSERT INTO public.tblcast VALUES ('Henry Cavil', :LASTOID)" | psql yourdb
</code></pre>
<p><strong>loèu出口</strong>:</p>
<pre><code>psql yourdb -c "SELECT lo_export(castimage, 'path_to_export_file') FROM tblcast;"
</code></pre>
<p><strong>pg\u read\u文件</strong>(不太灵活):</p>
<p>这个<a href="https://www.postgresql.org/docs/9.5/functions-admin.html" rel="nofollow noreferrer">^{<cd5>}</a>提供了在服务器中读取文件的可能性。但是,它仅限于数据目录路径。如果您想知道它在系统中的位置,请尝试以下命令:</p>
<pre><code>SHOW data_directory
</code></pre>
<p>下面是一个如何使用它的方法——正如Mike Pur的回答所示:</p>
<pre><code>INSERT INTO public.tblcast(castname, castimage)
VALUES ('Henry Cavill',pg_read_file('path to file')::bytea);
</code></pre>
<p><strong>原始答案:</strong></p>
<p>文件路径中有<code>\</code>。用<code>E</code>转义字符串就可以了。你知道吗</p>
<pre><code>INSERT INTO public.tblcast(castname, castimage)
VALUES ('Henry Cavill',bytea(E'E:\Cast\henry.jpg'));
</code></pre>