brunch

You can make anything
by writing

C.S.Lewis

by Daniel Lim Jul 05. 2017

[CentOS] PostgreSQL 설치 가이드

yum 또는 source compile을 이용하는 방법

[CentOS] PostgreSQL 설치 가이드

PostgreSQL은 높은 인지도와 많은 인기를 얻는 오픈소스 RDMBS입니다. 
오랜 역사와 많은 레퍼런스를 가지기 때문에 안정성에서도 많은 신뢰를 얻는 소프트웨어입니다. 
국내에서는 MySQL을 많이 사용하지만 기업에서 배포하는 방식으로 사용 시 라이선스 문제가 있습니다. 
Postgresql의 경우 BSD와 MIT와 비슷한 Liberal Open Source License를 따릅니다.

초기 주요 설정

초기에 설치되어야 하는 모듈과 설정은 다음과 같습니다.

max open files 수 조정

기본 값으로 사용할 경우 운영시 Getting Too many Open files error for Postgres라는 메시지를 볼 수 있습니다. 
(사용자가 많을 경우 발생합니다.)  

$> vi /etc/security/limits.confpostgres        soft      nofile        65535 postgres        hard      nofile        65535

또는 리눅스 커널에서 다음과 같이 설정할 수 있습니다.  

$> vi /etc/sysctl.conffs.file-max = 65535

관련 패키지 설치

gcc 

gcc-c++ 

make 

autoconf 

readline 

readline-devel 

zlib 

zlib-devel 

openssl 

openssl-devel 

gettext 

gettext-devel 

python 

python-devel

wget

관련 패키지 설치는 yum을 이용합니다.  

$> yum -y install gcc gcc-c++ make autoconf readline readline-devel zlib zlib-devel openssl openssl-devel gettext gettext-devel python python-devel wget

설치 진행

yum을 이용한 설치

Repostitory RPM을 설치한다.

$> yum install https://download.postgresql.org/pub/repos/yum/9.5/redhat/rhel-6-x86_64/pgdg-centos95-9.5-2.noarch.rpm

Client 패키지를 설치한다.

$> yum install postgresql95

Server 패키지를 설치한다.

$> yum install postgresql95-server

설치 후 데이터 설정과 자동 시작  

# 데이터 설정
$> service postgresql-9.5 initdb
# 위의 명령어가 동작하지 않는 다면, 
$> /usr/pgsql-9.5/bin/postgresql95-setup initdb
# 서비스 등록
$> chkconfig postgresql-9.5 on
# PosgreSQL 시작
$> service postgresql-9.5 start

Source Compile을 이용한 설치 방법

PostgreSQL Source Download

PostgreSQL FIle Browser에서 원하는 버전을 확인한다.   

$> cd /usr/local/src
$> wget https://ftp.postgresql.org/pub/source/v9.5.7/postgresql-9.5.7.tar.gz
$> tar zxvf postgresql-9.5.7.tar.gz

postgres 사용자 생성  

$> useradd -d /home/postgres postgres
$> passwd postgres

postgres 계정의 .bash_profile 수정

사용자 환경설정을 진행한다.  

$> su - postgres
$> vi .bash_profile

.bash_profile 파일의 하단에 다음 내용을 추가한다.  

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

# I do not know why $PGLIB is not working. 
# but once some error(.. cannot open shared object...) occured,
# I have solved this problem with adding below two sentences.
LD_LIBRARY_PATH=/home/postgres/pgsql/lib
export LD_LIBRARY_PATH

PostgreSQL 설치  

$> cd /usr/local/src/postgresql-9.5.7
$> ./configure --prefix=/home/postgres/pgsql --enable-depend --enable-nls=utf-8 --with-python

설치 디렉터리 : /home/postgres/pgsql

./configure : 시스템에 맞추어 소스 트리를 설정(설치를 위한 Makefile 생성), 소스파일에 대한 환경 설정 명령어.

Configure Opion : http://www.postgresql.org/docs/9.5/static/install-procedure.html

Error나 Warning이 뜰 경우 "# yum install gcc bison flex"처럼 패키지 설치 후 재실행.

$> make
$> sudo make install

make : 소스를 컴파일해서 실행 가능한 파일로 생성

sudo make install : root 권한으로 make에서 만들어진 실행 가능한 파일을 실행하여 실행

데이터 디렉터리 추가 및 권한 부여  

