<設定ファイル>
/etc/rc.conf
/etc/resolv.conf
/usr/local/etc/smb4.conf
/usr/local/etc/rc.d
/usr/local/etc/initd.conf


<FreeBSDのシャットダウンと再起動>
シャットダウン前にpostgresを止めておくことを推奨された。postgresで
$ pg_ctl stop
rootで
# shutdown -p now (シャットダウン)
# shutdown -r now (再起動)
# reboot (再起動)

<設定ファイルの注意>
Windowsで作ったtxtファイルは改行がCr+Lfになっているのでそのままでは使えない。
改行をLfに変更すること。


まず、
# pkg update
※初めて実行する場合、「Do you want to fetch and install pkg now? [y/N]:」と聞かれるので y を押してください。

<Sambaの共有ディレクトリ>
# mkdir /home/pub
# chmod 777 /home/pub
# chown nobody:nobody /home/pub

<postgresユーザーの作成>
# pw useradd postgres -m -s /bin/sh -c "PostgreSQL Administrator"
-m: ホームディレクトリ(/home/postgres)を作成します
-s /bin/sh: ログインシェルを指定します
-c: コメント(説明)を付けます
# passwd postgres

<PostgreSQLで使うディレクトリ>
# mkdir /usr/local/src
(Linuxの時は既にこのディレクトリはあったが,FreeBSDではなかったので作成)
# chown postgres:postgres /usr/local/src (postgrs以外では使わない) 
# mkdir /home/pgdata
# chown postgres:postgres /home/pgdata (PostgreSQLのデータディレクトリ)
# mkdir /usr/local/pgsql
# chown postges:postgres /usr/local/pgsql (bin他が入る)

Linuxの時は/homeの容量を大きくとって、/home/pgdataにデータを置いていた。
ZFSの場合はドライブごと容量管理するので、特に/homeを大きく取る必要がない。
なので、postgresqlのdataは通常よく使われる
/usr/local/pgsql/data
にした方が良いかもしれない。

<Sambaのインストール>
# pkg install samba419 (sambaの再新版)
# cp /usr/local/etc/smb4.conf.sample /usr/local/etc/smb4.conf
# vi /usr/local/etc/smb4.conf
smb4.conf.sampleが見つからなかったので、WinodowPCから従来使っているsmb.confを入力した。
従来のsmb.confからの修正無し。

# smbpasswd -a postgres (通常postgresでログインするので)
# sysrc samba_server_enable="YES"
  (これで設定ファイルに書き込まれる。再起動時もサービスが自動開始される)
# service samba_server start 

<PostgreSQLのインストール>
makeにgmakeを使うところ以外はLinuxの場合と同じ
gmake: FreeBSD標準の make ではなく、GNU版の gmake を使うのが PostgreSQL ビルドの定石です。

GNU makeのインストール
# pkg install gcc gmake bison flex readline icu pkgconf

# pkg install perl5 (これがないとconfigureでエラーになった)
インストールが終わったら、念のためパスが通っているか確認します。
# perl -v
バージョン情報が表示されればOKです。

WindowsPCからソースコード(.tar.gz)をダウンロード。
ここから後はLinuxと同じ。
# cd /usr/local/src/
# tar -zxvf /home/pub/postgresql_source_mei
# su postgres
$ cd /usr/local/src/p*
$ ./configure --prefix=/usr/local/pgsql
$ gmake (結構時間がかかる、10分くらいかかった)
$ gmake install 
 (Geminiはrootで実行するようにとのことだったが、いつもpostgresで実行しいる)
(結論から言うと、そのまま postgres ユーザーで進めて大丈夫です。むしろ、ビルド(gmake)までは一般ユーザーで行うのが UNIX の伝統的な正しい作法です。)

インストールの確認
$ /usr/local/pgsql/bin/pg_config --version
インストールしたバージョンが表示されれば成功

環境変数の設定
$ vi /home/postgres/.shrc
.shrcの最後に下記を追加
export PATH=/usr/local/pgsql/bin:$PATH
export PGDATA=/home/pgdata

設定の反映
$ source /home/postgres/.shrc

Linuxの時は/homeの容量を大きくとって、/home/pgdataにデータをおいていた。
ZFSの場合はドライブごと容量管理するので、特に/homeを大きくとる作業がない。
なので、postgresqlのdataは通常よく使われる
/usr/local/pgsql/data
にした方が良いかもしれない。

環境変数が設定されているかどうか確認(PGDATA=/home/pgdata)
$ env
(rootでログインしてsu postgresしても.shrcは読み込まれない
permissin deniedになる。postgresで作業する時は最初からpostgresでログインすること)


PGDATAの設定が正しければ
$ /usr/local/pgsql/bin/initdb --no-locale --encoding=UTF8
($ /usr/local/pgsql/bin/initdb --no-locale --encoding=UTF8 -D /usr/local/pgsql/data)

動作するか確認
$ pg_ctl start
$ pg_ctl status
    pg_ctl: server is running (PID: 1234) と表示されればOK
    pg_ctl: no server running 動いてない
$ pg_ctl stop

ソースからインストールしたので、自動起動スクリプトを作成しなければならない
起動スクリプトの作成
WindowsのGeminiさんの画面から下記をコピーして/home/pubに保存

ファイル名: postgres (拡張子無し)
################################################################################

#!/bin/sh
#
# PROVIDE: postgres
# REQUIRE: LOGIN
# KEYWORD: shutdown

. /etc/rc.subr

name="postgres"
rcvar="postgres_enable"

# あなたの環境に合わせたパスを指定
postgres_user="postgres"
postgres_data="/home/pgdata"
postgres_binpath="/usr/local/pgsql/bin"

command="${postgres_binpath}/pg_ctl"
pidfile="${postgres_data}/postmaster.pid"

start_cmd="postgres_start"
stop_cmd="postgres_stop"
status_cmd="postgres_status"

postgres_start() {
    echo "Starting PostgreSQL..."
    su - ${postgres_user} -c "${command} start -D ${postgres_data}"
}

postgres_stop() {
    echo "Stopping PostgreSQL..."
    su - ${postgres_user} -c "${command} stop -D ${postgres_data} -m fast"
}

postgres_status() {
    su - ${postgres_user} -c "${command} status -D ${postgres_data}"
}

load_rc_config $name
run_rc_command "$1"

################################################################################

下記でCr+LfをLfに変換
# 改行コード(\r)を取り除くコマンド
sed -i '' 's/\r//' /usr/local/etc/rc.d/postgres

=================
下記は上記にCr+Lf変換前に実行して、そのまま残っている。
これが必要かどうかわからない。

中身の postgres_start 部分を以下のように書き換えます(${postgres_binpath}/ を追加)。
postgres_start() {
    echo "Starting PostgreSQL..."
    # command の前にフルパスを指定
    su - ${postgres_user} -c "${postgres_binpath}/pg_ctl start -D ${postgres_data}"
}
=================

上記スクリプトをコピー
# cp /home/home/pub/postgres /usr/local/etc/rc.d/


実行権限を付与
# chmod 555 /usr/local/etc/rc.d/postgres

自動起動を有効にする
# sysrc postgres_enable="YES" (これで、自動起動するように保存される)

service コマンドが使えるようになりました!
起動: service postgres start
確認: service postgres status
停止: service postgres stop

いつもの、pg_hba.conf、postgres.confを編集