[PostgreSQL] 소스 컴파일 설치 방법 - 12버전

PostgreSQL 12버전에 대해서 소스 컴파일로 설치합니다. 또한 OS 가장 많이 범용으로 사용되는 CentOS 설치합니다.

소스 컴파일로 하게 되면 불필요한 코드가 제거되고 현재 시스템에 맞게 필요한 바이너리만 생성되기 때문에 속도면에서 약간 유리할 있습니다.

또한 추가 기능 설치 특정 설정에 대해 변경이 가능하기 때문에 많은 기능을 이용하고 싶다면 컴파일 방식으로 설치를 할것을 추천드립니다.

물론 RPM으로도 설치가 가능합니다. RPM설치는 다음 포스팅에서 알아보도록 하겠습니다.

 

사전 설치 패키지

PostgreSQL 설치하기 전에 시스템에 설치해 주어야 하는 필수 패키지입니다.

shell> yum install readline readline-devel
shell> yum install zlib, zlib-devel
shell> yum install openssl, openssl-devel
shell> yum install python, python-devel
shell> yum install perl systemd-devel
shell> yum install gcc gcc-c++ 
shell> yum install systemd systemd-devel
shell> yum install python python-devel
shell> yum install gettext gettext-devel autoconf wget flex flex-devel

GNU make version 3.80 혹은 높은 버전을 설치합니다.

 

■ configure 옵션

기본적인 소스 컴파일 옵션은 다음과 같습니다.

--prefix : PostgreSQL 설치할 디렉토리

--sysconfdir :  환경파일 위치. 기본값은 prefix밑의 etc디렉토리.

--datarootdir : 다양한 유형의 읽기 전용 데이터 파일에 대한 루트 디렉토리를 설정합니다. 다음 옵션 일부에 대해서만 기본값을 설정합니다. 기본값은 PREFIX / share입니다.

--datadir : 설치된 프로그램에서 사용하는 읽기 전용 데이터 파일의 디렉토리를 설정합니다. 기본값은 DATAROOTDIR입니다. 이것은 데이터베이스 파일이 설치될 위치와는 아무 관련이 없습니다.

--with-perl : perl 사용할 있도록 합니다.

--with-python : 파이선을 사용할 있도록 합니다

--with-openssl : SSL통신을 사용할 있도록 합니다.

--with-segsize : 세그먼트 크기 (GB) 설정합니다. 테이블은 각각 세그먼트 크기와 동일한 크기의 여러 운영 체제 파일로 나뉩니다. 이렇게하면 많은 플랫폼에 존재하는 파일 크기 제한 문제를 방지 있습니다. 기본 세그먼트 크기 1GB 지원되는 모든 플랫폼에서 안전합니다. 운영 체제에 "largefile"지원이있는 경우 (요즘 대부분 지원) 세그먼트 크기를 사용할 있습니다. 이는 매우 테이블로 작업 사용되는 파일 설명자 수를 줄이는 도움이 있습니다. 그러나 플랫폼 사용하려는 파일 시스템에서 지원하는 것보다 값을 선택하지 않도록주의하십시오. tar 같이 사용할 수있는 다른 도구도 사용 가능한 파일 크기에 대한 제한을 설정할 있습니다. 절대적으로 필요한 것은 아니지만이 값은 2 거듭 제곱이되는 것이 좋습니다. 값을 변경하려면 initdb 필요합니다.

--with-block-size : 블록 크기 (KB) 설정합니다. 이것은 테이블 내의 스토리지 I / O 단위입니다. 기본값 8KB 대부분의 상황에 적합합니다. 그러나 특별한 경우에는 다른 값이 유용 있습니다. 값은 1에서 32 (킬로바이트) 사이의 2 제곱이어야합니다. 값을 변경하려면 initdb 필요합니다.

--with-wal-blocksize :WAL 블록 크기를 KB 단위로 설정합니다. 이것은 WAL 로그 내의 스토리지 I / O 단위입니다. 기본값 8KB 대부분의 상황에 적합합니다. 그러나 특별한 경우에는 다른 값이 유용 있습니다. 값은 1에서 64 (킬로바이트) 사이의 2 거듭 제곱이어야합니다. 값을 변경하려면 initdb 필요합니다.

--enable-nls : 사용자가 원하는 메세지로 번역되게 합니다. 서버의 로케일 동작은 클라이언트 환경이 아니라 서버에서 표시되는 한경 변수에서 결정됩니다.

--with-systemd : CentOS 7이상에서 지원하는 systemd 사용하여 PostgreSQL시작 중지를 합니다.

 

소스 컴파일 설치

위의 configure 옵션을 이용해서 configure 컴파일을 실행합니다.

shell> tar -zxvf postgresql-12.6.tar.gz
shell> cd postgresql-12.6
shell> ./configure --prefix=/usr/local/pgsql --with-python --with-openssl --with-systemd --sysconfdir=/usr/local/pgsql/conf

 

컴파일을 진행합니다. 컴파일 방법은 2가지가 있습니다.

1. 일반설치 : configure에서 설정했던 기능을 설정

make
make check
make install

 

2. 전체 설치 : 문서를 포함하여 모든 기능을 빌드

make world
make check
make install-docs
make install-world

 

