4. @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
6. @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;
7. @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.
11. @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’);
12. @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;
13. @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
16. @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).