본문 바로가기
아이티 공부/PostgreSQL

PostgreSQL 9.4 버전 소스 컴파일을 통해 설치하는 방법

by Klero 2018. 8. 24.
PostgreSQL 9.0.0 버전 소스 컴파일을 통해 설치하는 방법


테스트 환경 :

OS - CentOS7.4 64Bit Minimal

PostgreSQL - 9.4.0 소스파일





1. postgreSQL 다운로드


- 다운로드는 공식 홈페이지인 https://www.postgresql.org/download/ 받을 수 있습니다.

또는 아래처럼 wget 명령어를 사용하여 다운로드 받을 수도 있습니다. 



2. limits.conf 파일에 내용 추가


- /etc/security/limits.conf 파일을 수정합니다.

아래처럼 postgres 사용자에 대한 설정 값을 입력해줍니다.


1
[root@Centos7 ~]# vi /etc/security/limits.conf


1
2
3
postgres        soft    nofile          65535
postgres        hard    nofile          65535
 

- soft는 soft로 설정한 용량을 넘어가면 경고 메세지를 남긴다는 뜻입니다.


- hatd는 hard로 설정한 용량을 넘을 수 없다는 뜻입니다.


- nofile은 해당 도메인(사용자, 그룹)이 오픈할 수 있는 최대 파일 개수입니다.

많은 데이터와 소켓 통신을 하는 서버일 경우 65535 또는 많이 잡으면 131072

정도면 충분하다고 합니다.



3. 커널 설정 튜닝


- /etc/sysctl.conf 파일을 수정합니다.

이 파일에 fs.file-max=65535 라고 입력합니다.

그리고 튜닝한 내용을 적용하기 위해서 sysctl -p /etc/sysctl.conf 명령어를 입력합니다.


1
2
3
4
5
6
[root@Centos7 ~]# vi /etc/sysctl.conf
fs.file-max = 65535
[root@Centos7 ~]# sysctl -p /etc/sysctl.conf
fs.file-max = 65535
[root@Centos7 ~]#



4. PostgreSQL 설치를 위한 필요 패키지들 설치


1
2
3
[root@Centos7 ~]# yum -y install gcc gcc-c++ make autoconf readline \
> readline-devel zlib zlib-devel openssl openssl-devel gettext \
> gettext-devel python python-devel wget bison flex


5. PostgreSQL 소스 파일 이동(복사) 및 압축해제


- /usr/local/src 디렉토리 안에 postgresql 디렉토리를 만들었습니다.

그리고 postgresql 파일을 postgresql 디렉토리 안에 복사하였습니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@Centos7 ~]# cd /usr/local/src/
[root@Centos7 src]# mkdir postgresql
[root@Centos7 src]# cd postgresql/
[root@Centos7 postgresql]# pwd
/usr/local/src/postgresql

[root@Centos7 postgresql]#
[root@Centos7 postgresql]# cp /root/Downloads/postgresql-9.4.0.tar.gz ./
[root@Centos7 postgresql]# ls -al
total 22224
drwxr-xr-x  2 root root       37 Aug 18 21:25 .
drwxr-xr-x. 3 root root       24 Aug 18 21:25 ..
-rw-r--r--  1 root root 22755540 Aug 18 21:25 postgresql-9.4.0.tar.gz
[root@Centos7 postgresql]#



- 아래처럼 tar 명령어를 이용하여 압축을 해제합니다.

ls -al 명령어를 통해 확인해 보면 현재 postgresql-9.4.0 디렉토리는에 대한 소유권을

가지고 있는 계정 root 인 것을 볼 수 있습니다.


1
2
3
4
5
6
7
8
[root@Centos7 postgresql]# tar xvf postgresql-9.4.0.tar.gz
[root@Centos7 postgresql]# ls -al
total 22224
drwxr-xr-x  3 root root       61 Aug 18 21:28 .
drwxr-xr-x. 3 root root       24 Aug 18 21:25 ..
drwxrwxrwx  6 1107 1107      273 Dec 16  2014 postgresql-9.4.0
-rw-r--r--  1 root root 22755540 Aug 18 21:25 postgresql-9.4.0.tar.gz
[root@Centos7 postgresql]# 



6. postgres 유저 생성 및 소유권 부여


- PostgreSQL은 보안상 이유로 root 계정이 아닌 별도 계정(postgres)로 

데이터베이스를 실행할 수 있게 되어 있습니다.

그래서 postgres 유저를 생성하고, 소유권을 넘겨줘야 합니다.


- useradd 의 -d 옵션은 postgres 유저를 생성하면서 디폴트 디렉토리를 

