设为首页 收藏本站
查看: 711|回复: 0

[经验分享] PostgreSQL的hstore初步学习

[复制链接]
发表于 2016-11-21 09:11:21 | 显示全部楼层 |阅读模式
  安装hstore:
  进入源代码的 /contrib/hstore 目录,然后执行gmake 和 gmake install:



[iyunv@pg200 hstore]# gmake
gcc -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fpic -I. -I. -I../../src/include -D_GNU_SOURCE   -c -o hstore_io.o hstore_io.c
gcc -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fpic -I. -I. -I../../src/include -D_GNU_SOURCE   -c -o hstore_op.o hstore_op.c
gcc -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fpic -I. -I. -I../../src/include -D_GNU_SOURCE   -c -o hstore_gist.o hstore_gist.c
gcc -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fpic -I. -I. -I../../src/include -D_GNU_SOURCE   -c -o hstore_gin.o hstore_gin.c
gcc -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fpic -I. -I. -I../../src/include -D_GNU_SOURCE   -c -o hstore_compat.o hstore_compat.c
gcc -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fpic -I. -I. -I../../src/include -D_GNU_SOURCE   -c -o crc32.o crc32.c
gcc -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fpic -shared -o hstore.so hstore_io.o hstore_op.o hstore_gist.o hstore_gin.o hstore_compat.o crc32.o -L../../src/port  -Wl,-rpath,'/usr/local/pgsql/lib',--enable-new-dtags
  gmake:



[iyunv@pg200 hstore]# gmake install
/bin/mkdir -p '/usr/local/pgsql/lib'
/bin/mkdir -p '/usr/local/pgsql/share/extension'
/bin/mkdir -p '/usr/local/pgsql/share/extension'
/bin/sh ../../config/install-sh -c -m 755  hstore.so '/usr/local/pgsql/lib/hstore.so'
/bin/sh ../../config/install-sh -c -m 644 ./hstore.control '/usr/local/pgsql/share/extension/'
/bin/sh ../../config/install-sh -c -m 644 ./hstore--1.1.sql ./hstore--1.0--1.1.sql ./hstore--unpackaged--1.0.sql  '/usr/local/pgsql/share/extension/'
[iyunv@pg200 hstore]#
  然后,启动PostgreSQL,再启动psql后,安装hstore扩展:



postgres=# create extension hstore;
CREATE EXTENSION
postgres=#
  进行测试:
  建表:



postgres=# create table hstore_test(item_id serial, data hstore);
NOTICE:  CREATE TABLE will create implicit sequence "hstore_test_item_id_seq" for serial column "hstore_test.item_id"
CREATE TABLE
postgres=#
  插入数据:



postgres=# INSERT INTO hstore_test (data) VALUES ('"key1"=>"value1", "key2"=>"value2", "key3"=>"value3"');
INSERT 0 1
postgres=# select * from hstore_test;
item_id |                         data                        
---------+------------------------------------------------------
1 | "key1"=>"value1", "key2"=>"value2", "key3"=>"value3"
(1 row)
postgres=#
  修改数据:



postgres=# UPDATE hstore_test SET data = delete(data, 'key2')
postgres-# ;
UPDATE 1
postgres=# select * from hstore_test;
item_id |                data               
---------+------------------------------------
1 | "key1"=>"value1", "key3"=>"value3"
(1 row)
postgres=#



postgres=# UPDATE hstore_test SET data = data || '"key4"=>"some value"'::hstore;
UPDATE 1
postgres=# select * from hstore_test;
item_id |                           data                           
---------+----------------------------------------------------------
1 | "key1"=>"value1", "key3"=>"value3", "key4"=>"some value"
(1 row)
postgres=#
  按Key值查询:



postgres=# SELECT * FROM hstore_test WHERE data ? 'key4';
item_id |                           data                           
---------+----------------------------------------------------------
1 | "key1"=>"value1", "key3"=>"value3", "key4"=>"some value"
(1 row)
postgres=#
postgres=# SELECT * FROM hstore_test WHERE NOT data ? 'key5';
item_id |                           data                           
---------+----------------------------------------------------------
1 | "key1"=>"value1", "key3"=>"value3", "key4"=>"some value"
(1 row)
postgres=# SELECT * FROM hstore_test WHERE data @> '"key4"=>"some value"'::hstore;
item_id |                           data                           
---------+----------------------------------------------------------
1 | "key1"=>"value1", "key3"=>"value3", "key4"=>"some value"
(1 row)
postgres=# SELECT data -> 'key4' FROM hstore_test;
?column?  
------------
some value
(1 row)
postgres=# SELECT item_id, (each(data)).* FROM hstore_test WHERE item_id = 2;
item_id | key | value
---------+-----+-------
(0 rows)
postgres=# SELECT item_id, (each(data)).* FROM hstore_test WHERE item_id = 1;
item_id | key  |   value   
---------+------+------------
1 | key1 | value1
1 | key3 | value3
1 | key4 | some value
(3 rows)
postgres=#

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-303229-1-1.html 上篇帖子: Postgresql 触发器一例 下篇帖子: Postgresql中的dataType
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表