1. CentOS에 PostgreSQL 설치

설치 환경

  • NHN Cloud 인스턴스 또는 Docker 컨테이너
  • CentOS 7.9

 

Workflow

  1. 스크립트 실행시 설치하고자 하는 버전이 입력되었는지 확인
  2. 설치하고자 하는 PC의 OS, 아키텍처가 적합한지 확인
  3. CentOS 7인지 확인
  4. PostgreSQL이 이미 설치된 경우 제거
  5. 필요한 패키지 설치 및 yum 레포지토리 업데이트
  6. PostgreSQL 설치
  7. 초기화 및 유저 비밀번호 설정
  8. 외부 접속을 위한 pg_hba.conf와 postgresql.conf 수정 및 재시작
  9. PostgreSQL 실행

 

설치 스크립트

#!/usr/bin/env bash

# 설치하고자 하는 버전
version=$1
major_version=${version%%.*}  # 15.2 -> 15

# 설치하려는 버전 번호를 받았는지 확인
if [ $# -eq 0 ]; then
    echo -e "\033[31;1m>> Error: Version number is missing. Usage: ./install_postgres_yum.sh <version>\033[0m"
    exit 1
fi

# OS가 Linux x86_64인지 확인
if [ "$(uname -s)" != "Linux" ]; then
    echo -e "\033[31;1m>> Error: Your OS is not supported.\033[0m"
    exit 1
fi
if [ "$(uname -m)" != "x86_64" ]; then
    echo -e "\033[31;1m>> Error: Your architecture is not supported.\033[0m"
    exit 1
fi

# CentOS가 Enterprise Linux 7 (EL7)인지 확인
if [ "$(rpm -q --queryformat '%{VERSION}' centos-release)" != "7" ]; then
    echo -e "\033[31;1m>> Error: This script is only compatible with Enterprise Linux 7 (EL7)\033[0m"
    exit 1
fi

# PostgreSQL이 이미 설치되어 있다면 제거
if yum list installed | grep postgresql; then
    echo -e "\033[31;1m>> Remove PostgreSQL already installed...\033[0m"
    sudo yum -y remove postgresql*
fi

# 필요한 패키지 설치
echo -e "\033[31;1m>> Install required packages...\033[0m"
yum install -y sudo
sudo yum -y update
sudo yum install -y epel-release
sudo yum install -y zstd

# PostgreSQL 설치
echo -e "\033[31;1m>> Install PostgreSQL...\033[0m"
sudo yum install -y https://download.postgresql.org/pub/repos/yum/${major_version}/redhat/rhel-7-x86_64/postgresql${major_version}-libs-${version}-1PGDG.rhel7.x86_64.rpm
sudo yum install -y https://download.postgresql.org/pub/repos/yum/${major_version}/redhat/rhel-7-x86_64/postgresql${major_version}-${version}-1PGDG.rhel7.x86_64.rpm
sudo yum install -y https://download.postgresql.org/pub/repos/yum/${major_version}/redhat/rhel-7-x86_64/postgresql${major_version}-server-${version}-1PGDG.rhel7.x86_64.rpm

# 초기화
echo -e "\033[31;1m>> Initialize PostgreSQL...\033[0m"
sudo /usr/pgsql-${major_version}/bin/postgresql-${major_version}-setup initdb

# 시작
echo -e "\033[31;1m>> Start PostgreSQL...\033[0m"
sudo systemctl start postgresql-${major_version}
sudo systemctl enable postgresql-${major_version}

# 유저 생성
username="postgres"
password="nhnpostgres123"

sudo adduser $username
echo "$username:$password" | sudo chpasswd

# postgres user로 변경 후 설정파일 수정
echo -e "\033[31;1m>> Edit pg_hba.conf and postgresql.conf\033[0m"
sudo su - postgres <<EOF
    echo "host    all             all             133.186.251.35/22       md5" >> /var/lib/pgsql/${major_version}/data/pg_hba.conf

    sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/g" /var/lib/pgsql/${major_version}/data/postgresql.conf

    exit
EOF

# 재시작
echo -e "\033[31;1m>> Restart PostgreSQL...\033[0m"
sudo systemctl restart postgresql-${major_version}

# 실행
echo -e "\033[31;1m>> Run PostgreSQL...\033[0m"
sudo su - postgres -c "psql"

 

+ Recent posts