don't stop believing

PostgreSQL 설치 (on CentOS7) 본문

Database/PostgreSQL

PostgreSQL 설치 (on CentOS7)

Tongchun 2019. 4. 30. 16:11

CentOS7에 PostgreSQL을 설치하겠습니다.

 

OS 버전부터 확인하고 가겠습니다.

$ cat /etc/os-release
NAME="Amazon Linux"
VERSION="2"
ID="amzn"
ID_LIKE="centos rhel fedora"
VERSION_ID="2"
PRETTY_NAME="Amazon Linux 2"
ANSI_COLOR="0;33"
CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2"
HOME_URL="https://amazonlinux.com/"

AWS EC2 인스턴스 입니다. centos rhel fedora네요.

먼저 yum update를 해줍니다.

$ sudo yum update -y

Postgresql을 설치합니다.

$ sudo yum install -y postgresql postgresql-server postgresql-devel postgresql-contrib postgresql-docs

Postgresql을 초기화해 줍니다.

$ sudo service postgresql initdb

Postgresql을 시작해줍니다. 그리고 서버 재시작 후에도 자동으로 실행될 수 있도록 설정해 줍니다.

$ sudo systemctl start postgresql
$ sudo systemctl enable postgresql

 

이제 postgres계정으로 postgresql에 접속합니다.

$ sudo -u postgres psql
could not change directory to "/home/ec2-user"
psql (9.2.24)
Type "help" for help.

postgres=#

먼저 해줄것은 postgresql 기본 계정인 postgres의 비번을 설정하는 것입니다.

저는 ngle123456이라고 비번을 정했습니다.

postgres=# ALTER USER postgres WITH PASSWORD 'ngle123456';
ALTER ROLE

이제 실제 사용할 계정과 database를 만들어 줍니다.

계정은 ngle이라고 하고 superuser 권한을 줍니다. 그리고 비번도 정해줍니다.

postgres=# CREATE USER ngle SUPERUSER;
CREATE ROLE
postgres=# ALTER USER ngle WITH PASSWORD 'ngle123456';
ALTER ROLE

database를 만들고 ngle계정에 권한을 줍니다. 저는 ngledb라는 database를 만들었습니다.

database를 만들때 한가지 주의할 점은 encoding입니다. 기본 encoding은 SQL_ASCII입니다. UTF8로 지정하려면 database를 생성할 때 설정해 줘야 합니다. 그렇지 않으면 database를 삭제하고 다시 만들어야하는 귀찮음이 발생합니다. 그리고 UTF8로 지정하려면 template을 template0으로 지정해야 합니다.

postgres=# create database ngledb with owner ngle encoding 'UTF8' template template0;
CREATE DATABASE

database 생성과 권한(소유권)변경이 잘 되었는지 \l 명령으로 확인해 봅니다.

postgres=# \l
                              List of databases
    Name    |  Owner   | Encoding  | Collate | Ctype |   Access privileges   
------------+----------+-----------+---------+-------+-----------------------
 ngledb     | ngle     | UTF8      | C       | C     | 
 postgres   | postgres | SQL_ASCII | C       | C     | 
 template0  | postgres | SQL_ASCII | C       | C     | =c/postgres          +
            |          |           |         |       | postgres=CTc/postgres
 template1  | postgres | SQL_ASCII | C       | C     | =c/postgres          +
            |          |           |         |       | postgres=CTc/postgres
(4 rows)

 

이번에는 외부에서 postgresql에 접속할 수 있도록 설정합니다.

먼저 pg_hba.conf 파일을 수정하겠습니다. 파일을 열고 아래쪽에 있는 # IPv4 local connections: 부분을 아래와 같이 수정합니다.

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             ngle            0.0.0.0/0               md5
host    all             postgres        0.0.0.0/0               md5
# IPv6 local connections:
host    all             all             ::1/128                 ident

다음으로 postgresql.conf 파일을 수정합니다.

postgresql.conf 파일의 59라인의 내용(#listen_addresses = 'localhost')을 아래와 같이 어디에서든 접속할 수 있도록 수정합니다.

listen_addresses = '*'

두 개의 conf 파일이 수정되었다면 postgresql을 재시작해 줍니다.

$ sudo systemctl restart postgresql

 

여기까지 CentOS에 postgresql 설치였습니다.

 

'Database > PostgreSQL' 카테고리의 다른 글

Query를 Log 파일에 남겨 봅시다.  (0) 2018.03.06
외부 서버에서 postgresql 접속하기  (1) 2017.09.27
sql 파일 실행시키기  (0) 2017.09.27
alter table 사용하기  (0) 2017.09.26
Table 관련 기본 다루기  (0) 2017.09.26
Comments