从 bytea 列导出图像文件

Ahm*_*buğ 6 postgresql export postgresql-9.3 bytea

我正在尝试从 PostgreSQL 数据库users_data_circulation中导出图像文件。该表有一个photo(bytea) 列。

我的命令:

copy (select encode(photo,'hex') from users_data_circulation limit 1)
    TO '/tmp/imagetest.hext';
Run Code Online (Sandbox Code Playgroud)

在操作系统中:

$> xxd -p -r /tmp/imagetest.hex > /tmp/imagetest.jpg
$> file /tmp/imagetest.jpg
/tmp/imagetest.jpg: ASCII TEXT
Run Code Online (Sandbox Code Playgroud)

我无法打开jpg文件。我怎样才能把这个文件转换成jpg?

devinim@devinimpostgresql:~$ hexdump -C /tmp/image.hex | more
00000000  5c 5c 33 37 37 5c 5c 33  33 30 5c 5c 33 37 37 5c  |\\377\\330\\377\|
00000010  5c 33 34 30 5c 5c 30 30  30 10 4a 46 49 46 5c 5c  |\340\\000.JFIF\\|
00000020  30 30 30 01 02 5c 5c 30  30 30 5c 5c 30 30 30 01  |000..\\000\\000.|
00000030  5c 5c 30 30 30 01 5c 5c  30 30 30 5c 5c 30 30 30  |\\000.\\000\\000|
00000040  5c 5c 33 37 37 5c 5c 33  34 31 5c 5c 30 30 30 5c  |\\377\\341\\000\|
00000050  76 50 49 43 5c 5c 30 30  30 02 5c 6e 5c 6e 01 5c  |vPIC\\000.\n\n.\|
00000060  5c 30 30 30 5c 5c 33 37  37 5c 5c 33 37 36 5c 5c  |\000\\377\\376\\|
00000070  30 30 30 21 50 69 63 74  75 72 65 20 45 6c 65 6d  |000!Picture Elem|
00000080  65 6e 74 73 2c 20 49 6e  63 2e 20 49 53 45 2f 53  |ents, Inc. ISE/S|
... continues like that
Run Code Online (Sandbox Code Playgroud)

Eva*_*oll 4

将 jpeg 存储在数据库中是一个可怕的想法。

也就是说,如果你想从数据库中取出它,你可以使用 psql。这会将其以十六进制形式输出。

psql -t -A -o "/tmp/imagetest.jpg" -c \
  "SELECT photo FROM users_data_circulation LIMIT 1";
Run Code Online (Sandbox Code Playgroud)

您可能还想查看这个大物体。