$> mkdir -p /home/postgres/pgsql/data
$> chown -R postgres:postgres /home/postgres/pgsql

PostgreSQL 설정 데이터  

$> /home/postgres/pgsql/bin/initdb -E utf-8 -D /home/postgres/pgsql/data

-E : 인코딩 언어 셋

-D : PostgreSQL 초기 설정 데이터 위치

PostgreSQL 구동 & 종료

PorstgreSQL 구동  

$> su - postgres
$> cd /home/postgres/pgsql/bin
$> ./postgres -D /home/postgres/pgsql/data &
# 안될 시,
$> ./pg_ctl -D /home/postgres/pgsql/data -l logfile start

PostgreSQL 종료  

$> kill `cat /home/postgres/pgsql/data/postmaster.pid | head -1`

초기 설정

관리자 비밀번호 설정  

$> su - postgresql
$> psql -dpostgres

postgres=# \password postgres
Enter new password: [암호 입력]
Enter it again: [암호 재입력]
postgres=# \q

로컬 서버에서 암호를 이용한 접속으로 변경  

$> vi pg_hba.conf
# 변경 전
local all all peer
#변경 후
local all all md5

외부 접속 허용

iptables open  

$> vi /etc/sysconfig/iptables
# 추가
-A INPUT -m state --state NEW -m tcp -p tcp --dport 5432 -j ACCEPT
# iptable 재 시작
$> service iptables restart

PostgreSQL 설정 수정  

$> vi /home/postgres/pgsql/data/postgres.conf

# 변경 전
listen_addresses = 'localhost'
# 변경 후
listen_addresses = '*'

# 변경 전
#port = 5432
# 변경 후(주석제거)
port = 5432
$> vi /home/postgres/pgsql/data/pg_hba.conf
# 보통은 특정 대역을 표기 예 : `192.168.0.0/24`
# 추가 - 전체 접근 가능하도록 수정
host all all 0.0.0.0/0 password

변경 내역의 반영을 위하여 PostgreSQL 재기동 필요

DB, 사용자 추가 & 설정

Database 생성 & 조회  

$> su - postgres
$> /home/postgres/pgsql/bin/createdb [데이터베이스명]
$> /home/postgres/pgsql/bin/psql

# SQL을 이용한 데이터베이스 생성 옵션
CREATE DATABASE name
  [ [ WITH ] [ OWNER [=] user_name ]
  [ TEMPLATE [=] template ] [ ENCODING [=] encoding ]
  [ LC_COLLATE [=] lc_collate ]
  [ LC_CTYPE [=] lc_ctype ]
  [ TABLESPACE [=] tablespace_name ]
  [ CONNECTION LIMIT [=] connlimit ] ]

# 모든 데이터 베이스 목록 표출
postgres=# \l+
# 데이터 베이스 선택
postgres=# \c [데이터베이스 명]
# 테이블 목록 표출
postgres=# \d

테이블 스페이스 생성  

# linux에서 tablespace로 사용할 디렉터리 생성
$> mkdir /home/postgres/tablespace
$> /home/postgres/pgsql/bin/psql

# 여러 테이블 스페이스 추가 가능
postgres=# create tablespace data_tablespace owner sys location '/home/postgres/tablespace';
#테이블 스페이스 생성 확인
postgres=# select * from pg_tablespace;

사용자 추가  

# 사용자 추가
$> /home/postgres/pgsql/bin/createuser [아이디]
# 비밀번호 변경
$> /home/postgres/pgsql/bin/psql
postgres=# alter user [아이디] with encrypted password '[비밀번호]';
# 제한된 권한만 부여 가능 예) grant connect, temp on database [데이터베이스 명] to [아이디]
# DB 권한 부여
postgres=# grant all privileges on database [데이터베이스 명] to [아이디];

backup & restore  

# 백업
$> /home/postgres/pgsql/bin/pg_dumpall -U postgres -f [백업 파일명]
#복구
$> /home/postgres/pgsql/bin/psql -U postgres -f [백업 파일명]

참고 자료

PosgreSQL Linux Downloads

PostgreSQL 수동 설치

한눈에 살펴보는 PostgreSQL

Getting Too many Open files error for Postgres

읽기전용 POSTGRESQL 튜닝

작가의 이전글 [CentOS 6] Postgresql 소스 설치

작품 선택

키워드 선택 0 / 3 0

댓글여부

afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari