Configuring PgBouncer
=====================

PEMデータベースサーバーと連携するようにPgBouncerを構成する必要があります。

構成手順におけるディレクトリとファイルの名前と場所は、PgBouncerのコミュニティバージョンとEDB
PgBouncerのどちらをインストールしたかによって異なります。この手順では、EDB
PgBouncerを使用していることを前提としています。コミュニティリポジトリからPgBouncerをインストールしている場合、例のファイルとディレクトリの名前をPgBouncer値に置き換えます。

.. csv-table::
  :header: Name,PgBouncer,EDB PgBouncer
  :widths: 12,25,15
  :align: left
  :class: longtable

  PgBouncer directory,`/etc/pgbouncer<1.x>`,`/etc/edb/pgbouncer<1.x>`
  ini file,`pgbouncer.ini`,`edb-pgbouncer.ini`
  userlist file,`/etc/pgbouncer<1.x>/userlist.txt`,`/etc/edb/pgbouncer<1.x>/userlist.txt`
  HBA file,`(/etc/pgbouncer<1.x>/hba_file)`,`(/etc/edb/pgbouncer<1.x>/hba_file)`
  Service file,`pgbouncer-<1.x>`,`edb-pgbouncer-<1.x>`

この例では、enterprisedbシステムユーザーとしてPgBouncerを実行し、PgBouncerの構成プロセスの概要を示します。

1. ターミナルウィンドウを開き、PgBouncerディレクトリに移動します。

2. PgBouncerの\ ``etc`` ディレクトリ\ ``pgbouncer.ini``
   が存在する場所の所有者を\ ``enterprisedb``
   に変更し、ディレクトリのアクセス許可を\ ``0700`` に変更します。

.. code:: shell

       $ chown -R enterprisedb:enterprisedb /etc/edb/pgbouncer<1.x>
       $ chmod 0700 /etc/edb/pgbouncer<1.x>

3. ``pgbouncer.ini`` または\ ``edb-pgbouncer.ini``
   ファイルの内容を変更します。

.. code:: ini

       [databases]
       ;; Change the pool_size according to maximum connections allowed
       ;; to the PEM database server as required.
       ;; auth_user will be used for authenticate the db user (proxy
       ;; agent user in our case)

       pem = port=5444 host=localhost dbname=pem auth_user=pgbouncer
       pool_size=80 pool_mode=transaction
       * = port=5444 host=localhost dbname=pem auth_user=pgbouncer
       pool_size=10

       [pgbouncer]
       logfile = /var/log/edb/pgbouncer<1.x>/edb-pgbouncer-<1.x>.log
       pidfile = /var/run/edb/pgbouncer<1.x>/edb-pgbouncer-<1.x>.pid
       listen_addr = *
       ;; Agent needs to use this port to connect the pem database now
       listen_port = 6432
       ;; Require to support for the SSL Certificate authentications
       ;; for PEM Agents
       client_tls_sslmode = require
       ;; These are the root.crt, server.key, server.crt files present
       ;; in the present under the data directory of the PEM database
       ;; server, used by the PEM Agents for connections.
       client_tls_ca_file = /var/lib/edb/as11/data/root.crt
       client_tls_key_file = /var/lib/edb/as11/data/server.key
       client_tls_cert_file = /var/lib/edb/as11/data/server.crt
       ;; Use hba file for client connections
       auth_type = hba
       ;; Authentication file, Reference:
       ;; https://pgbouncer.github.io/config.html#auth_file
       auth_file = /etc/edb/pgbouncer<1.x>/userlist.txt
       ;; HBA file
       auth_hba_file = /etc/edb/pgbouncer<1.x>/hba_file
       ;; Use pem.get_agent_pool_auth(TEXT) function to authenticate
       ;; the db user (used as a proxy agent user).
       auth_query = SELECT * FROM pem.get_agent_pool_auth($1)
       ;; DB User for administration of the pgbouncer
       admin_users = pem_admin1
       ;; DB User for collecting the statistics of pgbouncer
       stats_users = pem_admin1
       server_reset_query = DISCARD ALL
       ;; Change based on the number of agents installed/required
       max_client_conn = 500
       ;; Close server connection if its not been used in this time.
       ;; Allows to clean unnecessary connections from pool after peak.
       server_idle_timeout = 60

..  Note::
   `auth_user` の詳細については、 `Authentication settings <https://www.pgbouncer.org/config.html#authentication-settings>`_ を参照してください。

4. PgBouncerの\ ``/etc/edb/pgbouncer<1.x>/userlist.txt``
   認証ファイルを作成および更新します。

.. code:: sql

       ## Connect to pem database as a superuser, 
       ## create the userslist.txt file and add 
       ## username and their password list in CSV format
       $ psql -p 5444 -d pem -U enterprisedb
       
       pem=# 
       COPY (
       SELECT pgbouncer::TEXT, pgbouncer_password
       UNION ALL
       SELECT pem_admin1::TEXT, pem_admin1_password)
       TO /etc/edb/pgbouncer<1.x>/userlist.txt
       WITH (FORMAT CSV, DELIMITER  , FORCE_QUOTE *);
       __OUTPUT__
       COPY 2

これにより、 ``/etc/edb/pgbouncer<1.x>/userlist.txt``
ファイルが作成され、ユーザー名とパスワードのリストが追加されます。

..  Note::
   pem_adminユーザーがスーパーユーザーの場合、パスワードを認証ファイルこの例では`enterprisedb` に追加する必要があります。これにより、 pem_adminユーザーはPEM認証照会ファンクション`pem.get_proxy_auth(text)` を呼び出すことができます。

5. 次のコンテンツを含むPgBouncerのHBAファイル\ ``(/etc/edb/pgbouncer<1.x>/hba_file)``
   を作成します。

.. code:: ini

       # Use authentication method md5 for the local connections to
       # connect pem database & pgbouncer (virtual) database.
       local pgbouncer all md5
       # Use authentication method md5 for the remote connections to
       # connect to pgbouncer (virtual database) using enterprisedb
       # user.

       host pgbouncer,pem pem_admin1 0.0.0.0/0 md5

.. code:: ini

       # Use authentication method cert for the TCP/IP connections to
       # connect the pem database using pem_agent_user1

       hostssl pem pem_agent_user1 0.0.0.0/0 cert

6. HBAファイル ``(/etc/edb/pgbouncer<1.x>/hba_file)``
   の所有者を\ ``enterprisedb`` に変更し、ディレクトリ権限を\ ``0600``
   に変更します。

.. code:: shell

       $ chown enterprisedb:enterprisedb /etc/edb/pgbouncer<1.x>/hba_file
       $ chmod 0600 /etc/edb/pgbouncer<1.x>/hba_file

7. PgBouncerサービスを有効にし、サービスを開始します。

.. code:: shell

       $ systemctl enable edb-pgbouncer-<1.x>
       __OUTPUT__
       Created symlink from
       /etc/systemd/system/multi-user.target.wants/edb-pgbouncer-<1.x>.service
       to /usr/lib/systemd/system/edb-pgbouncer-<1.x>.service.

.. code:: shell

       $ systemctl start edb-pgbouncer-<1.x>
