Skip to content

Commit 831367b

Browse files
committed
filedump
1 parent 4305f3d commit 831367b

File tree

7 files changed

+1072
-0
lines changed

7 files changed

+1072
-0
lines changed

201306/20130626_01.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
## PostgreSQL 大对象或bytea存取pdf,jpg等文件 - PostgreSQL export regular file like pdf, word, text, doc stored in bytea type
2+
3+
### 作者
4+
digoal
5+
6+
### 日期
7+
2013-06-26
8+
9+
### 标签
10+
PostgreSQL , bytea , large object , 大对象 , 存取 , export , import
11+
12+
----
13+
14+
## 背景
15+
群里一位兄弟问到的关于导出bytea字段的问题, 例如它在数据库中使用bytea存储了pdf, text文档等. 需求是导出这类文件.
16+
17+
使用lo_export可以导出.
18+
19+
测试如下 :
20+
21+
1\. 创建测试表.
22+
23+
```
24+
digoal=# create table bytea_test(id int, info bytea);
25+
CREATE TABLE
26+
```
27+
28+
2\. 写入bytea.
29+
30+
```
31+
digoal=# insert into bytea_test values (1, pg_read_binary_file('postgresql.conf'));
32+
INSERT 0 1
33+
```
34+
35+
将$PGDATA/postgresql.conf文件写入到bytea_test表.
36+
37+
38+
3\. 将bytea插入大对象
39+
40+
```
41+
digoal=# begin;
42+
BEGIN
43+
digoal=# select lo_creat(-1);
44+
lo_creat
45+
----------
46+
33301
47+
(1 row)
48+
49+
digoal=# select lo_open(33301,131072);
50+
lo_open
51+
---------
52+
0
53+
(1 row)
54+
55+
digoal=# select lowrite(0,info) from bytea_test where id=1;
56+
lowrite
57+
---------
58+
20273
59+
(1 row)
60+
61+
digoal=# select lo_close(0);
62+
lo_close
63+
----------
64+
0
65+
(1 row)
66+
67+
digoal=# end;
68+
COMMIT
69+
```
70+
71+
4\. 使用lo_export导出大对象.
72+
73+
```
74+
digoal=# \lo_export 33301 '/home/pg93/20130625.conf'
75+
lo_export
76+
```
77+
78+
5\. 比较文件是否一致.
79+
80+
```
81+
pg93@db-172-16-3-33-> md5sum /home/pg93/20130625.conf
82+
b9d8f424843d5fd6311659619f567592 /home/pg93/20130625.conf
83+
pg93@db-172-16-3-33-> md5sum $PGDATA/postgresql.conf
84+
b9d8f424843d5fd6311659619f567592 /pgdata1999/postgresql.conf
85+
```
86+
87+
##参考
88+
1\. http://blog.163.com/digoal@126/blog/static/16387704020130931040444/

0 commit comments

Comments
 (0)