1.2 PostgreSQLのインストールと初期設定(Ubuntuの場合)

以下を確認します。なお、OS管理者権限(sudoコマンド)を行使できるubuntuユーザでログインして実行します。

  • postgresユーザを作成する
  • postgresqlをインストールし、サーバを起動する

postgresユーザの作成

login as: ubuntu
ubuntu@localhost's password:
Welcome to Ubuntu 24.04 LTS (GNU/Linux 6.8.0-40-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/pro

 System information as of 2024年  8月  8日 木曜日 21:46:23 JST

  System load:  0.8                Processes:               101
  Usage of /:   41.1% of 11.21GB   Users logged in:         0
  Memory usage: 8%                 IPv4 address for enp0s3: 10.0.2.15
  Swap usage:   0%


Expanded Security Maintenance for Applications is not enabled.

0のアップデートはすぐに適用されます。

Enable ESM Apps to receive additional future security updates.
See https://ubuntu.com/esm or run: sudo pro status


Last login: Thu Aug  8 21:30:06 2024 from 10.0.2.2
ubuntu@host1:~$ sudo adduser postgres
[sudo] ubuntu のパスワード: ※ubuntuユーザのパスワードを入力
info: ユーザ `postgres' を追加しています...
info: Selecting UID/GID from range 1000 to 59999 ...
info: 新しいグループ `postgres' (1001) を追加しています...
info: Adding new user `postgres' (1001) with group `postgres (1001)' ...
info: ホームディレクトリ `/home/postgres' を作成しています...
info: `/etc/skel' からファイルをコピーしています...
新しいパスワード: ※postgresユーザに設定するパスワードを入力
新しいパスワードを再入力してください: ※postgresユーザのパスワードを再入力
passwd: パスワードは正しく更新されました
postgres のユーザ情報を変更中
新しい値を入力してください。標準設定値を使うならリターンを押してください
        フルネーム []:
        部屋番号 []:
        職場電話番号 []:
        自宅電話番号 []:
        その他 []:
以上で正しいですか? [Y/n] y
info: Adding new user `postgres' to supplemental / extra groups `users' ...
info: ユーザ `postgres' をグループ `users' に追加しています...
ubuntu@host1:~$ sudo -iu postgres ※次に表示されるプロンプトがpostgres@に変わることを確認
postgres@host1:~$ pwd ※カレントディレクトリが/home/postgresであることを確認
/home/postgres
postgres@host1:~$ exit ※postgresユーザからログアウトすることを確認
ログアウト
ubuntu@host1:~$

postgresユーザの情報は、OSの標準設定値で問題ありません。

postgresqlのインストールとサーバの起動

ubuntu@host1:~$ sudo apt install postgresql
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています... 完了
状態情報を読み取っています... 完了
以下の追加パッケージがインストールされます:
  libcommon-sense-perl libjson-perl libjson-xs-perl libllvm17t64 libpq5
  libtypes-serialiser-perl postgresql-16 postgresql-client-16
  postgresql-client-common postgresql-common ssl-cert
提案パッケージ:
  postgresql-doc postgresql-doc-16
以下のパッケージが新たにインストールされます:
  libcommon-sense-perl libjson-perl libjson-xs-perl libllvm17t64 libpq5
  libtypes-serialiser-perl postgresql postgresql-16 postgresql-client-16
  postgresql-client-common postgresql-common ssl-cert
アップグレード: 0 個、新規インストール: 12 個、削除: 0 個、保留: 0 個。
43.5 MB のアーカイブを取得する必要があります。
この操作後に追加で 175 MB のディスク容量が消費されます。
続行しますか? [Y/n] y ※インストールされるパッケージを確認して y を入力
取得:1 http://jp.archive.ubuntu.com/ubuntu noble/main amd64 libjson-perl all 4.10000-1 [81.9 kB]
取得:2 http://jp.archive.ubuntu.com/ubuntu noble/main amd64 postgresql-client-common all 257build1 [36.3 kB]
取得:3 http://jp.archive.ubuntu.com/ubuntu noble/main amd64 ssl-cert all 1.1.2ubuntu1 [17.8 kB]
取得:4 http://jp.archive.ubuntu.com/ubuntu noble/main amd64 postgresql-common all 257build1 [161 kB]
取得:5 http://jp.archive.ubuntu.com/ubuntu noble/main amd64 libcommon-sense-perl amd64 3.75-3build3 [20.4 kB]
取得:6 http://jp.archive.ubuntu.com/ubuntu noble/main amd64 libtypes-serialiser-perl all 1.01-1 [11.6 kB]
取得:7 http://jp.archive.ubuntu.com/ubuntu noble/main amd64 libjson-xs-perl amd64 4.030-2build3 [83.6 kB]
取得:8 http://jp.archive.ubuntu.com/ubuntu noble/main amd64 libllvm17t64 amd64 1:17.0.6-9ubuntu1 [26.2 MB]
取得:9 http://jp.archive.ubuntu.com/ubuntu noble-updates/main amd64 libpq5 amd64 16.3-0ubuntu0.24.04.1 [141 kB]
取得:10 http://jp.archive.ubuntu.com/ubuntu noble-updates/main amd64 postgresql-client-16 amd64 16.3-0ubuntu0.24.04.1 [1,269 kB]
取得:11 http://jp.archive.ubuntu.com/ubuntu noble-updates/main amd64 postgresql-16 amd64 16.3-0ubuntu0.24.04.1 [15.5 MB]
取得:12 http://jp.archive.ubuntu.com/ubuntu noble/main amd64 postgresql all 16+257build1 [11.5 kB]
43.5 MB を 6秒 で取得しました (7,224 kB/s)
パッケージを事前設定しています ...
以前に未選択のパッケージ libjson-perl を選択しています。
(データベースを読み込んでいます ... 現在 121579 個のファイルとディレクトリがインストールされています。)
.../00-libjson-perl_4.10000-1_all.deb を展開する準備をしています ...
libjson-perl (4.10000-1) を展開しています...
以前に未選択のパッケージ postgresql-client-common を選択しています。
.../01-postgresql-client-common_257build1_all.deb を展開する準備をしています ...
postgresql-client-common (257build1) を展開しています...
以前に未選択のパッケージ ssl-cert を選択しています。
.../02-ssl-cert_1.1.2ubuntu1_all.deb を展開する準備をしています ...
ssl-cert (1.1.2ubuntu1) を展開しています...
以前に未選択のパッケージ postgresql-common を選択しています。
.../03-postgresql-common_257build1_all.deb を展開する準備をしています ...
'postgresql-common による /usr/bin/pg_config から /usr/bin/pg_config.libpq-dev への退避 (divert)' を追加しています
postgresql-common (257build1) を展開しています...
以前に未選択のパッケージ libcommon-sense-perl:amd64 を選択しています。
.../04-libcommon-sense-perl_3.75-3build3_amd64.deb を展開する準備をしています ...
libcommon-sense-perl:amd64 (3.75-3build3) を展開しています...
以前に未選択のパッケージ libtypes-serialiser-perl を選択しています。
.../05-libtypes-serialiser-perl_1.01-1_all.deb を展開する準備をしています ...
libtypes-serialiser-perl (1.01-1) を展開しています...
以前に未選択のパッケージ libjson-xs-perl を選択しています。
.../06-libjson-xs-perl_4.030-2build3_amd64.deb を展開する準備をしています ...
libjson-xs-perl (4.030-2build3) を展開しています...
以前に未選択のパッケージ libllvm17t64:amd64 を選択しています。
.../07-libllvm17t64_1%3a17.0.6-9ubuntu1_amd64.deb を展開する準備をしています ...
libllvm17t64:amd64 (1:17.0.6-9ubuntu1) を展開しています...
以前に未選択のパッケージ libpq5:amd64 を選択しています。
.../08-libpq5_16.3-0ubuntu0.24.04.1_amd64.deb を展開する準備をしています ...
libpq5:amd64 (16.3-0ubuntu0.24.04.1) を展開しています...
以前に未選択のパッケージ postgresql-client-16 を選択しています。
.../09-postgresql-client-16_16.3-0ubuntu0.24.04.1_amd64.deb を展開する準備をしています ...
postgresql-client-16 (16.3-0ubuntu0.24.04.1) を展開しています...
以前に未選択のパッケージ postgresql-16 を選択しています。
.../10-postgresql-16_16.3-0ubuntu0.24.04.1_amd64.deb を展開する準備をしています ...
postgresql-16 (16.3-0ubuntu0.24.04.1) を展開しています...
以前に未選択のパッケージ postgresql を選択しています。
.../11-postgresql_16+257build1_all.deb を展開する準備をしています ...
postgresql (16+257build1) を展開しています...
postgresql-client-common (257build1) を設定しています ...
libpq5:amd64 (16.3-0ubuntu0.24.04.1) を設定しています ...
libcommon-sense-perl:amd64 (3.75-3build3) を設定しています ...
libllvm17t64:amd64 (1:17.0.6-9ubuntu1) を設定しています ...
ssl-cert (1.1.2ubuntu1) を設定しています ...
Created symlink /etc/systemd/system/multi-user.target.wants/ssl-cert.service → /usr/lib/systemd/system/ssl-cert.service.
libtypes-serialiser-perl (1.01-1) を設定しています ...
libjson-perl (4.10000-1) を設定しています ...
libjson-xs-perl (4.030-2build3) を設定しています ...
postgresql-client-16 (16.3-0ubuntu0.24.04.1) を設定しています ...
update-alternatives: /usr/share/man/man1/psql.1.gz (psql.1.gz) を提供するために自動モードで /usr/share/postgresql/16/man/man1/psql.1.gz を使います
postgresql-common (257build1) を設定しています ...

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 → /usr/lib/systemd/system/postgresql.service.
postgresql-16 (16.3-0ubuntu0.24.04.1) を設定しています ...
Creating new PostgreSQL cluster 16/main ...
/usr/lib/postgresql/16/bin/initdb -D /var/lib/postgresql/16/main --auth-local peer --auth-host scram-sha-256 --no-instructions
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 "ja_JP.UTF-8".
The default database encoding has accordingly been set to "UTF8".
initdb: could not find suitable text search configuration for locale "ja_JP.UTF-8"
The default text search configuration will be set to "simple".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/16/main ... ok ※データベースクラスタの場所
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Tokyo
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
postgresql (16+257build1) を設定しています ...
man-db (2.12.0-4build2) のトリガを処理しています ...
libc-bin (2.39-0ubuntu8.2) のトリガを処理しています ...
Scanning processes...
Scanning linux images...

Running kernel seems to be up-to-date.

No services need to be restarted.

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host.
ubuntu@host1:~$ sudo ls /var/lib/postgresql/16/main ※データベースクラスタの内容を確認
PG_VERSION    pg_logical    pg_snapshots  pg_twophase           postmaster.pid
base          pg_multixact  pg_stat       pg_wal
global        pg_notify     pg_stat_tmp   pg_xact
pg_commit_ts  pg_replslot   pg_subtrans   postgresql.auto.conf
pg_dynshmem   pg_serial     pg_tblspc     postmaster.opts
ubuntu@host1:~$ sudo systemctl status postgresql ※データベースサーバの状態を確認
● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; prese>
     Active: active (exited) since Thu 2024-08-08 21:53:49 JST; 1min 7s ago
   Main PID: 2378 (code=exited, status=0/SUCCESS)
        CPU: 1ms

 8月 08 21:53:49 host1 systemd[1]: Starting postgresql.service - PostgreSQL RDB>
 8月 08 21:53:49 host1 systemd[1]: Finished postgresql.service - PostgreSQL RDB>
ubuntu@host1:~$

これ以降、原則としてpostgresqlサーバは常時起動させておいてください。また、何らかの理由により起動中のpostgresqlサーバを停止させたい場合は、ubuntuユーザで sudo systemctl stop postgresql を入力してください。