DEV Community

nabbisen
nabbisen

Posted on • Originally published at obsd.solutions

1

PostgreSQL 14.5 on OpenBSD 7.2: インストール

はじめに

OpenBSD において 7.2 のリリースに伴って PostgreSQL14.5 にアップデートされました。

最新の OpenBSD に PostgreSQL をインストールする流れをご紹介します。

環境

  • OS: OpenBSD 7.2
  • データベース: PostgreSQL 14.5

概観

各ステップの詳細はこの後で記載します。

$ doas pkg_add postgresql-server

$ doas su - _postgresql

$ # 下記 --locale の指定は必要に応じて行います
$ # スーパーユーザーすなわち下記 "postgres" のパスワードを聞かれます
$ initdb -D /var/postgresql/data -U postgres \
      -W -A scram-sha-256 -E UTF-8 --locale=xx_XX.UTF-8

$ exit

$ doas rcctl enable postgresql
$ doas rcctl start postgresql
Enter fullscreen mode Exit fullscreen mode

チュートリアル

手順ごとの説明を以下に記します。

1. PostgreSQL サーバーのインストール

Ports システムからパッケージを取得してインストールします:

$ doas pkg_add postgresql-server
Enter fullscreen mode Exit fullscreen mode

出力は以下の通りでした:

quirks-6.42 signed on 2023-01-13T18:22:41Z
postgresql-server-14.5:postgresql-client-14.5: ok
useradd: Warning: home directory `/var/postgresql' doesn't exist, and -m was not specified
postgresql-server-14.5: ok
The following new rcscripts were installed: /etc/rc.d/postgresql
See rcctl(8) for details.
New and changed readme(s):
    /usr/local/share/doc/pkg-readmes/postgresql-server
Enter fullscreen mode Exit fullscreen mode

2. データベースの初期環境構築

アクセス権限によるエラーを起こさないように、_postgresql ユーザーにスイッチします。こちらのユーザーは、上の手順で作成されています:

$ doas su - _postgresql
Enter fullscreen mode Exit fullscreen mode

init_db を実行します。データベース・クラスターが作成されます。
この時 /var/postgresql ディレクトリが自動でつくられます:

$ initdb -D /var/postgresql/data -U postgres \
      -W -A scram-sha-256 -E UTF-8 --locale=xx_XX.UTF-8
Enter fullscreen mode Exit fullscreen mode

上の -U postgres (--user=... と同じ) にはスーパーユーザーの名前をセットします。

出力は以下のように始まりました:

The files belonging to this database system will be owned by user "_postgresql".
This user must also own the server process.

The database cluster will be initialized with locale "ja_JP.UTF-8".
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.
Enter fullscreen mode Exit fullscreen mode

ここで -W (--pwprompt と同じ) を指定した場合、パスワードをたずねられます。
-W-A scram-sha-256 (--auth=... と同じ) を指定するのは、セキュリティ保護のためです。

ご参考までに readme (/usr/local/share/doc/pkg-readmes/postgresql-server) の内容を引用します:

It is strongly advised that you do not work with the postgres dba account
other than creating more users and/or databases or for administrative tasks.
Use the PostgreSQL permission system to make sure that a database is only
accessed by programs/users that have the right to do so.

それから --locale=... ですが、こちらは必要に応じて指定します。en_US.UTF-8 で良い場合は、指定不要です。

さて、パスワードを入力しましょう:

Enter new superuser password: 
Enter it again: 
Enter fullscreen mode Exit fullscreen mode

以下が出力されました:

creating directory /var/postgresql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 20
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

Success. You can now start the database server using:

    pg_ctl -D /var/postgresql/data -l logfile start

Enter fullscreen mode Exit fullscreen mode

やりました。成功です。exit を実行して _postgersql ユーザーセッションを終了しましょう:

$ exit
Enter fullscreen mode Exit fullscreen mode

3. PostgreSQL サーバーの起動

デーモンを有効化しましょう。そして起動します:

$ doas rcctl enable postgresql
postgresql(ok)

$ doas rcctl start postgresql
postgresql(ok)
Enter fullscreen mode Exit fullscreen mode

4. サーバーを使う

Postgresql サーバーのデーモンが有効になり、起動されました。RDBMS として使うことができるようになっています。クライアントからのリクエストを処理してくれます。

設定ファイルには postgresql.confpg_hba.conf などがありますが、これらは自動で生成されています。また psql コマンドもインストールされています。

設定ファイル

サーバー周りの設定 (英語) を行う時に使います。

psql コマンド

ターミナルから利用できる PostgreSQL 操作用のフロントエンドツールです。先ほどたずねられたパスワードを入力することで、サーバーに接続できます:

$ psql -U postgres
Password for user postgres:
Enter fullscreen mode Exit fullscreen mode

迎え入れられるでしょう :)

psql (14.5)
Type "help" for help.

postgres=#
Enter fullscreen mode Exit fullscreen mode

参考

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay