본문 바로가기

Database/PostgreSQL

Ubuntu 18.04에 PostgreSql 11 설치

반응형

Ubuntu하고는 인연이 없었는데, 어쩌다가 Ubuntu에 서비스 환경을 구축하게 됐다. 그 와중에 PostgreSQL 설치하게 됐는데 언젠가 다시 Ubuntu를 만날 때를 대비해 기록해 놓는다. 

1. 저장소 추가

RedHat 계열에서 사용되는 Yum Repository 추가/변경 하듯이, Ubuntu에서도 PostgreSQL을 설치하려면 PostgreSQL을 위한 APT Repository를 새로 추가해야 한다. 아래는 PostgreSQL 11이 설치가 되지 않아 Repository를 추가하는 과정을 기록한 것이다.

# 현재 Repository에 PostgreSQL Package가 없어 Install 실패하고 있다.
browndwarf@ubuntu-server:~/work$ sudo apt-get install postgresql-11
패키지 목록을 읽는 중입니다... 완료
의존성 트리를 만드는 중입니다       
상태 정보를 읽는 중입니다... 완료
E: postgresql-11 패키지를 찾을 수 없습니다
...

# root user로 권한 변경
browndwarf@ubuntu-server:~/work$ su
Password: 

# postgresql 설치를 위한 Repository 추가
root@ubuntu-server:/home/browndwarf/work# cd /etc/apt/
root@ubuntu-server:/etc/apt# ls
apt.conf.d  auth.conf.d  preferences.d  sources.list  sources.list.d  trusted.gpg.d
root@ubuntu-server:/etc/apt# cd sources.list.d/
root@ubuntu-server:/etc/apt/sources.list.d# vi pgdg.list
...
# vi에서 아래 Line 추가 후 저장 (LTS 18.04의 경우에는 'bionic' 으로 사용)
deb http://apt.postgresql.org/pub/repos/apt/ bionic-pgdg main
...

# Repository 확인
root@ubuntu-server:/etc/apt# cat /etc/apt/sources.list.d/pgdg.list 
deb http://apt.postgresql.org/pub/repos/apt/ bionic-pgdg main
...

# Signing Key 등록
root@ubuntu-server:/etc/apt# wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
OK

