PostgreSQL (2) by Aswin

17
@agatestudio PostGreSQL Aswin Juari Knight Agate Studio

Transcript of PostgreSQL (2) by Aswin

Page 1: PostgreSQL (2) by Aswin

@agatestudio

PostGreSQL

Aswin Juari

Knight

Agate Studio

Page 2: PostgreSQL (2) by Aswin

@agatestudio @agatestudio @agatestudio

Overview

• Introduction

• Differences with MySQL

• Installation

• Unique Features

• Notes

Page 3: PostgreSQL (2) by Aswin

@agatestudio @agatestudio @agatestudio

Introduction

• Advanced Open Source SQL

– More features

• Unique Features:

– Table Inherintance

– Create Customized Function

– Etc

Page 4: PostgreSQL (2) by Aswin

@agatestudio @agatestudio @agatestudio

Differences With MySQL

PostgreSQL MySQL

Access defined by file configuration: (Edit postgresql.conf to define who can access database and from where)

Access is created by MySQL (create user ‘something’@’some_ip’)

User defined by UNIX/Server User is created with MySQL

Port: 5432 Port: 3306

Can Store Array Cannot Store Array

Page 5: PostgreSQL (2) by Aswin

@agatestudio @agatestudio @agatestudio

Installation

• What you need (Case : PHP – Postgresql)

– Postgresql

– Postgresql-server

– Php-postgresql

– Php-pdo

• Client Tools

– pgadmin

Page 6: PostgreSQL (2) by Aswin

@agatestudio @agatestudio @agatestudio

Installation (2)

• Setting:

– Service postgresql initdb

– /var/lib/pgsql9/conf/pgsql.conf

• Listen address : *

– /var/lib/pgsql9/conf/pg_hba.conf

• Set which host/user that can access postgresql

• Create db

– su – postgres

– Create database something;

– Create user some_user with password ‘some_password’;

– Grant all privileges on database “something” to some_user;

Page 7: PostgreSQL (2) by Aswin

@agatestudio @agatestudio @agatestudio

Unique Feature

• PL/PGSQL

– Scripting in database level

– Advantage: • source code in application level looks more simpler.

• Extra round trips between client and server are eliminated

• Intermediate result that client doesn’t need do not have to be transferred between client and server

• Multiple round of query parsing can be avoided.

Page 8: PostgreSQL (2) by Aswin

@agatestudio @agatestudio @agatestudio

Basic Expression PLPGSL

[ <<label>> ]

[ DECLARE declarations ]

BEGIN

statements

END

[ label ];

Page 9: PostgreSQL (2) by Aswin

@agatestudio @agatestudio @agatestudio

Sample PLSQL (Fibonacci) CREATE OR REPLACE FUNCTION fib(

fib_for integer

) RETURNS integer AS $$

DECLARE

some_var integer := 1;

BEGIN

IF fib_for < 2 THEN

RETURN fib_for;

END IF;

ret := fib(fib_for - 2) + fib(fib_for - 1);

RETURN ret;

END;

$$ LANGUAGE plpgsql;

Page 10: PostgreSQL (2) by Aswin

@agatestudio @agatestudio @agatestudio

• Capital

City_id Name State

1 Bandung Jawa Barat

2 Surabaya Jawa Timur

3 Manado Sulawesi Utara

Page 11: PostgreSQL (2) by Aswin

@agatestudio @agatestudio @agatestudio

Sample PLPSQL (2)

CREATE OR REPLACE FUNCTION test4

(state_name character varying)

RETURNS SETOF character varying AS

$BODY$

BEGIN

return query SELECT name from capital where

"state"=state_name;

END;

$BODY$

LANGUAGE plpgsql;

Select test4(‘Jawa Barat’);

Page 12: PostgreSQL (2) by Aswin

@agatestudio @agatestudio @agatestudio

Sample PGSQL (3) CREATE TYPE my_type (f1 varchar(10), f2 varchar(10), ... );

CREATE OR REPLACE FUNCTION get_obj (name text)

RETURNS my_type

AS $$

DECLARE result_record my_type;

BEGIN

SELECT f1, f2 INTO result_record.f1, result_record.f2

FROM table1 WHERE pk_col = 42;

SELECT f3 INTO result_record.f3 FROM table2

WHERE pk_col = 24;

return result_record;

END

$$ language plpgsql;

Page 13: PostgreSQL (2) by Aswin

@agatestudio @agatestudio @agatestudio

Unique Feature (2)

• Table Inheritance – Like Class Inheritance and FOREIGN KEY

– Child Table akan mewarisi atribut dari parent table

– Table Entry (Row) in child table will be inserted to parent table

– Can be multiple inheritance.

• Advantage: – The number of rows that processed can be lower

Page 14: PostgreSQL (2) by Aswin

@agatestudio @agatestudio @agatestudio

Example Table inheritance

CREATE TABLE cities (

name text,

population float,

);

CREATE TABLE capitals (

state char(2) )

INHERITS (cities);

Page 15: PostgreSQL (2) by Aswin

@agatestudio @agatestudio @agatestudio

Table Inheritance

• DELETE/UPDATE

– Delete/Update will remove/update rows from original table and child or parent table.

Page 16: PostgreSQL (2) by Aswin

@agatestudio @agatestudio @agatestudio

Notes

• Utilize Firewall (iptables) to Restrict Access

• Be careful with 8.x. (This version has no ‘create table A if not exists’

• Quote is different from double quote

• If using data binary, use ‘false’ not false, and ‘true’ not true (In PHP).

Page 17: PostgreSQL (2) by Aswin

@agatestudio @agatestudio @agatestudio

Source

• http://www.postgresql.org/docs/9.2/

• http://www.codeproject.com/Articles/33734/How-to-write-PL-pgSQL-functions-for-PostgreSQL