FreeBSD 14.0 RELEASE - postgreSQL - 概要・初期設定

 クラウディア
1. 概要
2. インストール
3. コンフィグレーションファイル更新
4. データベースの初期化と起動

1. 概要

 PostgreSQL は、マルチプラットフォームで動作する、リレーショナルデータベースです。  「FreeBSD」上でもフルスペックで操作します。  データベースといえば、Oracle が有名ですが、「Oracle」は「Linux」のサポートを始めたようですが、「FreeBSD」はサポートされていません。  しかも、有償で個人では手が出せない金額です。  「PostgreSQL」は標準的な、「SQL」コマンドはほとんど実装されています。  大規模なデータベースでも、実用的になんら不自由は感じない、優秀なデータベースです。

2. インストール

 2023年12月14日時点で、下記のバージョンが存在します。

$ ls -d /usr/ports/databases/postgresql*-server
/usr/ports/databases/postgresql11-server  /usr/ports/databases/postgresql14-server
/usr/ports/databases/postgresql12-server  /usr/ports/databases/postgresql15-server
/usr/ports/databases/postgresql13-server  /usr/ports/databases/postgresql16-server
 バージョン「10.4」にしておきます(最新は、避けるのだ)。

cd /usr/ports/databases/postgresql14-server
make config
 デフォルトの状態です。
「FreeBSD 14.0 RELEASE」-「/usr/ports/databases/postgresql14-server」「オプション」

 「GSSAPI」は、わたしの設定では何かと支障があったのでチェックしない方がよいかと思います。
 「XML」は、役に立つかもしれないのでチェックをいれておきます。
 あとは、デフォルトのままで。

 ここに至るまでに、他のアプリケーションとの依存関係で、「postgresqlxx-client」がインストールされてしまっているときは。
 それを先に、アンインストールしてから作業します。
 インストールされているかは、下記で確認


pkg version -v | grep postgresql
 なんも表示されなければ、そのまま次項へ。  例えば、下記のように表示された場合は。

postgresql12-client-12.15          =   up-to-date with index
 「postgresql12-client」がインストールされているので、下記の要領でアンインストールします。

cd /usr/ports/databases/postgresql12-client
make deinstall clean
 「postgresql14-server」インストール。

cd /usr/ports/databases/postgresql14-server
make
make install
 「postgresql14-client」も必要になりますが、「server」、「client」間には依存関係が存在します。  「server」をインストールすれば「client」も自動的にインストールされます。  「postgresql14-client」のオプションは、デフォルトのままにします。  ところが、前回、「postgresql12-server」は、「llvm13」を必要としていて、「llvm13」がどうしてもインストールできないものだから。  苦しまぎれに、「llvm13」は、「pkg」でインストールしました。  今回必要だったのは、「llvm15」。  幸い、エラーにはなりませんでしたが、「llvm15」のインストールには、数時間かかります。  インストール時のメッセージを掲載しておきます(いささか長くなります)。

Installing postgresql14-server-14.10_1...
===> Creating groups.
Creating group 'postgres' with gid '770'.
===> Creating users
Creating user 'postgres' with uid '770'.
===> Creating homedir(s)

  =========== BACKUP YOUR DATA! =============
  As always, backup your data before
  upgrading. If the upgrade leads to a higher
  major revision (e.g. 9.6 -> 10), a dump
  and restore of all databases is
  required. This is *NOT* done by the port!
  See https://www.postgresql.org/docs/current/upgrading.html
  ===========================================
For procedural languages and postgresql functions, please note that
you might have to update them when updating the server.

If you have many tables and many clients running, consider raising
kern.maxfiles using sysctl(8), or reconfigure your kernel
appropriately.

The port is set up to use autovacuum for new databases, but you might
also want to vacuum and perhaps backup your database regularly. There
is a periodic script, /usr/local/etc/periodic/daily/502.pgsql, that
you may find useful. You can use it to backup and perform vacuum on all
databases nightly. Per default, it performs 'vacuum analyze'. See the
script for instructions. For autovacuum settings, please review
~postgres/data/postgresql.conf.

