Tuesday, September 10, 2013

Postgresql 9.3 and NoSQL Awesomeness on Fedora 19

Postgresql 9.3 was released yesterday. This document explains how to install postgresql 9.3 on Fedora 19 system and play with the built-in JSON support for the columns. (i.e the NOSQL Feature within a RDBMS database!)

1. Install Fedora 19 as you usually do

2. Setup PGDG Repository

[root@fallenangel ~]# yum install http://yum.postgresql.org/9.3/fedora/fedora-19-x86_64/pgdg-fedora93-9.3-1.noarch.rpm -y
oaded plugins: fastestmirror, langpacks, refresh-packagekit
pgdg-fedora93-9.3-1.noarch.rpm  | 5.1 kB  00:00:00     
Examining /var/tmp/yum-root-cjnqf8/pgdg-fedora93-9.3-1.noarch.rpm: pgdg-fedora93-9.3-1.noarch
Marking /var/tmp/yum-root-cjnqf8/pgdg-fedora93-9.3-1.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package pgdg-fedora93.noarch 0:9.3-1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved
Total size: 2.1 k
Installed size: 2.1 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : pgdg-fedora93-9.3-1.noarch         1/1 
  Verifying  : pgdg-fedora93-9.3-1.noarch                         1/1 

Installed:
  pgdg-fedora93.noarch 0:9.3-1
Complete!

3. Install postgresql-9.3
[root@fallenangel ~]# yum install postgresql93-server -y

4. Initialize the Postgresql database

[root@fallenangel ~]#  su - postgres
-bash-4.2$ env PGDATA=/var/lib/pgsql/9.3/data /usr/pgsql-9.3/bin/initdb
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/pgsql/9.3/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
creating configuration files ... ok
creating template1 database in /var/lib/pgsql/9.3/data/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    /usr/pgsql-9.3/bin/postgres -D /var/lib/pgsql/9.3/data
or
    /usr/pgsql-9.3/bin/pg_ctl -D /var/lib/pgsql/9.3/data -l logfile start

-bash-4.2$ 

5. Start the postgresql server

[root@fallenangel ~]# service postgresql-9.3 start
Redirecting to /bin/systemctl start  postgresql-9.3.service
[root@fallenangel ~]# 


6. Connect to the Server and create a test database

[root@fallenangel ~]# su - postgres
-bash-4.2$ createdb json

-bash-4.2$ psql json
psql (9.3.0)
Type "help" for help.

json=# create user json_user with password 'json_password';
CREATE ROLE
json=# grant all privileges on database json to json_user;
GRANT
json=# \q

7. Connect to the Server with newly created Username

[root@fallenangel ~]# psql -U json_user -d json
psql (9.3.0)
Type "help" for help.

8. Create a new table with JSON datatype in one of the columns

json=> create table "User" (
json(> id integer,
json(> details json
json(> );

CREATE TABLE
json=> insert into "User" (id, details) values (1, '{"uuid":1234, "name":"John Doe", "email": "John.Doe@acme.com", "status":"active"}');
INSERT 0 1

json=> insert into "User" (id, details) values (1, '{"uuid":1235, "name":"Tomcat", "email": "tomcat@acme.com", "status":"active", "phones":["+1 800 700 500", "+1 400 300 200"]}');
INSERT 0 1
json=> 
9. Now the SQL Queries using NoSQL dataset

1. Get the list of all UUIDs (embedded inside `details` column)

json=> select details->'uuid' as "uuid" from "User";
 uuid 
------
 1234
 1235
(2 rows)

2. Get the list of all Emails from "User" table

json=> select details->'email' as "email" from "User";
        email         
---------------------
 "John.Doe@acme.com"
 "tomcat@acme.com"
(2 rows)

3. Get the First Phone number (from the array of phones) from "User" table

json=> select details->'name' as "name", details->'phones'->0 as "uuid" from "User";
  name  |       uuid       
------------+------------------
 "John Doe" | 
 "Tomcat"   | "+1 800 700 500"
(2 rows)


Please be aware that JSON Implies UTF-8 Charset. There is no need to explicitly mention this.

More JSON functions in postgres-9.3 are available in this document.

LIMITATIONS: Currently, to update the column, we need to pass the entire json data instead of updating a single KEY inside JSON content.

For example, you might expect the following works, But it doesn't.

Update the 'name' key inside the json dataset.

json=> update "User" set details->'name' = 'John Doe II' where details->'name' = 'John Doe';

ERROR:  syntax error at or near "->"
LINE 1: update "User" set details->'name' = 'John Doe II' where deta..

It would be awesome if postgresql developers add such feature.

0 comments:

Post a Comment