PostgreSQL将大对象导出到客户端

kxkpmulp  于 2023-03-01  发布在  PostgreSQL
关注(0)|答案(5)|浏览(149)

我有一个PostgreSQL 9.1数据库,其中的图片存储为大对象。有没有办法通过SQL查询将文件导出到客户端的文件系统?

select lo_export(data,'c:\img\test.jpg') from images where id=0;

我正在寻找一种类似于上述行的方式,但以客户为目标。提前感谢!

kd3sttzy

kd3sttzy1#

这个答案是很晚,但将是有帮助的,所以有人我肯定
要将映像从服务器获取到客户端系统,可以使用以下命令

"C:\Program Files\PostgreSQL\9.0\bin\psql.exe" -h 192.168.1.101 -p 5432 -d mDB -U mYadmin -c  "\lo_export 19135 'C://leeImage.jpeg' ";

地点
1.* * h www.example.com**:是服务器系统IP 192.168.1.101**: is the server system IP
1.* -d移动数据库*:数据库名称
1.* -您的管理员*:用户名
1.* \lo_导出*:将在客户端系统位置创建映像的导出函数
1.* * C://李图像. jpeg**:映像OID中目标映像的位置和名称
1.* * 19135年**:这是表中映像的OID。
文档位于commandprompt.com

ajsxfq5m

ajsxfq5m2#

乔治,
根据Python 9.1的文档,lo_export是相对于执行调用的客户端的,所以如果客户端A连接到数据库B,那么当客户端A执行SQL时,lo_export应该在客户端A上创建您指定的文件。
鉴于您已经声明在MATLAB下使用JDBC(我不熟悉您在MATLAB下可以做什么,也不熟悉执行调用的接口),如果您从JDBC连接手动调用:

java.sql.Connection conn= ...
java.sql.Statement stmt= conn.createStmt();
java.sql.ResultSet rs= stmt.executeQuery("select data from images where id=0");
// Assume one result
rs.next();
// Gets the blob input stream
InputStream blobData= rs.getInputStream(1);

// At this point you will have to write it to a file. 
// See below

rs.close();
stmt.close();
conn.close();

为了简洁起见,我在这里对JDBC操作进行了非常宽松和快速的处理。应该有更多的错误检查以及try/catch/finally语句来 Package 和清理连接。
File copy example.

pb3s4cty

pb3s4cty3#

这是不可能的,因为PostgreSQL服务器所能做的就是通过客户端建立的网络连接将数据发送回客户端。特别是,它不能在客户端文件系统上创建文件,只有客户端代码才能做到这一点。

uinbv5nw

uinbv5nw4#

来源:http://www.postgresql.org/docs/8.4/static/lo-funcs.html

CREATE TABLE image (
    name            text,
    raster          oid
);

SELECT lo_creat(-1);       -- returns OID of new, empty large object

SELECT lo_create(43213);   -- attempts to create large object with OID 43213

SELECT lo_unlink(173454);  -- deletes large object with OID 173454

INSERT INTO image (name, raster)
    VALUES ('beautiful image', lo_import('/etc/motd'));

INSERT INTO image (name, raster)  -- same as above, but specify OID to use
    VALUES ('beautiful image', lo_import('/etc/motd', 68583));

SELECT lo_export(image.raster, '/tmp/motd') FROM image
    WHERE name = 'beautiful image';
aor9mmx1

aor9mmx15#

这是边缘相关的,但是由于每次有人搜索“lo_export”时都会出现这个StackOverflow问题,并且由于缺少文档,我不得不反复试验,所以存储在表imageraster列中的每个对象都可以导出到/tmp/exportdir/,语句如下:

SELECT lo_export(image.raster, concat('/tmp/exportdir/', image.id))
FROM image

这假定名为id的列标识每一行,并包含可用作文件名的值。

相关问题