FreeBSD に PostgreSQL をインストールする

PowerDNS を PostgreSQL バックエンドでインストールして、PowerDNS Admin を動かしたいということで、 順を追って手順を記録していきます。

もくじ

  • PostgreSQL をインストールする ← 今回
  • PowerDNS をインストールする
  • PowerDNS Admin をインストールする

インストールの前に

PostgreSQL メジャーバージョンアップの場合は、忘れずに pg_dump しましょう。 でも、この記事は新規インストールを前提としているので、その辺は書きません。

インストールと設定

パッケージから postgresql14-server をインストールします*1

# pkg install postgresql14-server

サーバーの設定に関する注意はインストール時のパッケージメッセージでも出てきますが、/usr/local/share/doc/postgresql/README-server にあります。 こちらを参照すると、/etc/login.conf を設定するように書かれているので、次の項目を追記します。

postgres:\
        :lang=en_US.UTF-8:\
        :setenv=LC_COLLATE=C:\
        :tc=default:

追記したら cap_mkdb /etc/login.conf を実行します。

/etc/login.conf の設定を反映してサーバーが起動するように、/etc/rc.conf を設定します。

postgresql_login_class="postgres"
postgresql_enable="YES"

その上で、データベースの初期化を行います。

# 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

データベース関連のファイルと設定ファイルが /var/db/postgres/data14 に出来ていますので、必要に応じて pg_hba.conf と postgresql.conf を設定します。 また、log は syslog に送られますので、/var/log/postgresql.log に記録されるように syslog の設定を行います。 /usr/local/etc/syslog.d/postgresql.conf に

!postgres
*.*                                                                     /var/log/postgresql.log

と記述し、root で

# touch /var/log/postgresql.log

を実行します。さらに newsyslog の設定を /usr/local/etc/newsyslog.conf.d/postgresql.conf に次のように設定しておきます。

/var/log/postgresql.conf    644    7    10000    *    J

PowerDNS のバックエンドにするということで、slave への反映を PowerDNS 推奨の DB レプリケーションで行う場合は、localhost 以外にも bind するように設定します。PostgreSQL 14 の場合は /var/db/postgres/data14/postgresql.conf で

listen_addresses = '*'

特定のアドレスだけ listen する場合はカンマで区切ってアドレスを列挙します。

以上の設定が終わったら、

service postgresql start

postgresql server を実行します。

*1:なんて書いてぼーっとしているうちに postgresql15 が出てしまいました