PostgreSQL のインストール (Ubuntu)

はじめに

この手順では Ubuntu 18.04 に PostgreSQL をインストールする手順を説明します。

インストール対象の Alfresco Content Services でサポートされているバージョンのものを選択してください。

本手順に記載のバージョンは、サポート対象バージョンにあわせて適宜読み替えてください。


1.リポジトリ情報の登録

リポジトリファイルに追記します。

実行コマンド
$ sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

リポジトリ用のキーを登録します。

実行コマンド
$ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
期待する結果
O K

パッケージを更新します。

実行コマンド
$ sudo apt-get update
期待する結果
Get:1 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]    
Hit:2 http://archive.ubuntu.com/ubuntu bionic InRelease                        
Get:3 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]      
Get:4 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]    
Get:5 http://apt.postgresql.org/pub/repos/apt bionic-pgdg InRelease [104 kB]   
Get:6 http://apt.postgresql.org/pub/repos/apt bionic-pgdg/main amd64 Packages [231 kB]
Fetched 587 kB in 4s (132 kB/s)    
Reading package lists... Done

2.PostgreSQL のインストール

次のコマンドで PostgreSQL をインストールします。

実行コマンド
$ sudo apt-get install postgresql-11
期待する結果
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libllvm6.0 libpq5 libsensors4 pgdg-keyring postgresql-client-11
  postgresql-client-common postgresql-common ssl-cert sysstat
Suggested packages:
  lm-sensors postgresql-doc-11 libjson-perl openssl-blacklist isag
The following NEW packages will be installed:
  libllvm6.0 libpq5 libsensors4 pgdg-keyring postgresql-11
  postgresql-client-11 postgresql-client-common postgresql-common ssl-cert
  sysstat
0 upgraded, 10 newly installed, 0 to remove and 0 not upgraded.
Need to get 31.0 MB of archives.
After this operation, 116 MB of additional disk space will be used.
Do you want to continue? [Y/n] 
Get:1 http://apt.postgresql.org/pub/repos/apt bionic-pgdg/main amd64 libpq5 amd64 13.2-1.pgdg18.04+1 [177 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic/main amd64 libllvm6.0 amd64 1:6.0-1ubuntu2 [14.5 MB]
Get:3 http://apt.postgresql.org/pub/repos/apt bionic-pgdg/main amd64 pgdg-keyring all 2018.2 [10.7 kB]
Get:4 http://apt.postgresql.org/pub/repos/apt bionic-pgdg/main amd64 postgresql-client-common all 225.pgdg18.04+1 [89.4 kB]
Get:5 http://apt.postgresql.org/pub/repos/apt bionic-pgdg/main amd64 postgresql-client-11 amd64 11.11-1.pgdg18.04+1 [1483 kB]
Get:6 http://archive.ubuntu.com/ubuntu bionic/main amd64 libsensors4 amd64 1:3.4.0-4 [28.8 kB]
Get:7 http://archive.ubuntu.com/ubuntu bionic/main amd64 ssl-cert all 1.0.39 [17.0 kB]
Get:8 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 sysstat amd64 11.6.1-1ubuntu0.1 [295 kB]
Get:9 http://apt.postgresql.org/pub/repos/apt bionic-pgdg/main amd64 postgresql-common all 225.pgdg18.04+1 [238 kB]
Get:10 http://apt.postgresql.org/pub/repos/apt bionic-pgdg/main amd64 postgresql-11 amd64 11.11-1.pgdg18.04+1 [14.1 MB]
Fetched 31.0 MB in 29s (1059 kB/s)                                             
Preconfiguring packages ...
Selecting previously unselected package libllvm6.0:amd64.
(Reading database ... 60169 files and directories currently installed.)
Preparing to unpack .../0-libllvm6.0_1%3a6.0-1ubuntu2_amd64.deb ...
Unpacking libllvm6.0:amd64 (1:6.0-1ubuntu2) ...
Selecting previously unselected package libpq5:amd64.
Preparing to unpack .../1-libpq5_13.2-1.pgdg18.04+1_amd64.deb ...
Unpacking libpq5:amd64 (13.2-1.pgdg18.04+1) ...
Selecting previously unselected package libsensors4:amd64.
Preparing to unpack .../2-libsensors4_1%3a3.4.0-4_amd64.deb ...
Unpacking libsensors4:amd64 (1:3.4.0-4) ...
Selecting previously unselected package pgdg-keyring.
Preparing to unpack .../3-pgdg-keyring_2018.2_all.deb ...
Unpacking pgdg-keyring (2018.2) ...
Selecting previously unselected package postgresql-client-common.
Preparing to unpack .../4-postgresql-client-common_225.pgdg18.04+1_all.deb ...
Unpacking postgresql-client-common (225.pgdg18.04+1) ...
Selecting previously unselected package postgresql-client-11.
Preparing to unpack .../5-postgresql-client-11_11.11-1.pgdg18.04+1_amd64.deb ...
Unpacking postgresql-client-11 (11.11-1.pgdg18.04+1) ...
Selecting previously unselected package ssl-cert.
Preparing to unpack .../6-ssl-cert_1.0.39_all.deb ...
Unpacking ssl-cert (1.0.39) ...
Selecting previously unselected package postgresql-common.
Preparing to unpack .../7-postgresql-common_225.pgdg18.04+1_all.deb ...
Adding 'diversion of /usr/bin/pg_config to /usr/bin/pg_config.libpq-dev by postgresql-common'
Unpacking postgresql-common (225.pgdg18.04+1) ...
Selecting previously unselected package postgresql-11.
Preparing to unpack .../8-postgresql-11_11.11-1.pgdg18.04+1_amd64.deb ...
Unpacking postgresql-11 (11.11-1.pgdg18.04+1) ...
Selecting previously unselected package sysstat.
Preparing to unpack .../9-sysstat_11.6.1-1ubuntu0.1_amd64.deb ...
Unpacking sysstat (11.6.1-1ubuntu0.1) ...
Setting up libllvm6.0:amd64 (1:6.0-1ubuntu2) ...
Setting up ssl-cert (1.0.39) ...
Setting up libpq5:amd64 (13.2-1.pgdg18.04+1) ...
Setting up libsensors4:amd64 (1:3.4.0-4) ...
Setting up pgdg-keyring (2018.2) ...
Removing apt.postgresql.org key from trusted.gpg: OK
Setting up sysstat (11.6.1-1ubuntu0.1) ...