/home/postgres 로 하겠다는 의미입니다.


- chown -R 명령어로 /usr/local/src/postgresql 경로에서 postgresql 디렉토리와

이 디렉토리 하위에 대해서 유저와 그룹에 대한 소유권을 postgres로 넘겨줄 수 있습니다.


1
2
3
4
5
6
7
8
9
10
[root@Centos7 ~]# useradd -d /home/postgres postgres
[root@Centos7 ~]# passwd postgres
[root@Centos7 ~]# chown -R postgres:postgres /usr/local/src/postgresql/
[root@Centos7 ~]# ls -al /usr/local/src/postgresql/
total 22224
drwxr-xr-x  3 postgres postgres       61 Aug 18 21:28 .
drwxr-xr-x. 3 root     root           24 Aug 18 21:25 ..
drwxrwxrwx  6 postgres postgres      273 Dec 16  2014 postgresql-9.4.0
-rw-r--r--  1 postgres postgres 22755540 Aug 18 21:25 postgresql-9.4.0.tar.gz
[root@Centos7 ~]#



7. postgres 유저로 접속 및 사용자 환경설정 진행


- su 명령어를 통해 postgres 유저로 접속합니다.

그리고 whoami명령어로 내가 접속한 유저가 무엇인지 확인할 수 있습니다.


1
2
3
4
5
6
[root@Centos7 ~]# su - postgres
[postgres@Centos7 ~]$
[postgres@Centos7 ~]$
[postgres@Centos7 ~]$ whoami
postgres
[postgres@Centos7 ~]$ 



- 사용자 설정파일은 .bash_profile을 설정해줘야 합니다.

이 파일은 해당 사용자(postgres)가 로그인할 때 마다 이 파일을 로드하여

설정을 적용합니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[postgres@Centos7 ~]$ vi .bash_profile
[postgres@Centos7 ~]$ cat .bash_profile
# .bash_profile 
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi 
# User specific environment and startup programs 
POSTGRES_HOME=/home/postgres/pgsql
PGLIB=$POSTGRES_HOME/lib
PGDATA=$POSTGRES_HOME/data
MANPATH=$MANPATH:$POSTGRES_HOME/man
PATH=$POSTGRES_HOME/bin:$PATH:$HOME/.local/bin:$HOME/bin
 
export PATH
export POSTGRES_HOME
export PGLIB
export PGDATA
export MANPATH


8. PostgreSQL 설치


- postgresql-9.4.0 디렉토리로 이동합니다.

- 아래 configure 를 통해 소스 파일에 대해 환경설정을 해줍니다.

설치를 하기 위한 Makefile 생성이라고 볼 수 있습니다.


- configure에 대한 자세한 설정 옵션은 PostgreSQL 공식 홈페이지에서 참고해주세요.


--prefix 옵션 : 해당 디렉토리에 모든 파일을 설치한다는 의미입니다.

--enable-depend 옵션 : 자동 종속 추적을 활성화 하는 옵션입니다.

이 옵션 사용시 makefile이 셋업되므로 헤더 파일이 변경시 영향을 받는 모든 개체 파일이

리빌드 된다고 합니다. 계속 수정이 필요한 개발 작업에는 유용한 옵션이지만

한 번만 설정해서 설치하는 경우 오버헤드 낭비라고 합니다.


-- enable-nls 옵션 : 프로그램에 대한 언어 표시 기능인 NLS를 utf8로 활성화 합니다.

-- with-python 옵션 : PL/Python 서버 측 언어를 빌드하는 옵션입니다. 


1
2
3
4
5
[postgres@Centos7 ~]$ cd /usr/local/src/postgresql/postgresql-9.4.0
[postgres@Centos7 postgresql-9.4.0]$
[postgres@Centos7 postgresql-9.4.0]$
[postgres@Centos7 postgresql-9.4.0]$ ./configure --prefix=/home/postgres/pgsql \
> --enable-depend --enable-nls=utf-8 --with-python 


- make는 파일 관리 유틸리티로, 파일 간의 종속 관계를 파악해 Makefile의 내용 대로

컴파일러에게 명령하여 쉘 명령이 순차적으로 실행되게 합니다. 

즉, 환경설정한 소스를 실행하여 실행가능한 파일로 생성해줍니다.


- make install은 make를 통해 생성한 실행 가능한 파일을 실행하여 해당 디렉토리로 복사합니다.


- make clean은 Makefile을 읽어서 clean 규칙을 찾습니다. 

그리고 clean 규칙에 해당하는 내용을 실행합니다.

