PostgreSQL 설치
PostgreSQL을 설치하는 방법을 정리해보자!
지원 플랫폼 및 OS
- 대부분의 플랫폼 호환성 테스트는 PostgreSQL Build Farm의 테스트 시스템에서 자동으로 수행됩니다.
PostgreSQL BuildFarm
The PostgreSQL build farm is a distributed system for automatically testing changes in the source code for PostgreSQL as they occur, on a wide variety of platforms. This server is the central repository for the results of those tests. To see the current st
buildfarm.postgresql.org
| 지원 CPU | x86, x86_64, IA64, PowerPC, PowerPC 64, S/390, S/390x, Sparc, Sparc 64, ARM, MIPS, MIPSEL 및 PA-RISC |
| 지원 OS | Linux(모든 최신 배포판), Windows(XP 이상), FreeBSD, OpenBSD, NetBSD, macOS, AIX, HP/UX 및 Solaris |
패키지
설치 전에 필수 패키지와 선택 패키지를 설치합니다.
필수 패키지
yum install -y make gcc gcc-c++ tar zlib zlib-devel readline readline-devel gettext gettext-devel git libxslt libicu
선택 패키지
yum install -y perl perl-libs tcl python python-devel python3-libs openssl openssl-devel
최소 설치
최소 설치 사양
- CPU core : 1core
- Memory : 300MB
- Disk 공간 : 750MB
- 컴파일 시 source : 350MB
- PostgreSQL 설치 디렉토리 : 60MB
- Database Cluster : 40MB
- regression Test : 300MB
최소 설치 방법 - Linux 소스 설치
| OS | CentOS 7.9 |
| PostgreSQL Version | 14.2 |
PostgreSQL: File Browser
www.postgresql.org
공식 홈페이지에서 제공하는 소스파일을 다운 받아 압축해제까지 해줍니다. 나는 14.2 버전을 설치해주었다!
cd postgresql-14.2
./configure
make 수행전 configure를 수행하면 여러 테스트를 통해 OS 종속변수의 값이나 문제점을 감지하고 빌드트리에 여러 파일을 생성하여 발견된 내용을 기록한다고 한다. 그 다음 빌드를 실행해준다.
make
make install
유저 생성
PostgreSQL을 사용할 유저와 데이터베이스 클러스터 디렉토리를 생성합니다.
adduser test
mkdir /test/pg/14/data # 데이터베이스 클러스터로 사용할 디렉토리
chown test:test -R /test
다음 test 유저로 변경하여, 데이터베이스 클러스터를 생성해주고 PostgreSQL을 기동합니다.
su - test
# 데이터베이스 클러스터 생성
/usr/local/pgsql/bin/initdb -D /test/pg/14/data
# PostgreSQL 기동
/usr/local/pgsql/bin/pg_ctl -D /test/pg/14/data -l logfile start
# testdb 라는 이름의 데이터베이스 생성
/usr/local/pgsql/bin/createdb testdb
# testdb 데이터베이스로 PostgreSQL 기동
/usr/local/pgsql/bin/psql testdb
이렇게 하면 소스로 PostgreSQL을 설치하여 데이터베이스를 생성 후, 접속까지 한 것입니다!!!!!
다음은 조금 더 편하게 설치해보겠습니다.
Linux 환경에 PostgreSQL 설치
| OS | Centos 7.9 |
| PostgreSQL Version | 14.2 |
필수 패키지
yum install -y make gcc gcc-c++ tar zlib zlib-devel readline readline-devel gettext gettext-devel git libxslt libicu
선택 패키지
yum install -y perl perl-libs tcl python python-devel python3-libs openssl openssl-devel
Repository 설치
postgresql 설치 및 업데이트에 필요한 패키지들을 모아놓은 repository인 PGDG repository를 설치해줍니다.
yum install -y <https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm>
PostgreSQL 설치
- YUM 설치
yum install -y postgresql14 postgresql14-libs postgresql14-server postgresql14-contrib
- RPM 설치
Index of /pub/repos/yum/
download.postgresql.org
rpm -ivh <https://yum.postgresql.org/14/redhat/rhel-7-x86_64/postgresql14-libs-14.2-1PGDG.rhel7.x86_64.rpm>
rpm -ivh <https://yum.postgresql.org/14/redhat/rhel-7-x86_64/postgresql14-14.2-1PGDG.rhel7.x86_64.rpm>
rpm -ivh <https://yum.postgresql.org/14/redhat/rhel-7-x86_64/postgresql14-server-14.2-1PGDG.rhel7.x86_64.rpm>
rpm -ivh <https://yum.postgresql.org/14/redhat/rhel-7-x86_64/postgresql14-contrib-14.2-1PGDG.rhel7.x86_64.rpm>
| 패키지명 | 설명 |
| postgresql14-libs-14.2-1PGDG.rhel7.x86_64.rpm | 모든 postgresql 클라이언트에 필요한 공유 라이브러리 |
| postgresql14-14.2-1PGDG.rhel7.x86_64.rpm | postgresql 클라이언트 프로그램 및 라이브러리 |
| postgresql14-server-14.2-1PGDG.rhel7.x86_64.rpm | postgresql 서버를 만들고 실행하는 데 필요한 프로그램 |
| postgresql14-contrib-14.2-1PGDG.rhel7.x86_64.rpm | postgresql과 함께 배포되는 기여 소스 및 바이너리 |
- rpm 설치 시 반드시 순서대로 설치해주셔야됩니다!!!
- 14.2 버전 외 다른 rpm 파일은 다음 링크에서 참고하길 바랍니다.
yum install -y postgresql14 postgresql14-libs postgresql14-server postgresql14-contrib
OS 유저와 그룹 생성
groupadd test
useradd test -g test -c 'PostgreSQL TEST Database' -d /test
/var/run 내의 디렉토리는 휘발성으로 삭제되기 때문에 이를 방지하기 위해 다음 명령어를 입력합니다.
echo "d /run/test 0755 test test -" > /usr/lib/tmpfiles.d/test.conf
디렉토리 생성
다음 용도로 사용될 디렉토리를 생성 후, test유저에게 소유권을 부여해보겠습니다.
| 경로 | 설명 |
| /test/pg/14/data | PostgreSQL Database Cluster 경로 |
| /test/pg/14/log/pg_log | PostgreSQL 관련 log 중에 PostgreSQL의 log 경로 |
| /test/pg/14/pg_wal | PostgreSQL의 wal파일이 저장되는 경로 |
| /test/pg/14/archive | PostgreSQL의 archive파일이 저장되는 경로 |
| /var/run/test | PostgreSQL 통신을 위한 socket이 저장되는 경로 |
mkdir -p /test/pg/14/data
mkdir -p /test/pg/14/log/pg_log
mkdir -p /test/pg/14/pg_wal
mkdir -p /test/pg/14/archive
mkdir -p /var/run/test
chown -R test:test /test
chown -R test:test /var/run/test
방화벽 설정
PostgreSQL 데이터베이스는 기본적으로 5432 포트를 사용합니다. 저는 테스트하기 편하게 하기 위해서 방화벽을 아예 내리고 했습니다 ㅎㅎ
firewall-cmd --permanent --zone=public --add-port=5432/tcp
firewall-cmd --reload
환경 변수 설정
test 계정으로 전환하여 bash_profile에 postgresql 환경 변수와 자주 사용할 ALIAS를 저장합니다.
vi ~/.bash_profile
#ref : <https://www.postgresql.org/docs/current/libpq-envars.html>
# PostgreSQL ENV
PS1="[\\u@\\h:\\W]$ "
export PGVERSION=14
if [ -f "/usr/pgsql-$PGVERSION/bin/pg_ctl" ]; then
export PGINST=/usr/pgsql-$PGVERSION #postgresql binary installed path
export MANPATH=/usr/pgsql-$PGVERSION/share/man:$MANPATH #postgresql Man path
export LD_LIBRARY_PATH=/usr/pgsql-$PGVERSION/lib:$LD_LIBRARY_PATH #ld library path
export PATH=/usr/pgsql-$PGVERSION/bin:$PATH #postgresql binary path
export PGLIB=/usr/pgsql-$PGVERSION/lib #postgresql libary path
fi
export PGHOME=/test/pg #engine directory
export PGDATA=/test/pg/$PGVERSION/data #database clsuter directory
export PGHOST=/var/run/test #socket directory
export PGUSER=postgres #Basic DB User Name
#PostgreSQL ALIAS
alias pginst="cd $PGINST"
alias pglib="cd $PGLIB"
alias pghome="cd $PGHOME"
alias pgdata="cd $PGDATA"
alias pglog="cd $PGHOME/$PGVERSION/log/pg_log"
alias pgwal="cd $PGHOME/$PGVERSION/pg_wal"
alias pgconf="vi $PGDATA/postgresql.conf"
alias pghba="vi $PGDATA/pg_hba.conf"
source ~/.bash_profile
Database Cluster 생성
initdb를 통해 PostgreSQL의 데이터베이스 클러스터를 생성합니다. 위에서 설정한 환경변수를 이용해서 편하게 할 수 있습니다!
initdb -U postgres -D $PGDATA -X /test/pg/$PGVERSION/pg_wal
위 커맨드를 입력하면 아래와 같이 클러스터를 생성하는 과정을 보여주며 생성됩니다.
The files belonging to this database system will be owned by user "test".
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 "U TF8".
The default text search configuration will be set to "englis h".
Data page checksums are disabled.
fixing permissions on existing directory /test/pg/14/data .. . ok
fixing permissions on existing directory /test/pg/14/pg_wal ... 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 c onnections
You can change this by editing pg_hba.conf or using the opti on -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 /test/pg/14/data -l logfile start
postgresql.conf 파라미터 수정
가장 중요한 postgresql.conf 파라미터를 수정하여 PostgreSQL을 시작할 준비를 완료해보도록 하겠습니다. 우선 가장 기본적인 파라미터만 해보겠습니다.
cat << EOF >> $PGDATA/postgresql.conf
listen_addresses = '*'
port = 5432
unix_socket_directories = '/var/run/test'
logging_collector = on
log_directory = '/test/pg/14/log/pg_log'
log_filename = 'postgresql-%Y-%m-%d-%H%M%S.log'
EOF
- archive mode 설정[선택사항]
mkdir /test/pg/14/archive cat << EOF >> $PGDATA/postgresql.conf archive_mode = on archive_command = 'cp "%p" "/test/pg/14/archive/%f"' EOF - pg_wal에 쌓이는 wal 파일을 주기적으로 보관하기 위해 archive를 설정할 경우, 디렉토리를 생성 하고 archive_command를 설정합니다.
저는 다음과 같이 archive_mode 와 archive_command를 설정했습니다.
cat << EOF >> $PGDATA/postgresql.conf
archive_mode = on
archive_command = '/bin/ture'
EOF
📌 archive_mode를 변경할 때 PostgreSQL을 재기동 해야됩니다. 만약 PostgreSQL을 운영중인 데이터베이스로 사용 중이거나 재기동을 못하는 상황일때는 archive_mode 를 변경하는 데에 어려움을 겪게 됩니다. 그러나 archive_command 파라미터는 변경 시 재기동이 아니라 config파일을 다시 load해주기만 하면 되기 때문에 ‘/bin/ture’ 로 사용 하다가 command만 archive 디렉토리로 변경하는 방법을 사용하는 것도 괜찮을 것 같습니다!
PostgreSQL 서버 실행 및 종료
$PGDATA로 데이터클러스터를 명시하여 서버를 실행 및 종료합니다.
# PostgreSQL 서버 실행
pg_ctl start -D $PGDATA
# PostgreSQL 서버 종료
pg_ctl stop -D $PGDATA
psql 툴을 이용한 PostgreSQL 접속
-U 옵션을 통해 postgres 유저로 기본으로 잡혀있는 postgres 데이터베이스로 접속한다는 의미입니다.
psql -U postgres
psql (14.2)
Type "help" for help.
postgres=#
'PostgreSQL' 카테고리의 다른 글
| PostgreSQL 14 Streaming Replication (0) | 2023.06.03 |
|---|---|
| PostgreSQL 14.2 File-based Log Shipping Replication - Failover (0) | 2023.06.02 |
| PostgreSQL 14.2 File-based Log Shipping Replication 구축하기 (2) | 2023.06.02 |
| PostgreSQL의 고가용성 | high availability of PostgreSQL (0) | 2023.06.01 |
| CentOS7.9에서 설치한 PostgreSQL 제거하기 | Uninstalling PostgreSQL from CentOS 7.9 (0) | 2023.06.01 |