Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
1Activity
0 of .
Results for:
No results containing your search query
P. 1
PostgreSQL Modules - chkpass, hstore, fuzzystrmatch, isn tutorial

PostgreSQL Modules - chkpass, hstore, fuzzystrmatch, isn tutorial

Ratings: (0)|Views: 234 |Likes:
Published by Sagar Arlekar
This article will give you an introduction to installing PostgreSql modules.
- Learn how to query the key-value store with hstore,
- Store and validate ISBN numbers with isn
- Store encrypted data with chkpass
- Do partial keyword match (fuzzy string matching) with fuzzystrmatch
This article will give you an introduction to installing PostgreSql modules.
- Learn how to query the key-value store with hstore,
- Store and validate ISBN numbers with isn
- Store encrypted data with chkpass
- Do partial keyword match (fuzzy string matching) with fuzzystrmatch

More info:

Published by: Sagar Arlekar on Mar 05, 2012
Copyright:Attribution Non-commercial Share Alike

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

07/14/2012

pdf

text

original

 
 Admin
How To
P
ostgreSQL has been called the ‘most advanced opensource database’. I have been using it for the last fouryears as an RDBMS for
 Foodlets.in
, and as a spatialdata store at CSTEP (Center for Study of Science, Technologyand Policy). PostgreSQL is one piece of software that doesn’tfail to impress me every now and then.
Installing the modules
Note:
I am running Ubuntu 10.04 and PostgreSQL 8.4.Install the
 postgresql-contrib
package and restart thedatabase server, then check the
contrib
directory for the list ofavailable modules:
sudo apt-get install postgresql-contribsudo /etc/init.d/postgresql-8.4 restartcd /usr/share/postgresql/8.4/contrib/ls
Create a test database called
module_test:
su postgrescreatedb module_test
Apply the
chkpass, fuzzystrmatch, isn
and
hstore
modulesto the
module_test 
database by running the followingcommands:
psql -d module_test -f chkpass.sqlpsql -d module_test -f fuzzystrmatch.sqlpsql -d module_test -f isn.sqlpsql -d module_test -f hstore.sql
Let us now look at an example of how each of themodules is used.
Using
chkpass
The
chkpass
module will introduce a new data type,
‘chkpass’,
in the database. This type is used to store anencrypted field, e.g., a password. Let’s see how
chkpass
 works for a user account table that we create and inserttwo rows into:
In this article, we will learn how to install and use the PostgreSQL modules
chkpass, fuzzystrmatch, isn
and
 hstore
. Modules add different capabilities to a database, likeadmin and monitoring tools, new data types, operators, functions and algorithms.Let’s look at modules that add new data types and algorithms, which will help us topush some of the application logic to the database.
88
|
 
March 2012|LINUX For YoU|www.LinuxFU.m
Installingand Using
PostgreSQL
 
Modules
 
How To
Admin
CREATE TABLE accounts (username varchar(100), passwordchkpass);INSERT INTO accounts(username, “password”) VALUES (‘user1’,‘pass1’);INSERT INTO accounts(username, “password”) VALUES (‘user2’,‘pass2’);
We can authenticate users with a query like the onethat follows:
SELECT count(*) from accounts where username=’user1’ andpassword = ‘pass1’
The ‘=’ operator uses the
eq(column_name, text)
inthe module to test for equality.
Chkpass
uses the Unix
crypt()
function, and hence it is weak; only the first eightcharacters of the text are used in the algorithm.
Chkpass
 has limited practical use; the
 pgcrypto
module is aneffective alternative.
Using
fuzzystrmatch
This module installs the
soundx(), difference(),levenshtein()
and
metaphone()
functions.
Soundx()
and
 metaphone()
are phonetic algorithms—they convert atext string to a code string based on its pronunciation.
 Difference()
and
levenshtein()
return a numeric valuebased on the similarity of the two input strings. Let’snow look into the
levenshtein()
and
metaphone()
 functions. The Levenshtein distance between twostrings is the minimum number of insertions, deletionsor substitutions required to convert one string toanother.
SELECT levenshtein(‘foodlets’, ‘booklets’);
This query returns 2, as is obvious.The
metaphone()
function takes a text string andthe maximum length of the output code as its two inputparameters. These examples return FTLTS:
SELECT metaphone(‘foodlets’, 6);SELECT metaphone(‘fudlets’, 6);
If we try to get the Levenshtein distance between thereturned strings, this returns 0:
SELECT levenshtein(‘FTLTS’,’FTLTS’);
This means that the two words sound similar.
 Fuzzystrmatch
is very helpful in implementing thesearch feature for a website. Now the search can work withalternate spellings and misspelled keywords. Reminds youof the ‘Did you mean...’ feature on Google Search, right?
Using
isn
This module will introduce data types to storeinternational standard numbers like International StandardBook Numbers (ISBN), International Standard MusicNumbers (ISMN), International Standard Serial Numbers(ISSN), Universal Product Codes (UPC), etc. It will alsoadd functions to validate data, type-cast numbers fromolder formats to the newer 13-digit formats, and vice-versa. Let’s test this module for storing book information:
CREATE TABLE books(number isbn13, title varchar(100))INSERT INTO books(“number”, title) VALUES (‘978-03’,‘Rework’);
The INSERT statement throws an error:
 Invalidinput syntax for ISBN number: “978-03”.
However, thisworks just fine:
INSERT INTO books(“number”, title) VALUES (‘978-0307463746’,‘Rework’)
To convert a 10-digit ISBN to 13 digits, use the
isbn13()
function:
INSERT INTO books(“number”, title) VALUES(isbn13(‘0307463745’), ‘Rework’)
(Actually, the name of the book mentioned here,'Rework' by Jason Fried, happens to be my favouritebook on product/project management! I have prescribedit to all my team-mates.)
Using
 hstore
You must have heard enough about NoSQL and key-value databases. It’s not always NoSQL vs relationaldatabases—with the
hstore
module, PostgreSQLallows you to store data in the form of key-value pairs,within a column of a table. Imagine you are processingspreadsheets and you have no idea about the columnheaders and the data type of the data in the sheets.That’s when
hstore
comes to your rescue! Incidentally,
hstore
takes keys and values as text; the value canbe NULL, but not the key. Let’s create a table with acolumn of type
hstore
and insert some rows:
CREATE TABLE kv_data( id integer, data hstore)INSERT into kv_data values(1, hstore(‘name’, ‘amit’) || hstore(‘city’, ‘bangalore’)),(2, hstore(‘name’, ‘raghu’) || hstore(‘age’, ‘26’)),(3, hstore(‘name’, ‘ram’) || hstore(‘age’, ‘28’));
You can create your own keys like
‘height’,‘favourite_book,’
etc. The ‘||’ operator is used for
www.LinuxFU.m|LINUX For YoU|March 2012| 
89

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->