즉, 빌드를 했는데 뭔가 이상해서 빌드 결과물을 삭제하고 다시 빌드 하기 위해서는

make clean; make 명령어를 수행하면 됩니다.


1
[postgres@Centos7 postgresql-9.4.0]$ make && make install


9. PostgreSQL 데이터 디렉토리 생성


1
[postgres@Centos7 bin]$ mkdir -/home/postgres/pgsql/data



10. initdb를 통한 데이터 디렉토리 설정


- initdb 명령어를 통해 PostgreSQL의 데이터 디렉토리를 설정해줍니다.

정상적으로 initdb 설정이 완료되었습니다. 

아래를 보면 데이터베이스 서버를 실행할 수 있으며, 실행 방법까지 나와있습니다.


-E 옵션은 인코딩을 utf-8 로 설정하겠다는 의미입니다.

-D 옵션은 PostgreSQL 데이터베이스의 데이터 디렉토리 위치를 지정하는 옵션입니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
[postgres@Centos7 data]$ /home/postgres/pgsql/bin/initdb -E utf-8 -/home/postgres/pgsql/data
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process. 
The database cluster will be initialized with locale "en_US.UTF-8".
The default text search configuration will be set to "english". 
Data page checksums are disabled. 
fixing permissions on existing directory /home/postgres/pgsql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
creating template1 database in /home/postgres/pgsql/data/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok
syncing data to disk ... ok 
WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb. 
Success. You can now start the database server using: 
    /home/postgres/pgsql/bin/postgres -/home/postgres/pgsql/data
or
    /home/postgres/pgsql/bin/pg_ctl -/home/postgres/pgsql/data -l logfile start
 
[postgres@Centos7 data]$


11. PostgreSQL 구동 및 확인 및 종료


- pg_ctl 명령어를 통해 PostgreSQL 데이터베이스를 

시작할 수도 있고, 상태를 확인할 수도 있고, 종료할 수도 있습니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[postgres@Centos7 bin]$ pwd
/home/postgres/pgsql/bin
[postgres@Centos7 bin]$ ./pg_ctl start
server starting
[postgres@Centos7 bin]$ LOG:  database system was shut down at 2018-08-18 22:25:06 KST
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started
 
[postgres@Centos7 bin]$
[postgres@Centos7 bin]$
[postgres@Centos7 bin]$ ps -ef | grep postgres
root       1181    995  0 21:37 pts/0    00:00:00 su - postgres
postgres   1182   1181  0 21:37 pts/0    00:00:00 -bash
root       5079   5049  0 22:00 pts/0    00:00:00 su - postgres
postgres   5080   5079  0 22:00 pts/0    00:00:00 -bash
postgres   9982      1  0 22:25 pts/0    00:00:00 /home/postgres/pgsql/bin/postgres
postgres   9984   9982  0 22:25 ?        00:00:00 postgres: checkpointer process
postgres   9985   9982  0 22:25 ?        00:00:00 postgres: writer process
postgres   9986   9982  0 22:25 ?        00:00:00 postgres: wal writer process
postgres   9987   9982  0 22:25 ?        00:00:00 postgres: autovacuum launcher process
postgres   9988   9982  0 22:25 ?        00:00:00 postgres: stats collector process
postgres   9989   5080  0 22:25 pts/0    00:00:00 ps -ef
postgres   9990   5080  0 22:25 pts/0    00:00:00 grep --color=auto postgres
[postgres@Centos7 bin]$ ./pg_ctl status
pg_ctl: server is running (PID: 9982)
/home/postgres/pgsql/bin/postgres
[postgres@Centos7 bin]$ ./pg_ctl stop
waiting for server to shut down....LOG:  received smart shutdown request
LOG:  autovacuum launcher shutting down
LOG:  shutting down
LOG:  database system is shut down
 done
server stopped
[postgres@Centos7 bin]$ 


12. 패스워드 변경


- psql 명령어를 입력하면 데이터베이스에 접속이 가능합니다.

\password postgres 를 입력하여 postgres 에 대해 패스워드를 변경합니다.


1
2
3
4
5
6
7
8
9
10
[postgres@Centos7 bin]$ ./psql
psql (9.4.0)
Type "help" for help. 
postgres=#
postgres=# \password postgres
Enter new password:
Enter it again:
postgres=#
 



소스 컴파일을 통해 PostgreSQL9.4 설치를 완료하였습니다.

방화벽 및 외부 접속을 위한 설정파일 수정 작업도 있지만

PostgreSQL9.4 설치는 여기서 마무리하겠습니다.



이 글이 업무 또는 공부에 도움이 되시기를 바라겠습니다.


반응형

댓글