1. PostgreSQL repository 설치
$ sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
$ sudo yum update -y
2. PostgreSQL repository 활성화 확인
$ sudo yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.kakao.com
* epel: mirror-kr.misakamikoto.network
* extras: mirror.kakao.com
* updates: mirror.kakao.com
repo id repo name status
base/7/x86_64 CentOS-7 - Base 10,072
epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 13,747
extras/7/x86_64 CentOS-7 - Extras 515
pgdg-common/7/x86_64 PostgreSQL common RPMs for RHEL / CentOS 7 - x86_64 383
pgdg10/7/x86_64 PostgreSQL 10 for RHEL / CentOS 7 - x86_64 1,153
pgdg11/7/x86_64 PostgreSQL 11 for RHEL / CentOS 7 - x86_64 1,383
pgdg12/7/x86_64 PostgreSQL 12 for RHEL / CentOS 7 - x86_64 989
pgdg13/7/x86_64 PostgreSQL 13 for RHEL / CentOS 7 - x86_64 736
pgdg14/7/x86_64 PostgreSQL 14 for RHEL / CentOS 7 - x86_64 465
pgdg15/7/x86_64 PostgreSQL 15 for RHEL / CentOS 7 - x86_64 177
updates/7/x86_64 CentOS-7 - Updates 4,425
repolist: 34,045
3. PostgreSQL 설치
$ sudo yum -y install postgresql13 postgresql13-server
4. 설치 된 패키지 확인
$ rpm -qa | grep postgresql
postgresql13-libs-13.9-1PGDG.rhel7.x86_64
postgresql13-server-13.9-1PGDG.rhel7.x86_64
postgresql13-13.9-1PGDG.rhel7.x86_64
5. 기본 Database 생성
initdb
명령어를 통해 기본 데이터베이스를 설치합니다. 기본 데이터베이스는 postgres
라는 이름으로 생성됩니다.
$ sudo /usr/pgsql-13/bin/postgresql-13-setup initdb
Initializing database ... OK
6. 서비스 실행 및 상태 확인
$ sudo systemctl start postgresql-13
$ sudo systemctl status postgresql-13
재부팅 시 PostgreSQL 서비스가 시작되도록 서비스 등록
$ sudo systemctl enable postgresql-13
Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-13.service to /usr/lib/systemd/system/postgresql-13.service.
7. 방화벽 확인
$ netstat -ntlp | grep
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 29298/postmaster
tcp6 0 0 ::1:5432 :::* LISTEN 29298/postmaster
PostgreSQL의 기본 포트는 5432이며, Local Address 가 127.0.01 이면 내부에서만 접속이 가능합니다.
8. Listen address 및 Port 변경
$ vi /var/lib/pgsql/13/data/postgresql.conf
...
#listen_address = 'localhost'
listen_address = '*'
#port = 5432
port = 54322
...
listen_address = '*'
로 변경해주어 모든 외부 주소에서 접근을 허용합니다.
port = 5432
를 원하는 포트로 설정합니다.
9. PostgreSQL 재실행
$ sudo systemctl restart postgresql-13
10. 사용자 추가
$ sudo -u postgres createuser --interactive username
$ sudo -u postgres psql
ALTER USER username WITH ENCRYPTED PASSWORD 'password';
* Port 번호를 변경해서 서비스를 하려고 하는 경우 아래와 같이 오류가 발생할 수 있습니다.
createuser: error: could not connect to database template1: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
$ sudo -u postgres createuser -p 54322 --interactive kbpa
$ sudo -u postgres psql -p 54322
이렇게 실행할 때 변경된 포트를 옵션으로 추가하면 데이터베이스에 접속이 가능합니다.
11. 사용자 인증 설정
$ vi /var/lib/pgsql/13/data/pg_hba.conf
- 전체 사용자 허용
...
# TYPE DATABASE USER ADDRESS METHOD
...
host all all 0.0.0.0/0 trust
...
- 특정 사용자 허용
...
# TYPE DATABASE USER ADDRESS METHOD
...
host all username 0.0.0.0/0 md5
...
METHOD
옵션 중 trust
는 postgres 계정 접속 시 비밀번호 입력 없이 접속이 가능합니다.
METHOD를 md5
로 변경하면 사용자를 생성할 때 입력한 패스워드로 인증을 하여 접속이 가능합니다.
PostgreSQL를 재실행하여 설정을 적용해줍니다.
12. Database 생성 (한글 collation 적용)
CREATE DATABASE database_name
OWNER=owner_name
TEMPLATE=template0
ENCODING='UTF8'
LC_COLLATE='C'
LC_CTYPE='C';
'Server' 카테고리의 다른 글
[CentOS] Jenkins 설치방법 (0) | 2022.12.13 |
---|---|
[CentOS] Apache Maven 설치 방법 (0) | 2022.12.12 |
[CentOS] Apache 설치 및 Tomcat 연동(mod_jk) (0) | 2022.12.10 |
[CentOS] Tomcat9 설치 방법 (0) | 2022.12.09 |
[CentOS] Java OpenJDK 11 설치 및 환경변수 설정 (0) | 2022.12.08 |