# PostgreSQL 11 설치를 위한 key를 등록하고 Repository를 Update 한다
# 아래 Highlight된 부분이 PostgreSQL repository이다.
root@ubuntu-server:/etc/apt# sudo apt-get update
기존:1 http://kr.archive.ubuntu.com/ubuntu bionic InRelease                                                             
받기:2 http://kr.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]                                                       
받기:3 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]                                                    
받기:4 http://kr.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]                                                     
받기:5 http://apt.postgresql.org/pub/repos/apt bionic-pgdg InRelease [46.3 kB]                      
받기:6 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [632 kB]                           
받기:7 http://kr.archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [851 kB]                          
받기:8 http://apt.postgresql.org/pub/repos/apt bionic-pgdg/main amd64 Packages [178 kB]                 
받기:9 http://kr.archive.ubuntu.com/ubuntu bionic-updates/main i386 Packages [641 kB]                   
받기:10 http://kr.archive.ubuntu.com/ubuntu bionic-updates/main Translation-en [297 kB]                                        
받기:11 http://kr.archive.ubuntu.com/ubuntu bionic-updates/main amd64 DEP-11 Metadata [294 kB]                                      
받기:12 http://kr.archive.ubuntu.com/ubuntu bionic-updates/main DEP-11 48x48 Icons [73.8 kB]                                        
받기:13 http://kr.archive.ubuntu.com/ubuntu bionic-updates/main DEP-11 64x64 Icons [140 kB]                                         
받기:14 http://security.ubuntu.com/ubuntu bionic-security/main i386 Packages [436 kB]                                               
받기:15 http://kr.archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [30.0 kB]                                      
받기:16 http://kr.archive.ubuntu.com/ubuntu bionic-updates/restricted Translation-en [7,888 B]                                      
받기:17 http://kr.archive.ubuntu.com/ubuntu bionic-updates/universe i386 Packages [1,007 kB]                                        
받기:18 http://kr.archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [1,048 kB]                                       
받기:19 http://security.ubuntu.com/ubuntu bionic-security/main Translation-en [207 kB]                                      
받기:20 http://security.ubuntu.com/ubuntu bionic-security/main amd64 DEP-11 Metadata [38.5 kB]                                      
받기:21 http://security.ubuntu.com/ubuntu bionic-security/main DEP-11 48x48 Icons [17.6 kB]                                         
받기:22 http://security.ubuntu.com/ubuntu bionic-security/main DEP-11 64x64 Icons [41.5 kB]                                         
받기:23 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [643 kB]                                          
받기:24 http://kr.archive.ubuntu.com/ubuntu bionic-updates/universe Translation-en [324 kB]                                        
받기:25 http://apt.postgresql.org/pub/repos/apt bionic-pgdg/main i386 Packages [177 kB]                                             
받기:26 http://security.ubuntu.com/ubuntu bionic-security/universe i386 Packages [615 kB]                                           
받기:27 http://security.ubuntu.com/ubuntu bionic-security/universe Translation-en [216 kB]                                        
받기:28 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 DEP-11 Metadata [42.1 kB]                                  
받기:29 http://security.ubuntu.com/ubuntu bionic-security/universe DEP-11 48x48 Icons [16.4 kB]                                     
받기:30 http://security.ubuntu.com/ubuntu bionic-security/universe DEP-11 64x64 Icons [111 kB]                                      
받기:31 http://kr.archive.ubuntu.com/ubuntu bionic-updates/universe amd64 DEP-11 Metadata [264 kB]                                  
받기:32 http://kr.archive.ubuntu.com/ubuntu bionic-updates/universe DEP-11 48x48 Icons [197 kB]                                     
받기:33 http://kr.archive.ubuntu.com/ubuntu bionic-updates/universe DEP-11 64x64 Icons [445 kB]                                    
받기:34 http://kr.archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 DEP-11 Metadata [2,464 B]                             
받기:35 http://kr.archive.ubuntu.com/ubuntu bionic-backports/universe i386 Packages [4,028 B]  
받기:36 http://kr.archive.ubuntu.com/ubuntu bionic-backports/universe amd64 Packages [4,028 B]                                    
받기:37 http://kr.archive.ubuntu.com/ubuntu bionic-backports/universe amd64 DEP-11 Metadata [8,280 B]                             
받기:38 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 DEP-11 Metadata [2,464 B]                                
내려받기 9,312 k바이트, 소요시간 7초 (1,400 k바이트/초)                                                                             
패키지 목록을 읽는 중입니다... 완료

Repository를 Update하면 위에 표시된 줄과 같이 PostgreSQL 관련 Repositoty가 추가됐음을 확인할 수 있다.

2. PostgreSQL 설치

Repository Update가 끝나면 위에서 실패했던 PostgreSQL 설치를 진행할 수 있다. PostgreSQL 10부터는 postgresql-contrib에 포함된 Extended Module(ex. pgBench)들이 기본 Package에 포함되기 때문에 서버만 설치하면 된다. 

root@ubuntu-server:/etc/apt# apt-get install postgresql-11
패키지 목록을 읽는 중입니다... 완료
의존성 트리를 만드는 중입니다       
상태 정보를 읽는 중입니다... 완료
The following additional packages will be installed:
  libpq5 pgdg-keyring postgresql-client-11 postgresql-client-common postgresql-common sysstat
제안하는 패키지:
  postgresql-doc-11 libjson-perl isag

...

Creating new PostgreSQL cluster 11/main ...
/usr/lib/postgresql/11/bin/initdb -D /var/lib/postgresql/11/main --auth-local peer --auth-host md5
이 데이터베이스 시스템에서 만들어지는 파일들은 그 소유주가 "postgres" id로
지정될 것입니다. 또한 이 사용자는 서버 프로세스의 소유주가 됩니다.

...

작업완료. 이제 다음 명령을 이용해서 서버를 가동 할 수 있습니다:

    pg_ctlcluster 11 main start

Ver Cluster Port Status Owner    Data directory              Log file
11  main    5432 down   postgres /var/lib/postgresql/11/main /var/log/postgresql/postgresql-11-main.log
update-alternatives: using /usr/share/postgresql/11/man/man1/postmaster.1.gz to provide /usr/share/man/man1/postmaster.1.gz (postmaster.1.gz) in auto mode
Processing triggers for ureadahead (0.100.0-21) ...
Processing triggers for systemd (237-3ubuntu10.24) ...
...