Creating config file /etc/default/sysstat with new version
update-alternatives: using /usr/bin/sar.sysstat to provide /usr/bin/sar (sar) in auto mode
Created symlink /etc/systemd/system/multi-user.target.wants/sysstat.service → /lib/systemd/system/sysstat.service.
Setting up postgresql-client-common (225.pgdg18.04+1) ...
Setting up postgresql-common (225.pgdg18.04+1) ...
Adding user postgres to group ssl-cert

Creating config file /etc/postgresql-common/createcluster.conf with new version
Building PostgreSQL dictionaries from installed myspell/hunspell packages...
Removing obsolete dictionary files:
Created symlink /etc/systemd/system/multi-user.target.wants/postgresql.service → /lib/systemd/system/postgresql.service.
Setting up postgresql-client-11 (11.11-1.pgdg18.04+1) ...
update-alternatives: using /usr/share/postgresql/11/man/man1/psql.1.gz to provide /usr/share/man/man1/psql.1.gz (psql.1.gz) in auto mode
Setting up postgresql-11 (11.11-1.pgdg18.04+1) ...
Creating new PostgreSQL cluster 11/main ...
/usr/lib/postgresql/11/bin/initdb -D /var/lib/postgresql/11/main --auth-local peer --auth-host md5
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "C.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/11/main ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default timezone ... Asia/Tokyo
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

Success. You can now start the database server using:

    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 libc-bin (2.27-3ubuntu1.4) ...
Processing triggers for systemd (237-3ubuntu10.44) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for ureadahead (0.100.0-21) ...

3.PostgreSQL の設定

postgresql.conf ファイルを修正します。ここで設定する値は要件にあわせて適宜変更してください。
(参考:PostgreSQL 公式ドキュメント - 第19章 サーバの設定)

実行コマンド
$ sudo vi /etc/postgresql/11/main/postgresql.conf
編集内容
(省略)

# - Connection Settings -
 
listen_addresses = 'localhost' ←DBへの接続を許可するIPアドレスを設定             # what IP address(es) to listen on;
                                                             # comma-separated list of addresses;
                                                             # defaults to 'localhost'; use '*' for all
                                                             # (change requires restart)

(中略)

# - Memory -
(中略)

max_connections = 275                   # (change requires restart)

(中略)

shared_buffers = 512MB                  # min 128kB
 
(中略)
 
work_mem = 4MB                          # min 64kB

(中略)

wal_buffers = -1                        # min 32kB, -1 sets based on shared_buffers

(中略)

pg_hba.conf ファイルを修正します。
(参考:PostgreSQL 公式ドキュメント - 第20章 クライアント認証PostgreSQL 公式ドキュメント - 20.1. pg_hba.confファイル)

実行コマンド
$ sudo vi /etc/postgresql/11/main/pg_hba.conf
編集内容
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5 ←"md5"に変更

PostgreSQL を起動します。

実行コマンド
$ sudo systemctl start postgresql.service \
&& systemctl is-active postgresql.service && systemctl status postgresql.service

PostgreSQL の自動起動設定をします。

実行コマンド
$ sudo systemctl enable postgresql.service \
&& systemctl is-enabled postgresql.service && systemctl status postgresql.service

リックソフト株式会社 は、日本でトップレベルのAtlassian Platinum Solution Partnerです。
大規模ユーザーへの対応実績が認められたEnterpriseの認定をうけ、高度なトレーニング要件をクリアし、小規模から大規模のお客様まで対応可能な実績を示したパートナー企業です。


Copyright © Ricksoft Co., Ltd. プライバシーポリシー お問い合わせ