OSS-DB試験勉強 - 黒本ch2 インストールと設定

OSS-DBPostgreSQL勉強メモ

PostgreSQL公式チュートリアル: Installation from Source Code

https://www.postgresql.org/docs/12/installation.html

環境を汚したくないのでコンテナで遊ぶ

[https://github.com/wand2016/oss-db-learning:embed:cite]

ビルド環境絡み

  • build-essential

    • 入れないとconfigure: error: no acceptable C compiler found in $PATHと怒られる
  • libreadline6-dev

    • psqlでコマンドライン編集・ヒストリ機能を利用するのに必要
    • configureの --without-readline オプションで無効化可能
  • zlib1g-dev

    • pg_dump, pg_restore機能に必要
    • configureの --without-zlib オプションで無効化可能

configure のオプション

問題集で触れられていたもの

  • --prefix=DIRECTORY

    • インストール場所指定
    • --bindir等に使われる
  • --with-openssl

    • 暗号化コネクション機能有効化
    • cryptoライブラリ必須

      • apt-get install libssl-dev
  • --enable-debug

    • デバッグシンボル付きで全プログラム・全ライブラリをコンパイル
  • --with-pgport=NUMBER

    • サーバ/クライアントのデフォルトポート番号指定
    • 未指定の場合5432
  • --with-krb5

    • Kerberos5認証を行う場合に使用する

      • PostgreSQL version 9.5で消えたみたい
  • --with-perl

    • PL/Perlサーバサイド手続き型言語をビルド
    • PL: Procedural Language
  • --with-python

    • PL/Pythonサーバサイド手続き型言語をビルド

変遷まとめ記事。ありがたい

https://qiita.com/nuko_yokohama/items/e2d479a1af0de6cc50a6

gmakeコマンドに関して

https://github.com/postgres/postgres/blob/master/GNUmakefile.in

  • make or make all

    • ビルド

      • べつにrootである必要なし
  • make check

    • 正常動作検証
    • 通らないとこういうエラーが出る:
pg_regress: initdb failed
Examine /work/postgresql-12.1/src/test/regress/log/initdb.log for the reason.
Command was: "initdb" -D "/work/postgresql-12.1/src/test/regress/./tmp_check/data" --no-clean --no-sync > "/work/postgresql-12.1/src/test/regress/log/initdb.log" 2>&1
make[1]: *** [check] Error 2
  • make install

    • インストール(コピー)

      • --prefixで指定の場所
      • デフォルトで/usr/local/pgsql/bin
  • make distclean

    • configureコマンドで間違ったオプションを指定してしまった場合に実行することが推奨されている

環境変数

https://www.postgresql.org/docs/current/libpq-envars.html

環境変数 対応するパラメータ
PGHOST host
PGHOSTADDR hostaddr
PGPORT port
PGDATABASE dbname

initdb

https://www.postgresql.org/docs/12/app-initdb.html

  • /usr/local/pgsql/bin/initdbというexecutableがある

    • pg_ctl initdb でも呼び出せる
  • 新しいPostgreSQLデータベースクラスタを作成する
  • PostgreSQLデータベースクラスタとは

    • 単一のサーバーにより管理されるデータベース群
  • initdbを実行すると、デフォルトで3つのデータベースができる
/# su postgres
/$ initdb
/$ psql
postgres=# \?

...
  \l[+]   [PATTERN]      list databases
...

postgres=# \l
\l
                                 List of databases
   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges   
-----------+----------+----------+------------+------------+-----------------------
 postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | 
 template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
(3 rows)
  • template1は、データベース作成時にデフォルトで使用される雛形

    • 必ず作成するテーブル等を定義しておくとよい
  • PostgreSQL 9.0からはデフォルトでPL/pgSQL言語インストール

    • cf. 昔はcreatelangコマンドが必要だった
/$ createlang
Error: pg_wrapper: createlang was not found in /usr/lib/postgresql/12/bin

contribディレクトリ

https://www.postgresql.org/docs/12/contrib.html

  • PostgreSQL本体に取り込まれていないモジュール

    • 限られたユーザ向け
    • 実験的すぎ
  • contribution の略

    • ユーザから寄贈された、の意
  • 含めたい場合は、ソースコードからインストールする際
make all

の代わりに

make world

を使用する

postgresql.conf

  • #始まりの行はコメント
  • サーバ再起動が必要なものの反映方法

    • pg_ctl stop + pg_ctl start
    • または pg_ctl restart
  • 再起動の必要ないものの反映方法

    • SIGHUPを送る(1番)
    • または pg_ctl reload

接続・認証

https://www.postgresql.org/docs/12/runtime-config-connection.html

軒並みサーバ再起動が必要

黒本で触れられているもの

  • listen_addresses (string)

    • *だと任意のIPアドレスをリッスン
    • 0.0.0.0だと任意のIPv4アドレス
    • ::だと任意のIPv6アドレス
    • 空だと一切リッスンしない

      • Unixドメインソケット通信でのみ接続可能
    • デフォルトlocalhost

      • ループバックのみ許可
    • 悪意のあるコネクション要求が繰り返されるのを防止するために使用する

      • cf. クライアント側の認証は細やかなアクセス制御に使用する
  • port (integer)

    • TCPポート番号(5432)
  • max_connections (integer)

  • superuser_reserved_connections (integer)

    • スーパーユーザのために予約する接続数(3)

      • max_connectionsが100、本パラメータが3なら、一般ユーザは97接続まで

リソース、WAL: Write Ahead Log

https://www.postgresql.org/docs/12/runtime-config-resource.html

https://www.postgresql.org/docs/12/runtime-config-wal.html

  • shared_buffers (integer in MB)

    • デフォルト値は小さめ

      • PostgreSQL 9.0だと
      • 【補】PostgreSQL 12だと128MB
      • スペックが低いH/Wでも動作するための配慮

        • 実運用を考慮してサイズを決めよう
    • 設定反映にサーバ再起動必要
  • temp_buffers (integer in MB)

    • データベースセッションが一時的に使用するメモリ

      • ソートなど
  • maintenance_work_mem (integer in MB)

    • 保守作業用メモリ

      • VACUUM
      • CREATE INDEX
      • ALTER TABLE ADD FOREIGN KEY
      • 等々
  • wal_buffers (integer in MB)

    • ロク先行書き込み(トランザクションログ)用に共有メモリで確保される確保されるメモリサイズ
    • 設定反映にサーバ再起動必要