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

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

1. 概要

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

2. インストール


$ ls -d /usr/ports/databases/postgresql*-server
/usr/ports/databases/postgresql10-server  /usr/ports/databases/postgresql13-server
/usr/ports/databases/postgresql11-server  /usr/ports/databases/postgresql95-server
/usr/ports/databases/postgresql12-server  /usr/ports/databases/postgresql96-server

cd /usr/ports/databases/postgresql96-server
make config
「FreeBSD 12.3 RELEASE」-「/usr/ports/databases/postgresql96-server」「オプション」



cd /usr/ports/databases/postgresqlxx-client
make deinstall clean

cd /usr/ports/databases/postgresql96-server
make install
 「postgresql96-client」も必要になりますが、「server」、「client」間には依存関係が存在します。  「server」をインストールすれば「client」も自動的にインストールされます。  「postgresql96-client」のオプションは、デフォルトのままにします。  インストール時のメッセージを掲載しておきます(いささか長くなります)。

===>  Installing for postgresql96-server-9.6.24
===>  Checking if postgresql96-server is already installed
===>   Registering installation for postgresql96-server-9.6.24
Installing postgresql96-server-9.6.24...
===> 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
  minor revision (e.g. 8.3.x -> 8.4), a dump
  and restore of all databases is
  required. This is *NOT* done by the port!
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

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

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:
and run 'cap_mkdb /etc/login.conf'.
Then add 'postgresql_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 only be enabled during
    the initdb phase, by adding the "--data-checksums" flag to
    the postgresql_initdb_flags rcvar.  Check the initdb(1) manpage
    for more info and make sure you understand the performance


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

      This port has installed the following files which may act as network
      servers and may therefore pose a remote security risk to the system.

      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:

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

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

sysrc postgresql_enable=YES

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

 2020年11月4日時点のバージョン「9.6」では、スクリプトに関して、オプションが 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 locale "C".
The default text search configuration will be set to "english".

Data page checksums are disabled.

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

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/data96 -l logfile start

service postgresql start

LOG:  ending log output to stderr
HINT:  Future log output will go to log destination "syslog".