##
## CentOS 6.4, PostgreSQL 9.2.4 community
##
## 1. PostgreSQL 다운로드
ㅇ http://www.postgresql.org/ftp/source 에서 확인.
root# cd /usr/local/src
root# wget http://ftp.postgresql.org/pub/source/v9.2.4/postgresql-9.2.4.tar.gz
root# tar zxvf postgresql-9.2.4.tar.gz
## 2. postgres 사용자 생성
root# useradd -d /home/postgres postgres -- 사용자 등록
root# cat /etc/passwd -- 사용자 등록 확인
root# passwd postgres -- 패스워드 생성
root# pwconv -- 패스워드 적용
## 3. postgres사용자의 .bash_profile 변경
ㅇ export PATH 위치에 주의
root# su - postgres
postgres$ vi .bash_profile
# BOF
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
POSTGRES_HOME=/home/postgres/pgsql
PGLIB=$POSTGRES_HOME/lib
PGDATA=$POSTGRES_HOME/data
MANPATH=$MANPATH:$POSTGRES_HOME/man
PATH=$POSTGRES_HOME/bin:$PATH
export PATH
export POSTGRES_HOME
export PGLIB
export PGDATA
export MANPATH
# EOF
postgres$ source .bash_profile
postgres$ exit
root#
## 4. PostgreSQL 설치
root# cd /usr/local/src/postgresql-9.2.4
root# ./configure --prefix=/home/postgres/pgsql --enable-depend --enable-nls=ko --with-python
root# make -- Start Build
root# make check -- Regression Test
root# make install -- Install PostgreSQL
## 5. 데이터베이스 초기화 폴더(data) 디렉토리 추가 및 권한 부여
root# mkdir -p /home/postgres/pgsql/data
root# chown -R postgres.postgres /home/postgres/pgsql
## 6. 데이터베이스 클러스터 생성
postgres$ su - postgres
postgres$ cd /home/postgres/pgsql/bin
postgres$ ./initdb -E utf-8 -D /home/postgres/pgsql/data
## 7. 데이터베이스 가동
postgres$ cd /home/postgres/pgsql/bin
postgres$ ./postgres -D /home/postgres/pgsql/data &
또는
postgres$ ./pg_ctl -D /home/postgres/pgsql/data -l logfile start
## 8. DB 가동 확인
postgres$ ps -ef |grep postgres
postgres$ pg_ctl status
pg_ctl: server is running (PID: 18719)
/home/postgres/pgsql/bin/postgres
## 9. SuperUser 생성
postgres$ psql
postgres=# create role {account} login password '{password}' superuser;
postgres=# \q
## 10. postgresql.conf 파일 변경
postgres$ vi /home/postgres/pgsql/data/postgresql.conf 에서
#listen_addresses = 'localhost' → listen_addresses = '*' 로 변경
## 11. pg_hba.conf 파일 변경
postgres$ vi /home/postgres/pgsql/data/pg_hba.conf 에서
host all all 172.20.20.0/24 md5
host all all 192.20.20.0/24 password 추가
## 12. DB 재시작
postgres$ cd
postgres$ pg_ctl -D /home/postgres/pgsql/data -l logfile restart
## 13. 방화벽 포트 오픈
root# vi /etc/sysconfig/iptables 에서
-A INPUT -m state --state NEW -m tcp -p tcp --dport 5432 -j ACCEPT 추가
## 14. 방화벽 재시작
root# service iptables restart
## 15. 기본 접근 및 SELECT
root# su - postgres
postgres$ psql
postgres=# select * from pg_database;
## 16. 사용자 테이블스페이스 생성
root# mkdir /home/database -- 사용자 TBS Root dir.
root# mkdir /home/database/ARCHIVE -- WAL Archive dir.
root# mkdir /home/database/tbs_data0 -- TBS dir.
root# mkdir /home/database/tbs_data1 -- TBS dir.
root# mkdir /home/database/tbs_data2 -- TBS dir.
root# mkdir /home/database/tbs_data3 -- TBS dir.
root# chown postgres.postgres /home/database -- 소유자 변경
root# su - postgres
postgres$ psql
## 17. 테이블스페이스 생성
postgres=# create tablespace tbs_data0 owner sys location '/home/database/tbs_data0';
postgres=# create tablespace tbs_data0 owner sys location '/home/database/tbs_data1';
postgres=# create tablespace tbs_data0 owner sys location '/home/database/tbs_data2';
postgres=# create tablespace tbs_data0 owner sys location '/home/database/tbs_data3';
## 18. TBS 생성 확인
postgres=# select * from pg_tablespace;
## 19. 최초 PostgreSQL Cold Backup
postgres=# \q
postgres$ pg_ctl stop
postgres$ exit
root# mkdir /home/backup
root# cd /home/postgres
root# tar czf /home/backup/bak_pgsql.tar.gz ./pgsql
root# cd /home
root# tar czf /home/backup/bak_database.tar.gz ./database