포스트그레스는 ftp://ftp.postgresql.org/pub 에서 postgresql-7.0.3.tar.gz를 다운받고, root 유저로 설치한다. 다음은 압축을 푸는 예이다.
$ cd /usr/local/src
$ tar zxvf postgresql-7.0.3.tar.gz
Configure , make , make install를 한다. 다음은 configure, make 및 make install 예이다.
$ cd /usr/local/src/postgresql-7.0.3/src
$ ./configure --enable-multibyte \
--enable-locale \
--with-tcl
$
$ make
$ make install
$
$ cd ../doc
$ make install
$
$ adduser postgres
$ chown -R postgres:postgres /usr/local/pgsql
환경 설정을 한다. 환경설정은 /etc/ld.so.conf 와 /etc/profile 만을 편집한다.
다음은 환경설정 예이다.
$ vi /etc/ld.so.conf
/usr/local/pgsql/lib <=마지막 라인에 추가
$
$ /sbin/ldconfig
$
$ vi /etc/profile
...
...
PGDATA=/usr/local/pgsql/data
PGLIB=/usr/local/pgsql/lib
MANPATH=$MANPATH:/usr/local/pgsql/man
PATH=$PATH:/usr/local/pgsql/bin
export PGDATA PGLIB MANPATH PATH
$
$ . /etc/profile
$
설치를 완료하면 첫 실행을 할 준비가 되었다. 다음은 실행 예이다.
$ su - postgres
$ initdb -E EUC_KR
$ pg_ctl start
initdb 는 포스트그레스의 데이타베이스들을 관리하기 위한 정보 및 데이타베이스를 저장할 디렉토리를 만들며, 포스트그레스가 기동되기전에 실행되어져야 한다. 기타 포스트그레스와 관련된 여러가지 초기화 작업을 한다.
initdb 는 환경변수 PGDATA 값을 참조하여 /pgsql_base_dir/data 디렉토리를 만들며 /pgsql_base_dir/data/base 디렉토리에 각 데이타베이스들이 저장된다.
참고로 포스트그레스의 initdb 실행 후 template1이라는 데이타베이스가 생성된다.
initlocation은 다른 PGDATA 를 정의할 수 있다. 즉 데이타베이스가 $PGDATA외의 다른 디렉토리에 저장될 수 있다. 다음은 initlocation의 활용 예 및 데이타베이스 생성 예이다.
$ su - postgres
$ export PGDTA2=/usr/local/pgsql/data2
$ export PGDTA3=/usr/local/pgsql/data3
$ export PGDTA4=/usr/local/pgsql/data4
$ initlocation PGDATA2
The location will be initialized with username "postgres".
This user will own all the files and must also own the server process.
Creating directory /usr/local/pgsql/data2
Creating directory /usr/local/pgsql/data2/base
initlocation is complete.
You can now create a database using
CREATE DATABASE <db_name> WITH LOCATION='PGDATA2';
in SQL,
or
createdb <db_name> -D 'PGDATA2'
from thr shell.
$
$ initlocation PGDATA3
The location will be initialized with username "postgres".
This user will own all the files and must also own the server process.
Creating directory /usr/local/pgsql/data3
Creating directory /usr/local/pgsql/data3/base
initlocation is complete.
You can now create a database using
CREATE DATABASE <db_name> WITH LOCATION='PGDATA3';
in SQL,
or
createdb <db_name> -D 'PGDATA3'
from thr shell.
$
$ initlocation PGDATA4
The location will be initialized with username "postgres".
This user will own all the files and must also own the server process.
Creating directory /usr/local/pgsql/data4
Creating directory /usr/local/pgsql/data4/base
initlocation is complete.
You can now create a database using
CREATE DATABASE <db_name> WITH LOCATION='PGDATA4';
in SQL,
or
createdb <db_name> -D 'PGDATA4'
from thr shell.
$
$ pg_ctl start
$
$ createdb ngd_db1 -D 'PGDATA2'
CREATE DATABASE
$
$ psql ngd_db1
...
ngd_db1=# create database test_db with location='PGDATA2';
CREATE DATABASE
ngd_db1=# \q
$
$ psql test_db
..
test_db=# \q
$
$ createdb ngd_db2 -D 'PGDATA3'
CREATE DATABASE
$
$ psql ngd_db2
...
ngd_db2=# \q
$
$ createdb ngd_db3 -D 'PGDATA4'
CREATE DATABASE
$
$ psql ngd_db3
...
ngd_db3=# \q
$
"template1" 데이타베이스는 포스트그레스의 "initdb"명령을 통해 제일 처음 생성되는 데이타베이스이다. 새로 생성되는 데이타베이스들을 위한 기본적인 틀(template) 역할을 한다.
다음의 예에서 $PGDATA 를 삭제한다. 만약 자신의 중요한 데이타를 가지고 있는 데이타베이스가 있다면 이 예는 그냥 눈으로만 보고 따라하지 말기를 바란다. -- 주의 요망
template1 은 처음 생성되는 데이타베이스에게 자신의 속성을 제공하여 상속케한다. 또한 template1이 소유한 테이블 중 사용자가 작성한 테이블이 있다면 이 또한 새로 생성되는 데이타베이스에 상속된다.
다음은 template1의 상속 예를 보여준다.
$ su - postgres
$ pg_ctl stop
$ rm -rf /usr/local/pgsql/data
$ export PGDATA2=/usr/local/pgsql/data2
$ initdb -E EUC_KR
$ initlocation PGDATA2
위의 과정을 거치면서 포스트그레스는 물리적으로 서로 다른 데이타베이스 저장소를 가진다. (PGDATA, PGDTA2)
$ pg_ctl start
$ createlang plpgsql template1
CREATE LANGUAGE
$ psql template1
...
template1=# create table temp_tab (
id_temp int4 ,
name_temp text
);
CREATE
template1=# \q
$
$ createdb ngd_db1
CREATE DATABASE
$ createdb ngd_db2 -D 'PGDATA2'
CREATE DATABASE
$ psql ngd_db1
...
ngd_db1=# \d
List of relations
Name | Type | Owner
-----------+----------+----------
temp_tab | table | postgres
(1 row)
ngd_db1=# select * from pg_database;
lananem |..| lancompiler
------------+--+--------------
internal |..| n/a
C |..| /bin/cc
sql |..| postgres
plpgsql |..| PL/pgSQL
(4 rows)
ngd_db1=# \q
$ psql ngd_db2
...
ngd_db2=# \d
List of relations
Name | Type | Owner
-----------+----------+----------
temp_tab | table | postgres
(1 row)
ngd_db2=# select * from pg_database;
lananem |..| lancompiler
------------+--+--------------
internal |..| n/a
C |..| /bin/cc
sql |..| postgres
plpgsql |..| PL/pgSQL
(4 rows)
ngd_db2=# \q
$
pg_ctl은 쉘 스크립트이며 포스트그레스의 실행,중지,재실행등을 하며 또한, 실행 상태등을 보여주는 유틸리티이다.
다음은 pg_ctl의 실행 예이다.
$ su - postgres
$ pg_ctl start
// 위의 라인은 포스트그레스를 실행한다.
$ su - postgres
$ pg_ctl -w start
// 위의 라인은 포스트그레스를 실행하지만, 바로 실행하는 것이 아니라
// 정상적으로 실행할 때까지 일정시간 대기한다.
// "-w"는 "wait"를 의미한다.
// 다음은 "pg_ctl -w start' 한 후의 예이다.
$ pg_ctl -w start
Waiting for postmaster starting up...DEBUG: Data Base System is
starting up at Tue Mar 20 01:01:50 2001
DEBUG: Data Base System was shut down at Thu Mar 15 19:29:15 2001
DEBUG: Data Base System is in production state at Tue Mar 20 01:01:50 2001
done.
postmaster successfully started up.
$
$ su - postgres
$ pg_ctl restart
// 위의 라인은 "pg_ctl"을 재실행한다.
$ su - postgres
$ pg_ctl stop
// 포스트그레스의 실행을 멈춘다.
$ su - postgres
$ pg_ctl status
//위의 라인은 "pg_ctl"의 실행 상태를 보여준다.
$ su - postgres
$ pg_ctl status
pg_ctl: postmaster is running (pid: 899)
options are:
/usr/local/pgsql/bin/postmaster
-p 5432
-D /usr/local/pgsql/data
-B 64
-b /usr/local/pgsql/bin/postgres
-N 32
$
다음은 pg_ctl status의 결과에 대한 설명이다.
$PGDATA는 일반적으로 "/usr/local/pgsql/data" 을 값으로 가진다. 이러한 $PGDATA 디렉토리에는 pg_ctl의 실행시간에 참조할 옵션들을 저장하는 파일이 있다.
바로 "postmaster.opts.default" 라는 파일이다. 이 파일에는 다음과 같은 옵션이 저장될 수 있다.
-N 64 -B 256 -o '-F -S 1024'
위의 "-N" 은 동시에 실행될 프로세스의 최대개수를 정의하며 "-B" 는 각 포로세스들이 사용할 버퍼의 크기를 정의하며, 이 때 버퍼의 크기는 최소한 프로세스 최대개수의 두배이어야 한다.
-o 는 postgres 프로세스에 대한 옵션을 정의하며 "-F" 는 각각의 트랜잭션의 결과값을 저장하기 위한 fsync() 호출을 disable 한다. 즉, -F 는 Bool 의 값을 지닌다.
"-S" 는 "odrer by" 나 "join" 등의 sorting 을 할 때 사용하는 Sorting Buffer 의 크기를 정의한다. 이 때 소팅 버퍼의 크기가 작으면, 소팅 동작중 정의된 버퍼보다 많은 크기를 요구 한다면 디스크에 나머지 필요한 버퍼를 스왑핑(swap)한다.
당연히 디스크를 버퍼로 사용하는 스왑핑이 이루어지면 소팅의 속도가 느려지게 된다.
pg_hba.conf는 포스트그레스의 사용자들의 접근 권한 설정 파일이며 $PGDATA 디렉토리에 위치한다.
기타 자세한 내용은 포스트그레스 매뉴얼이나 DSN을 참고하기 바란다.