r3 - 13 Feb 2007 - 13:46:13 - KritsanaMunwongYou are here: NTL >  Main Web  > Winter2549_Internship > Kritsana_Deliverable > PostgreSQL-des
-- KritsanaMunwong - 06 Feb 2007

Postgresql คืออะไร Postgresql เป็น Object-Relational DBMS โดยสามารถใช้รูปแบบของภาษา SQL ได้เกือบทั้งหมด และสามารถใช้ subselects , transactions , user-defined types และ functions ได้ อีกทั้งเป็น Database ซึ่งให้ Source code ฟรี ด้วย

ประวัติการพัฒนา

· เป็น Project ของ Prof. Michael Stonebraker ที่ มหาวิทยาลัย Berkeley

· ซึ่งเดิมพัฒนามาจาก Ingres ซึ่งเป็นที่รู้จักในชื่อ CA-Ingres II ซึ่ง Ingres ใช้ ภาษา query , QUEL เป็นภาษาของตัวเอง ปัจจุบันได้หยุดพัฒนาไปแล้ว แต่ยังสามารถนำมาใช้ได้ฟรี

· ต่อมา Prof. Stonebraker ได้นำมาพัฒนาเป็น Postgres (มาจาก after Ingres) ซึ่งได้ใช้ภาษา query เป็น POSTQUEL เป็น Postgres version 4.2

· ต่อมาในช่วง ปี 1987 Postgres ได้เสนอ rules , procedures , time travel , extensible types และ object-relational concepts

· Postgres ถูกนำมาใช้ เพื่อการค้า ในชื่อว่า Illustra (ปัจจุบัน ถูก Informix ซื้อไป และรวมเข้าไว้ใน Universal Server)

· ต่อมา นักศึกษาปริญญาเอก 2 คน คือ Andrew Yu และ Jolly Chen ได้พัฒนา Postgres ให้ใช้ภาษา query ตามรูปแบบที่เป็นมาตราฐาน แทนที่ ภาษา POSTQUEL เดิม ซึ่งได้เผยแพร่ในปี 1995 จึงเรียกเป็น Postgres95 หรือ version 5 หลังจากนั้น การพัฒนาต่อ โดย กลุ่มพัฒนาทาง Internet

· ปัจจุบัน เปลี่ยนชื่อเป็น PostgreSQL? พัฒนาต่อเนื่องเป็น version 8.2

ลักษณะโครงสร้าง

ระบบที่ใช้ PostgreSQL? จะติดตั้ง PostgreSQL? ไว้ที่เครื่อง Server ซึ่งเป็นที่เก็บ database ด้วย และยังสามารถ ติดตั้ง PostgreSQL? ได้มากกว่า 1 ชุดใน Server เครื่องเดียว

ผู้ดูแลระบบ PostgreSQL? จะใช้ชื่อว่า postgres ซึ่งเป็นผู้ดูแลทั้ง ตัวโปรแกรม และ database ซึ่งสามารถทำงานกับบางคำสั่งเฉพาะ เพื่อจัดการ database และ ผู้ใช้บริการ (user) ซึ่ง ผู้ดูแลระบบ database (postgres) จะคล้ายการทำงานของ superuser ในระบบ Unix หน้าที่ของ postgres สามารถ สร้างชื่อ user และกำหนดสิทธิและระดับการใช้งานต่างๆได้

PostgreSQL? ใช้รูปแบบการทำงาน แบบ Client/Server ซึ่งในการทำงานจะประกอบด้วย 3 process ทำงานร่วมกัน คือ

1. Postmaster เป็น supervisory daemon process ซึ่งจัดการติดต่อระหว่าง Frontend กับ Backend process ในการ allocate share buffer , จัดการค่าเริ่มต้นต่างๆในระหว่างเริ่มทำงาน และเก็บบันทึกการเข้าใช้ระบบและความผิดพลาดต่างๆที่เกิดขึ้น

2. Postgres เป็น backend process เพื่อจัดการ database ถือว่าส่วนนี้เป็น process ที่ทำงานจริงๆ เช่น ทำงานตาม query โดย Postmaster จะสั่งให้สร้าง Backend process สำหรับทุกๆ การเชื่อมต่อกับ Frontend ดังนั้น Postgres นี้จะทำงานที่ server

3. Frontend เป็น application ซึ่งจะทำงานที่เครื่อง client และจะส่งคำสั่งการเชื่อมต่อ หรือคำสั่งต่างๆ มาที่ Postmaster แล้ว Postmaster จึงส่งต่อการทำงานไปที่ Postgres

หลักการทำงานของ PostgreSQL?

การทำงานจะแบ่ง process ที่ทำงานดังที่กล่าวมาแล้ว คือ

1. ในส่วนของ Supervisory daemon process คือ Postmaster

2. ในส่วนของ User's Frontend application เช่น โปรแกรม psql หรือ CGI-Perl

3. และในส่วน Backend database servers คือ Postgres

· เมื่อโปรแกรม ทาง Frontend ต้องการข้อมูล หรือทำงานกับ database โดยเรียกผ่านทาง library libpq ซึ่ง library libpq นี้ จะส่ง requests ผ่านทาง Network ไปยัง Postmaster เมื่อ Postmaster ได้รับ request ดังกล่าว ทาง Postmaster จะสร้าง Backend process ขึ้นที่ server เพื่อติดต่อกับ Frontend แทน การทำงานนั้นจะเกิดขึ้นระหว่าง Frontend กับ Backend โดยไม่ผ่าน Postmaster อีก และ Postmaster ก็ทำงานต่อไป คือรอรับ request อื่นๆต่อไป

