如何将Postgres bytea列下载为文件 [英] How to download Postgres bytea column as file

查看:101
本文介绍了如何将Postgres bytea列下载为文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有许多文件作为bytea存储在postgres 8.4中。文件类型为.doc,.odt,.pdf,.txt等。

Currently, i have a number of files stored in postgres 8.4 as bytea. The file types are .doc, .odt, .pdf, .txt and etc.

我可以知道如何下载存储在Postgres中的所有文件吗,因为我需要做一个备份。
我需要使用其原始文件类型而不是字节格式。

May i know how to download all the file stored in Postgres because i need to to do a backup. I need them in their original file type instead of bytea format.

谢谢!

推荐答案

一个简单的选择是使用 COPY 命令和 编码 为十六进制格式,然后应用 xxd shell命令(带有-p 连续十六进制样式开关)。例如,假设我在示例表的bytea列中有jpg图像:

One simple option is to use COPY command with encode to hex format and then apply xxd shell command (with -p continuous hexdump style switch). For example let's say I have jpg image in bytea column in samples table:

\copy (SELECT encode(file, 'hex') FROM samples LIMIT 1) TO
    '/home/grzegorz/Desktop/image.hex'

$ xxd -p -r image.hex > image.jpg

我检查过它在实践中是否有效。

As I checked it works in practice.

这篇关于如何将Postgres bytea列下载为文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