설치가 끝나면 마지막 부분에 'pg_ctlcluster 11 main start' Command로 서버를 가동하라는 Message가 있는데 직접 사용했을 때는 설치가 끝나면 이미 가동되어 있고, 별도로 initdb를 실행할 필요 없이 설치 과정 중에 이루어지고 있다. 설치 제일 마지막 단계에서 현재 Data Directory와 Log File이 어디 위치하고 있는 지도 알려준다.

3. 설치 확인

PostgreSQL 실행 파일들, 설치 Package, 기본 User 'postgres' 등을 통해 설치 결과를 확인할 수 있다.

# PostgreSQL 각종 실행 파일들 확인
root@ubuntu-server:/etc/apt# ls -la /usr/lib/postgresql/11/bin/
total 10584
drwxr-xr-x 2 root root    4096  2월 10 10:37 .
drwxr-xr-x 4 root root    4096  2월 10 10:37 ..
-rwxr-xr-x 1 root root   68096 11월 12 19:41 clusterdb
-rwxr-xr-x 1 root root   68160 11월 12 19:41 createdb
-rwxr-xr-x 1 root root   72608 11월 12 19:41 createuser
-rwxr-xr-x 1 root root   63904 11월 12 19:41 dropdb
-rwxr-xr-x 1 root root   63872 11월 12 19:41 dropuser
-rwxr-xr-x 1 root root  146624 11월 12 19:41 initdb
-rwxr-xr-x 1 root root   26616 11월 12 19:41 oid2name
-rwxr-xr-x 1 root root   26696 11월 12 19:41 pg_archivecleanup
-rwxr-xr-x 1 root root  121920 11월 12 19:41 pg_basebackup
-rwxr-xr-x 1 root root   39072 11월 12 19:41 pg_controldata
-rwxr-xr-x 1 root root   51696 11월 12 19:41 pg_ctl
-rwxr-xr-x 1 root root  401280 11월 12 19:41 pg_dump
-rwxr-xr-x 1 root root   97704 11월 12 19:41 pg_dumpall
-rwxr-xr-x 1 root root   63808 11월 12 19:41 pg_isready
-rwxr-xr-x 1 root root   72456 11월 12 19:41 pg_receivewal
-rwxr-xr-x 1 root root   72560 11월 12 19:41 pg_recvlogical
-rwxr-xr-x 1 root root   51696 11월 12 19:41 pg_resetwal
-rwxr-xr-x 1 root root  171456 11월 12 19:41 pg_restore
-rwxr-xr-x 1 root root   84384 11월 12 19:41 pg_rewind
-rwxr-xr-x 1 root root   26632 11월 12 19:41 pg_standby
-rwxr-xr-x 1 root root   30912 11월 12 19:41 pg_test_fsync
-rwxr-xr-x 1 root root   26776 11월 12 19:41 pg_test_timing
-rwxr-xr-x 1 root root  137840 11월 12 19:41 pg_upgrade
-rwxr-xr-x 1 root root   39104 11월 12 19:41 pg_verify_checksums
-rwxr-xr-x 1 root root   84480 11월 12 19:41 pg_waldump
-rwxr-xr-x 1 root root  146744 11월 12 19:41 pgbench
-rwxr-xr-x 1 root root 7694704 11월 12 19:41 postgres
lrwxrwxrwx 1 root root       8 11월 12 19:41 postmaster -> postgres
-rwxr-xr-x 1 root root  658688 11월 12 19:41 psql
-rwxr-xr-x 1 root root   72288 11월 12 19:41 reindexdb
-rwxr-xr-x 1 root root   76480 11월 12 19:41 vacuumdb
-rwxr-xr-x 1 root root   26616 11월 12 19:41 vacuumlo

...

# PostgreSQL 관련 Package 확인
root@ubuntu-server:/etc/apt# dpkg -l | grep postgres
ii  pgdg-keyring                               2018.2                                       all          keyring for apt.postgresql.org
ii  postgresql-11                              11.6-1.pgdg18.04+1                           amd64        object-relational SQL database, version 11 server
ii  postgresql-client-11                       11.6-1.pgdg18.04+1                           amd64        front-end programs for PostgreSQL 11
ii  postgresql-client-common                   210.pgdg18.04+1                              all          manager for multiple PostgreSQL client versions
ii  postgresql-common                          210.pgdg18.04+1                              all          PostgreSQL database-cluster manager

...

# PostgreSQL 기본 User 생성 확인
root@ubuntu-server:/etc/apt# cat /etc/passwd | grep postgres
postgres:x:124:129:PostgreSQL administrator,,,:/var/lib/postgresql:/bin/bash
...