· Library libpq จะให้ หนึ่ง Frontend สามารถติดต่อได้หลาย Backend processes แต่การทำงานยังเป็นแบบ single threaded เนื่องจาก library libpq ยังไม่สามารถทำ multithreaded ได้

· ตามหลักการที่กล่าวมา ดังนั้น Postmaster กับ Backend จะต้องทำงานอยู่ที่ เครื่องเดียวกัน คือ database server แต่ Frontend จะทำงานที่เครื่องใดก็ได้

<IMG width="800" src='http://wiki.nectec.or.th/ntl /bin/viewfile/Main/PostgreSQL-des?rev=1;filename=im1.gif' height="300" border="0">

รูปแสดงการเชื่อมต่อระหว่าง client กับ server

การติดตั้ง PostgreSQL?

PostgreSQL? 8.1 (http://www.postgresql.org)

Compiling and installing new postgres.

1. create account postgres (and supposed with group postgres)

2. gunzip –c postgresql-v7.1 | tar xvf –

3. cd postgresql-v7.1/src

4. ./configure –prefix=/usr/local/pgsql –with-perl –with-php –with-odbc –with-tcl –with-libs=/usr/lib

5. if “—with-tcl” then add “-L/usr/lib –lcrypt”to the line SHLIB_LINK in the file Makefile under src/interfaces/libpgtcl since this is a bug in configure command)

6. make all

7. mkdir /usr/local/pgsql

8. make install

9. if “—with-perl” then cd src/interfaces/perl5 and then perl Makefile.PL and make install

10. also install man pages by – cd postgresql-v6.4.2/doc and make install

11. chown –R postgres:postgres /usr/local/pgsql

12. setenv LD_LIBRARY_PATH /usr/local/pgsql/lib

13. add /usr/local/pgsql/lib line to /etc/ld.so.conf and then run /sbin/ldconfig

14. initdb (this will create data directory under /usr/local/pgsql as /usr/local/pgsql/data and create template database named template1)

15. cp postgres.init /etc/rc.d/init.d/postgresql

16. cd /etc/rc.d/rc3.d; ln –s ../init.d/postgresql S85postgresql

17. cd /etc/rc.d/rc4.d; ln –s ../init.d/postgresql S85postgresql

18. cd /etc/rc.d/rc5.d; ln –s ../init.d/postgresql S85postgresql

19. add these lines to init script under user home directory PGLIB=/usr/local/pgsql/lib PGDATA=/usr/local/pgsql/data

การติดตั้ง phpPgAdmin
การติดตั้งโดยใช้ yum (download)
เมื่อ downlown เสร็จแล้วให้แตกไฟล์โดยใช้คำสั่ง

#tar xvf phpPgAdmin-4.x.tar.gz tar
เมื่อแตกไฟล์เสร็จแล้วทำการติดตั้งโดยใช้คำสั่ง
#yum install phpPgAdmin-4.x

การติดตั้งโดยใช้ rpm (download)

#rpm -ivh phpPgAdmin-4.x.x-x.src.rpm

การใช้คำสั่งสร้างผู้ใช้ ใน Postgres จะ user defult คือ postgres ใช้คำสั่งต่อไปนี้

#su - postgres
เสร็จแล้วกด Enter จะได้
-bash-3.1$
จากนั้นสร้าง user
-bash-3.1$ createuser nettec
กดEnter แล้วระบบจะถามว่า
Shall the new role be a superuser? (y/n) ให้ตอบ y เพื่อให้สามารถเพื่ม ลบ แก้ไข ได้ทุกอย่าง จากนั้นให้ตั้ง password โดยใช้คำสั่ง
alter user nectec password("**************")
จากนั้นให้ไปกำหนดการเข้ารหัสกำหนดความปลอดภัย โดยเข้าไปที่
#cd /var/lib/pgsql/data/
#vi pg_hba.conf
 # TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD
      
     # "local" is for Unix domain socket connections only
         local   all         all                              md5
     # IPv4 local connections:
        host    all         all         127.0.0.1/32          md5
     # IPv6 local connections:
        host    all         all         ::1/128             md5 

แก้ไข METHOD iden sameuser เป็น md5 จากนั้นใช้

#su - postgers
-bash-3.1$pg_ctl reload
คำสั่ง pg_ctl reload เป็นคำสั่งที่กระตุ้นให้ postgres ทำงาน

ศึกษาการเขียนโปรแกรมเพิ่มเติมได้ที่ http://www.commandprompt.com/community/pgdocs81/index

toggleopenShow attachmentstogglecloseHide attachments
Topic attachments
I Attachment Action Size Date Who Comment
gifgif im1.gif manage 4.8 K 06 Feb 2007 - 09:34 KritsanaMunwong  
Edit | WYSIWYG | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r3 < r2 < r1 | More topic actions
 
Powered by NTL
This site is powered by the TWiki collaboration platformCopyright © by National Electronics and Computer Technology Center, NECTEC.
Ideas, requests, problems regarding NTL? Send feedback