If you plan to access your PostgreSQL server using ODBC, please
consider running the SQL script /usr/local/share/postgresql/odbc.sql
to get the functions required for ODBC compliance.

Please note that if you use the rc script,
/usr/local/etc/rc.d/postgresql, to initialize the database, unicode
(UTF-8) will be used to store character data by default.  Set
postgresql_initdb_flags or use login.conf settings described below to
alter this behaviour. See the start rc script for more info.

To set limits, environment stuff like locale and collation and other
things, you can set up a class in /etc/login.conf before initializing
the database. Add something similar to this to /etc/login.conf:
---
postgres:\
        :lang=en_US.UTF-8:\
        :setenv=LC_COLLATE=C:\
        :tc=default:
---
and run 'cap_mkdb /etc/login.conf'.
Then add 'postgresql_login_class="postgres"' to /etc/rc.conf.

======================================================================

To initialize the database, run

  /usr/local/etc/rc.d/postgresql initdb

You can then start PostgreSQL by running:

  /usr/local/etc/rc.d/postgresql start

For postmaster settings, see ~postgres/data/postgresql.conf

NB. FreeBSD's PostgreSQL port logs to syslog by default
    See ~postgres/data/postgresql.conf for more info

NB. If you're not using a checksumming filesystem like ZFS, you might
    wish to enable data checksumming. It can be enabled during
    the initdb phase, by adding the "--data-checksums" flag to
    the postgresql_initdb_flags rcvar. Otherwise you can enable it later by
    pg_checksums.  Check the initdb(1) manpage for more info
    and make sure you understand the performance implications.

======================================================================

To run PostgreSQL at startup, add
'postgresql_enable="YES"' to /etc/rc.conf

===> SECURITY REPORT:
      This port has installed the following files which may act as network
      servers and may therefore pose a remote security risk to the system.
/usr/local/bin/postgres

      If there are vulnerabilities in these programs there may be a security
      risk to the system. FreeBSD makes no guarantee about the security of
      ports included in the Ports Collection. Please type 'make deinstall'
      to deinstall the port if this is a concern.

      For more information, and contact details about the security
      status of this software, see the following webpage:
https://www.postgresql.org/

3. コンフィグレーションファイル更新

 「ostgresSQL」を起動できるように、「/etc/rc.conf」を編集します。  「root」ユーザで

sysrc postgresql_enable=YES

4. データベースの初期化と起動

 2022年5月23日時点のバージョン「12.11」では、スクリプトに関して、オプションが 4、パラメータが 14 用意されています。  (やたら増えたので、理解できていないものが多数あります)  代表的に使用するパラメータについて説明しておきます。

service postgresql start      起動
service postgresql stop       停止
service postgresql restart    再起動
service postgresql initdb     初期化
service postgresql status     ステータスの確認
 まず、最初に、データベースの初期化と起動を行います。  初期化

service postgresql initdb
 以下のようなメッセージが表示されます。

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 locales
  COLLATE:  C
  CTYPE:    C.UTF-8
  MESSAGES: C.UTF-8
  MONETARY: C.UTF-8
  NUMERIC:  C.UTF-8
  TIME:     C.UTF-8
The default text search configuration will be set to "english".

Data page checksums are disabled.

creating directory /var/db/postgres/data14 ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Japan
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    /usr/local/bin/pg_ctl -D /var/db/postgres/data14 -l logfile start
 起動

service postgresql start
 以下のようなメッセージが表示されます。

2023-05-31 16:52:21.119 JST [50422] LOG:  ending log output to stderr
2023-05-31 16:52:21.119 JST [50422] HINT:  Future log output will go to log destination "syslog".
 「postgresql12」では、メッセージがやけに多かったのですが、「postgresql14」で以前のように戻ったようです。
earthcar(アースカー)
メンズミレット
葬送のフリーレン Prime Video