4. psql 실행 및 Test

간단한 동작 확인을 위해 psql을 통해 Database Instance를 하나 생성해 보았다.

# psql을 통한 postgreSQL 서버 접속
root@ubuntu-server:/var/lib/postgresql# sudo -u postgres psql
psql (11.6 (Ubuntu 11.6-1.pgdg18.04+1))
도움말을 보려면 "help"를 입력하십시오.

# 초기 DB Instance 확인
postgres=# \l
                                 데이터베이스 목록
   이름    |  소유주  | 인코딩 |   Collate   |    Ctype    |      액세스 권한      
-----------+----------+--------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8   | ko_KR.UTF-8 | ko_KR.UTF-8 | 
 template0 | postgres | UTF8   | ko_KR.UTF-8 | ko_KR.UTF-8 | =c/postgres          +
           |          |        |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8   | ko_KR.UTF-8 | ko_KR.UTF-8 | =c/postgres          +
           |          |        |             |             | postgres=CTc/postgres
(3개 행)

# User 'browndwarf' 생성
postgres=# CREATE USER browndwarf PASSWORD 'browndwarf' SUPERUSER;
CREATE ROLE
postgres=# SELECT * FROM PG_USER;
 usename   | usesysid | usecreatedb | usesuper | userepl | usebypassrls |  passwd  | valuntil | useconfig 
-----------+----------+-------------+----------+---------+--------------+----------+----------+-----------
 postgres  |       10 | t           | t        | t       | t            | ******** |          | 
 browndwarf|    16384 | f           | t        | f       | f            | ******** |          | 
(2개 행)

# DB Instance 'galaxy' 생성
postgres=# CREATE DATABASE "galaxy" OWNER browndwarf;
CREATE DATABASE

postgres=# \l
                                   데이터베이스 목록
    이름    |   소유주   | 인코딩 |   Collate   |    Ctype    |      액세스 권한      
------------+------------+--------+-------------+-------------+-----------------------
 galaxy     | browndwarf | UTF8   | ko_KR.UTF-8 | ko_KR.UTF-8 | 
 postgres   | postgres   | UTF8   | ko_KR.UTF-8 | ko_KR.UTF-8 | 
 template0  | postgres   | UTF8   | ko_KR.UTF-8 | ko_KR.UTF-8 | =c/postgres          +
            |            |        |             |             | postgres=CTc/postgres
 template1  | postgres   | UTF8   | ko_KR.UTF-8 | ko_KR.UTF-8 | =c/postgres          +
            |            |        |             |             | postgres=CTc/postgres
(4개 행)

# psql 종료
postgres=# \q

# User 'browndwarf'로 'galaxy' DB에 접속
root@ubuntu-server:/var/lib/postgresql# psql -h localhost -p 5432 -U browndwarf -d "galaxy"
browndwarf 사용자의 암호: 
psql (11.6 (Ubuntu 11.6-1.pgdg18.04+1))
SSL 연결정보 (프로토콜: TLSv1.3, 암호화기법: TLS_AES_256_GCM_SHA384, 비트: 256, 압축: off)
도움말을 보려면 "help"를 입력하십시오.

galaxy=# 

 

Remark : 환경 파일 위치

이번 설치 과정은 Demo를 위해 localhost에서만 사용하면 되는 상황이라 별다른 환경 설정이 필요 없었지만 혹시 pg_hba.conf나 postgresql.conf를 수정할 경우 아래 Directory에서 찾아서 수정하면 된다.

browndwarf@ubuntu-server:~$ ls -la /etc/postgresql/11/main/
total 60
drwxr-xr-x 3 postgres postgres  4096  2월 10 10:37 .
drwxr-xr-x 3 postgres postgres  4096  2월 10 10:37 ..
drwxr-xr-x 2 postgres postgres  4096  2월 10 10:37 conf.d
-rw-r--r-- 1 postgres postgres   315  2월 10 10:37 environment
-rw-r--r-- 1 postgres postgres   143  2월 10 10:37 pg_ctl.conf
-rw-r----- 1 postgres postgres  4686  2월 10 10:37 pg_hba.conf
-rw-r----- 1 postgres postgres  1636  2월 10 10:37 pg_ident.conf
-rw-r--r-- 1 postgres postgres 24181  2월 10 10:37 postgresql.conf
-rw-r--r-- 1 postgres postgres   317  2월 10 10:37 start.conf

 

참조

https://www.postgresql.org/download/linux/ubuntu/