■ systemd 사용을 위한 제어 스크립트

PostgreSQL 엔진을 시작 혹은 중지를 하기 위한 스크립트를 작성합니다. --with-systemd 컴파일 옵션을 추가했을 사용합니다.

 

작성 위치 : 

/etc/systemd/system/postgresql.service

systemd 스크립트 내용 :
[Unit]
Description=PostgreSQL database server
Documentation=man:postgres(1)

[Service]
Type=notify
User=postgres
ExecStart=/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data -p 5432 -c config_file=/usr/local/pgsql/data/postgresql.conf
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=SIGINT
TimeoutSec=0

[Install]
WantedBy=multi-user.target

 

실행 환경 설정

PostgreSQL MySQL 같이 특정계정으로 실행합니다.

보통은 postgres 계정명으로 실행합니다. 내부 정책에 따라 다른계정(예를 들면 dbadmin)으로 설치하기도 합니다.

 

계정 추가 :

shell> useradd -m - s /bin/bash postgres

 

소유권 변경 :

shell> cd /usr/local
shell> chown -R postgres:postgres pgsql

 

계정 스위치 : 

shell> su - postgres

 

계정 환경 설정 :

vi .bash_profile
export LD_LIBRARY_PATH=/usr/local/pgsql/lib
export PATH=$PATH:/usr/local/pgsql/bin

 

클러스터 생성

postgresql 실행하려면 클러스터를 생성해야 합니다. 클러스터를 생성해야 비로소 Database 사용할 있습니다.

클러스터 생성 postgresql이라는 데이터베이스가 생성되는데 이것이 기본 catalog database입니다.

데이터베이스 안에는 postgresql 유저정보, Database정보, 스키마 정보등 모든 정보들이 포함되어 있습니다.

postgresql DB 생성하려면 2가지 방법이 있습니다. postgres 게정으로 변경 실행합니다.

 

1. initdb 명령을 이용하는 방법

shell> initdb -D /usr/local/pgsql/data

 

2. pg_ctl 이용하는 방법

shell> pg_ctl -D /usr/local/pgsql/data initdb

 

서버 시작 방법

postgresql 시작방법이 다양합니다. 자신이 원하는 방법으로 사용하시면 됩니다.

 

1. systemctl 이용하는 방법

위에서 작성한 postgresql.service 이용해서 시작, 중지를 하는 방법입니다. root 실행합니다.

shell> systemctl start postgresql
shell> systemctl status postgresql
● postgresql.service - PostgreSQL database server
   Loaded: loaded (/etc/systemd/system/postgresql.service; disabled; vendor preset: disabled)
   Active: active (running) since 화 2021-06-01 21:42:28 KST; 1min 24s ago
     Docs: man:postgres(1)
 Main PID: 5237 (postgres)
    Tasks: 7
   CGroup: /system.slice/postgresql.service
           ├─5237 /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data -p 5432 -c config_file=/usr/local/pgsql/data/postgresql.conf
           ├─5241 postgres: checkpointer   
           ├─5242 postgres: background writer   
           ├─5243 postgres: walwriter   
           ├─5244 postgres: autovacuum launcher   
           ├─5245 postgres: stats collector   
           └─5246 postgres: logical replication launcher   

 6월 01 21:42:28 db01 systemd[1]: Starting PostgreSQL database server...
 6월 01 21:42:28 db01 postgres[5237]: 2021-06-01 21:42:28.414 KST [5237] LOG:  starting PostgreSQL 12.6 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
 6월 01 21:42:28 db01 postgres[5237]: 2021-06-01 21:42:28.417 KST [5237] LOG:  listening on IPv6 address "::1", port 5432
 6월 01 21:42:28 db01 postgres[5237]: 2021-06-01 21:42:28.417 KST [5237] LOG:  listening on IPv4 address "127.0.0.1", port 5432
 6월 01 21:42:28 db01 postgres[5237]: 2021-06-01 21:42:28.419 KST [5237] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
 6월 01 21:42:28 db01 postgres[5237]: 2021-06-01 21:42:28.434 KST [5240] LOG:  database system was shut down at 2021-05-24 23:35:26 KST
 6월 01 21:42:28 db01 postgres[5237]: 2021-06-01 21:42:28.440 KST [5237] LOG:  database system is ready to accept connections
 6월 01 21:42:28 db01 systemd[1]: Started PostgreSQL database server.

 

2. postgres명령어를 이용하는 방법

• Foreground 실행방법

콘솔에서 바로 실행하는 방법입니다. 콘솔의 foreground 실행되는 만큼 아무런 입력도 되지 않으며 ctrl + c 입력하면 서버가 바로 중지됩니다.

shell> postgres -D /usr/local/pgsql/data

 

• Background 실행방법

shell> postgres -D /usr/local/pgsql/data > /usr/local/pgsql/log/logfile 2>&1 &

옵션 설명

-D : Data Directory

 

3. 래퍼프로그램(pg_ctl) 이용하여 시작하는 방법

• 시작
shell> pg_ctl start -D /usr/local/pgsql/data -l pglog

• 중지
shell> pg_ctl stop -D /usr/local/pgsql/data

옵션 설명

-D : Data Directory.

-l : 로그를 남길 디렉토리.

 

Designed by JB FACTORY