*CentOS7에는 posgresql16의 repository를 제공하지 않습니다.
따라서 postgresql이 제공하는 소스코드를 빌드하여 컴파일 후 서버에 설치하는 방식으로 설치를 진행해야하기 때문에 해당 테스트를 진행.
PostgreSQL 설치 - Source Install(소스 설치) 방식
설치 테스트 환경
- OS : CentOS7.9
- DB : postgresql 16
테스트 전, CentOS 구축 시 외부 환경 또는 Host와 통신이 되어야합니다.
0. $yum update
1. 패키지 설치 : 우선 소스코드를 컴파일 시키기 위해서는 추가적인 패키지 설치가 필요하다.
1.1 Yum -y install gcc gcc-c++ make autoconf readline readline-devel zlib zlib-devel openssl openssl-devl gettext gettext-devel python python-devel wget
2. 소스코드 다운로드 : PostgreSQL 공식 사이트에서 Source를 다운로드한다. (wget으로 받아오거나, 직접 아래 path로 sftp를 사용하여 파일 전달을 해줄 수 있겠다.)
2.1 cd /usr/local/src
2.2 wget https://www.postgresql.org/ftp/source/v16.3/postgresql-16.3.tar.bz2
2.3 tar.gz 압축해제
i. tar -xvf postgresql-16.3.tar.bz
3. 환경설정 : Postgresql 사용자 생성 및 사용자 계정의 .bash_profile 수정
3.1 useradd -d /home/postgre ontune
3.2 passwd ontune
3.3 vi .bash_profile 수정 및 아래 내용 삽입
POSTGRE_HOME=/usr/local/pgsql # postgre 홈 디렉토리
PGLIB=$POSTGRES_HOME/lib # postgre 라이브러리 디렉토리
PGDATA=$POSTGRES_HOME/data # postgre 데이터 디렉토리 (이시점에는 data 디렉토리가 없다. 아래에서 수동으로 디렉토리 생성)
MANPATH=$MANPATH:$POSTGRES_HOME/man # postgre man 커맨드 디렉토리
PATH=$POSTGRES_HOME/bin:$PATH
export PATH
export POSTGRES_HOME
export PGLIB
export PGDATA
export MANDATA
LD_LIBRARY_PATH=/usr/local/pgsql/lib
export LD_LIBRARY_PATH
저장 후 OS적용
$ source ./.bash_profile
4. PostgreSQL 설치
4.1 cd /usr/local/src/postgresql-16.3
4.2 ./configure --prefix=/usr/local/pgsql --enable-depend --enable-nls=utf-8 --with-python
a) configure script : 컴파일러, 라이브러리, 헤더 파일 등 빌드(하기 전)에 필요한 준비물 검사 => makefile 생성 (makefile : 빌드 프로세스를 제어하는 데 사용됨)
b) --prefix : 사용자 옵션처리 옵션 (ex. 설치될 디렉토리 지정)
c) --enable-depend : 소스 파일 의존성 추적 후 변경된 파일만 다시 컴파일 하도록 설정하는 옵션
d) --with-python : python 언어로 사용
i. configure 스크립트 수행 시 에러 2가지 발생 (작성자의 경우)
1) configure: error: ICU library not found - 라이브러리 부재
a) Icu 라이브러리를 설치해준다.
i) Yum install icu libicu-devel -y
2) Python 버전 낮음
a) CentOS7에서는 기본적으로 Python -V 수행시 2.7.5 가 나온다. 즉, 버전이 낮아서 .. 빌드에 필요한 라이브러리의 부재...? 인 것 같다.
b) https://www.python.org/ftp/python/3.7.0/ -> 3.7.5 tar.xz 소스코드를 다운로드 한다.
c) Cd /python-3.7.5 경로 이동
d) ./configure --enable-optimizations 수행
e) Python install
i) Make altinstall : 기존 python 덮어쓰기하며 설치하는 커맨드(받은 소스파일을 압축을 해제하고 컴파일 및 빌드해주는 작업)
f) 설치 후 Python3.7 -V 버전 확인
i) Python -V 했을 때 2.7.5 기존 설치 버전이 나온다면 심볼릭링크를 변경해야한다.
ii) Rm /usr/bin/python # 기존 python 심볼릭링크 삭제
iii) Sudo ln -s /usr/bin/python3.7 /usr/bin/python # 신규 설치한 python 버전을 가리키는 심볼릭링크 생성
4.3 다시 4.2 커맨드를 수행한다.
4.4 소스코드 컴파일 : make
4.5 컴파일된 PostgreSQL을 시스템에 설치 : make install
5. PostgreSQL 설치 완료 후 작업 (root로 작업)
5.1 Data 디렉토리 생성 : mkdir -p /usr/local/pgsql/data
5.2 Ontune 사용자에게 권한 부여 : chown ontune:ontune /usr/local/pgsql/data
5.3 Ontune 사용자로 initdb 커맨드 실행 (데이터베이스 초기화): /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
6. PosgreSQL 실행
6.1실행 : sudo /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data start
6.2 상태 확인 : sudo /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data status
6.3 접속 : /usr/local/pg./pgsql/bin/psql -U ontune -d postgres
6.4 데이터베이스 생성 : create database ontune
6.5 접속 : ./psql -D ontune -d ontune -p5432
> initDB 수행
//initDB 수행
cd $POSTGRES_HOME/bin
$ ./initdb -D $PGDATA
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 database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /database/postgresql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Seoul
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
initdb: warning: enabling "trust" authentication for local connections
initdb: hint: 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:
pg_ctl -D /database/postgresql/data -l logfile start
// initDB 시작
$ ./pg_ctl -D $PGDATA start
waiting for server to start....2024-10-10 16:16:21.447 KST [42173] LOG: starting PostgreSQL 16.4 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-22), 64-bit
2024-10-10 16:16:21.450 KST [42173] LOG: listening on IPv6 address "::1", port 5432
2024-10-10 16:16:21.450 KST [42173] LOG: listening on IPv4 address "127.0.0.1", port 5432
2024-10-10 16:16:21.457 KST [42173] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
2024-10-10 16:16:21.467 KST [42176] LOG: database system was shut down at 2024-10-10 16:13:40 KST
2024-10-10 16:16:21.475 KST [42173] LOG: database system is ready to accept connections
done
server started
// initDB 상태 확인
$ ./pg_ctl -D $PGDATA status
pg_ctl: server is running (PID: 42173)
/database/postgresql/bin/postgres "-D" "/database/postgresql/data"
// initDB 기동 중지
$ ./pg_ctl -D $PGDATA stop
waiting for server to shut down....2024-10-10 16:18:01.835 KST [42173] LOG: received fast shutdown request
2024-10-10 16:18:01.847 KST [42173] LOG: aborting any active transactions
2024-10-10 16:18:01.850 KST [42173] LOG: background worker "logical replication launcher" (PID 42179) exited with exit code 1
2024-10-10 16:18:01.852 KST [42174] LOG: shutting down
2024-10-10 16:18:01.858 KST [42174] LOG: checkpoint starting: shutdown immediate
2024-10-10 16:18:01.918 KST [42174] LOG: checkpoint complete: wrote 42 buffers (0.3%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.016 s, sync=0.023 s, total=0.067 s; sync files=11, longest=0.006 s, average=0.003 s; distance=236 kB, estimate=236 kB; lsn=0/14BFE18, redo lsn=0/14BFE18
2024-10-10 16:18:01.923 KST [42173] LOG: database system is shut down
done
server stopped
위에는 공부하며 정리한 내용입니다.
icu 라이브러리 및 PYTHON3 버전을 설치 패키지 목록에 추가하였습니다.
리눅스 환경(python이 기본적으로 설치되어있는 환경 중)에 따라 달라질 수 있으니 python 패키지는 버전 확인 후 설치하시거나 커맨드에서 제외 후 사용하시는 것을 권장 드립니다.
트러블슈팅
[initDB 실행 전에 라이브러리 에러가 발생하는 경우]
[ontune@jwchoiTest bin]$ ldd ./initdb
linux-vdso.so.1 (0x00007ffcb37d6000)
libpq.so.5 => not found
libicui18n.so.60 => /lib64/libicui18n.so.60 (0x00007f52527d6000)
libicuuc.so.60 => /lib64/libicuuc.so.60 (0x00007f5252414000)
libicudata.so.60 => /lib64/libicudata.so.60 (0x00007f525086a000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f525064a000)
librt.so.1 => /lib64/librt.so.1 (0x00007f5250442000)
libm.so.6 => /lib64/libm.so.6 (0x00007f52500c0000)
libc.so.6 => /lib64/libc.so.6 (0x00007f524fcea000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f524fae6000)
libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007f524f751000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f524f539000)
/lib64/ld-linux-x86-64.so.2 (0x00007f5252c97000)
[postgres@ontune bin]$ ./initdb -D $PGDATA
./initdb: error while loading shared libraries: libpq.so.5: cannot open shared object file: No such file or directory
# libpq.so.5 라이브러리 찾고 심볼릭링크 추가 (기존 것이 있으면 안되니 삭제 후 다시 심볼릭링크 적용)
[root@ontune ~]# find / -name libpq.so.5
/database/postgresql/lib/libpq.so.5
[root@ontune ~]#
[root@ontune ~]# cd /database/postgresql/lib/
[root@ontune lib]# ls -arlt | grep libpq.so.5
-rwxr-xr-x. 1 postgres postgres 363864 Oct 1 14:03 libpq.so.5.16
lrwxrwxrwx. 1 postgres postgres 13 Oct 1 14:03 libpq.so.5 -> libpq.so.5.16
lrwxrwxrwx. 1 postgres postgres 13 Oct 1 14:03 libpq.so -> libpq.so.5.16
[root@ontune lib]#
[root@ontune lib]# ln -s libpq.so.5.16 libpq.so.5
ln: failed to create symbolic link 'libpq.so.5': File exists
[root@ontune lib]# rm libpq.so.5
rm: remove symbolic link 'libpq.so.5'? yes
[root@ontune lib]#
[root@ontune lib]# ln -s libpq.so.5.16 libpq.so.5
[systemd 등록시 에러가 발생할 경우]
- 환경 변수 설정
서비스 파일(/usr/lib/systemd/system/postgresql.service)에서 Environment 줄을 아래와 같이 수정하여 POSTGRES_HOME 변수를 명시적으로 설정하거나, 직접 경로를 입력해야한다.
Environment=POSTGRES_HOME=/usr/local/pgsql; PGDATA=$POSTGRES_HOME/data
하지만 두 번째 방법을 사용할 경우, POSTGRES_HOME 변수가 정의되어 있는지 확인해야한다.
[DB init이 이상할 경우]
[root@ontunev5 ~]# /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
initdb: error: cannot be run as root
initdb: hint: Please log in (using, e.g., "su") as the (unprivileged) user that will own the server process.
[root@ontunev5 ~]#
[root@ontunev5 ~]# su - postgres
Last login: Wed Oct 2 16:52:52 KST 2024 on pts/0
[postgres@ontunev5 ~]$ /usr/local/pgsql/bin/initdb -D /usr/local/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 database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /usr/local/pgsql/data ... initdb: error: could not change permissions of directory "/usr/local/pgsql/data": Operation not permitted
[postgres@ontunev5 ~]$ sudo chown -R postgres:postgres /usr/local/pgsql/data
[sudo] password for postgres:
[postgres@ontunev5 ~]$ chmod 700 /usr/local/pgsql/data
[postgres@ontunev5 ~]$ /usr/local/pgsql/bin/initdb -D /usr/local/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 database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /usr/local/pgsql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... ROK
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
initdb: warning: enabling "trust" authentication for local connections
initdb: hint: 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:
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start
[systemctl로 start가 안되는 경우]
//DB systemd 서비스 기동시 트러블슈팅
[root@ontune system]# systemctl status postgresql_ontune.service
● postgresql_ontune.service
Loaded: loaded (/usr/lib/systemd/system/postgresql_ontune.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Fri 2024-10-11 10:11:35 KST; 1min 56s ago
Process: 4026 ExecStart=/database/postgresql/bin/pg_ctl start -D ${PGDATA} -s (code=exited, status=1/FAILURE)
Oct 11 10:11:35 ontune.myguest.virtualbox.org systemd[1]: Starting postgresql_ontune.service...
Oct 11 10:11:35 ontune.myguest.virtualbox.org pg_ctl[4026]: pg_ctl: another server might be running; trying to start server anyway
Oct 11 10:11:35 ontune.myguest.virtualbox.org pg_ctl[4028]: 2024-10-11 10:11:35.770 KST [4028] FATAL: lock file "postmaster.pid" already exists
Oct 11 10:11:35 ontune.myguest.virtualbox.org pg_ctl[4028]: 2024-10-11 10:11:35.770 KST [4028] HINT: Is another postmaster (PID 2872) running in data directory "/database/postgresql/data"?
Oct 11 10:11:35 ontune.myguest.virtualbox.org pg_ctl[4026]: pg_ctl: could not start server
Oct 11 10:11:35 ontune.myguest.virtualbox.org pg_ctl[4026]: Examine the log output.
Oct 11 10:11:35 ontune.myguest.virtualbox.org systemd[1]: postgresql_ontune.service: Control process exited, code=exited status=1
Oct 11 10:11:35 ontune.myguest.virtualbox.org systemd[1]: postgresql_ontune.service: Failed with result 'exit-code'.
Oct 11 10:11:35 ontune.myguest.virtualbox.org systemd[1]: Failed to start postgresql_ontune.service.
=> 이미 DB가 initDB로 기동되었기 때문에 postmaster.pid 가 lock 상태
=> pid 찾아서 죽이고 systemctl 서비스를 start하면 된다.
'DataBase > postgreSQL' 카테고리의 다른 글
[PostgreSQL] idle in transaction 설정 (0) | 2024.10.25 |
---|---|
[PostgreSQL] initDB 실패 (0) | 2024.10.11 |
[PostgreSQL] Autovacuum이 호출되는 조건과 파라미터 (0) | 2024.08.20 |
[PostgreSQL] vacuum을 하는 이유 // vacuum, vacuum full 테스트 (0) | 2024.08.20 |
[postgreSQL] Oracle Linux 7.9 + postgreSQL 12 설치 (0) | 2024.06.04 |